Exemplo n.º 1
0
        public bool CreateVehicle(V2DScreen screen, TimeSpan totalGameTime)
        {
            bool result = false;

            if (laneKind == LaneKind.Empty)
            {
                return(result);
            }

            if (!((BaseScreen)screen).WaitingOnPanel)
            {
                if (firstVehicle)
                {
                    firstVehicle = false;
                    laneWidth    = (int)screen.ClientSize.X;
                    if (!createVehicleOnStartup || rnd.Next(maxCreationDelay) < minCreationDelay)
                    {
                        SetNextVehicleTime(totalGameTime);
                        return(result);
                    }
                }

                if (CanCreateVehicle())
                {
                    // todo: move this into vehicles
                    V2DInstance vInst;
                    switch (laneKind)
                    {
                    case LaneKind.Sidewalk:
                        vehicleStyleIndex = rnd.Next(4);
                        vInst             = screen.CreateInstanceDefinition("sidewalker" + vehicleStyleIndex, "vehicle" + nextDepth);
                        vInst.Rotation    = movesRight ? rightAngle : leftAngle;
                        break;

                    case LaneKind.Car:
                        if (rnd.Next(20) == 0)
                        {
                            // special vehicles
                            vehicleStyleIndex = rnd.Next(2) + 11;
                        }
                        else
                        {
                            vehicleStyleIndex = rnd.Next(11);
                        }
                        vInst          = screen.CreateInstanceDefinition("vehicle" + vehicleStyleIndex, "vehicle" + nextDepth);
                        vInst.Rotation = movesRight ? rightAngle : leftAngle;
                        break;

                    case LaneKind.Train:
                        vehicleStyleIndex = 0;
                        vInst             = screen.CreateInstanceDefinition("trainFull" + vehicleStyleIndex, "vehicle" + nextDepth);
                        vInst.Rotation    = movesRight ? rightAngle : leftAngle;
                        break;

                    case LaneKind.Shinkansen:
                        vehicleStyleIndex = 1;
                        vInst             = screen.CreateInstanceDefinition("trainFull" + vehicleStyleIndex, "vehicle" + nextDepth);
                        vInst.Rotation    = movesRight ? rightAngle : leftAngle;
                        break;

                    case LaneKind.Spaceship:
                        vehicleStyleIndex = rnd.Next(8);
                        vInst             = screen.CreateInstanceDefinition("spaceship" + vehicleStyleIndex, "vehicle" + nextDepth);
                        vInst.Rotation    = movesRight ? rightAngle + (float)rnd.Next(1000) / 2000f - .25f : leftAngle + (float)rnd.Next(1000) / 2000f - .25f;
                        break;

                    case LaneKind.DrownWater:
                        vehicleStyleIndex = rnd.Next(3);
                        vInst             = screen.CreateInstanceDefinition("boat" + vehicleStyleIndex, "boat" + nextDepth);
                        vInst.Rotation    = movesRight ? rightAngle : leftAngle;
                        break;

                    case LaneKind.SwimWater:
                        vehicleStyleIndex = FindBoatIndex(screen);
                        if (vehicleStyleIndex == -1)
                        {
                            return(result);
                        }

                        if (vehicleStyleIndex == 5)
                        {
                            vInst = screen.CreateInstanceDefinition("boat" + vehicleStyleIndex, "carrier" + nextDepth);
                        }
                        else
                        {
                            vInst = screen.CreateInstanceDefinition("boat" + vehicleStyleIndex, "boat" + nextDepth);
                        }
                        vInst.Rotation = movesRight ? rightAngle : leftAngle;
                        break;

                    case LaneKind.WideCar:
                        vehicleStyleIndex = 0;
                        vInst             = screen.CreateInstanceDefinition("wideVehicle" + vehicleStyleIndex, "vehicle" + nextDepth);
                        vInst.Rotation    = movesRight ? rightAngle : leftAngle;
                        break;

                    case LaneKind.SteamRoller:
                        vInst          = screen.CreateInstanceDefinition("steamRoller", "steamRoller" + nextDepth);
                        vInst.Rotation = movesRight ? rightAngle : leftAngle;
                        break;

                    default:
                        throw new Exception("Unsupported vehicle: " + laneKind);
                    }

                    vInst.Depth = nextDepth++;

                    vInst.Y = yLocation;
                    Vector4 shapeRect = vInst.Definition.GetShapeRectangle();
                    float   h         = shapeRect.W;
                    if (movesRight)
                    {
                        vInst.X  = 10;
                        vInst.Y += laneHeight - (laneHeight - h - shapeRect.Y) / 2f;// vInst.Definition.Height) / 2f;
                    }
                    else
                    {
                        vInst.X  = laneWidth - 10;
                        vInst.Y += (laneHeight - h - shapeRect.Y) / 2f;//vInst.Definition.Height) / 2f;
                    }

                    if (firstVehicle && !oneTimeVehicle)
                    {
                        vInst.X      = rnd.Next(0, laneWidth);
                        firstVehicle = false;
                    }

                    LaneVehicle v = (LaneVehicle)screen.AddInstance(vInst, screen);
                    v.vehicleStyleIndex = vehicleStyleIndex;

                    // trains shouldn't move off track
                    if (v.VisibleWidth > 500)
                    {
                        v.body.SetFixedRotation(true);
                    }

                    v.Lane      = this;
                    v.laneY     = v.Y;
                    v.direction = new Vector2((float)System.Math.Cos(vInst.Rotation), (float)System.Math.Sin(vInst.Rotation));
                    v.MaxSpeed  = vehicleSpeed;

                    vehicles.Add(v);

                    if (laneKind == LaneKind.WideCar || laneKind == LaneKind.DrownWater || laneKind == LaneKind.SwimWater || laneKind == LaneKind.Sidewalk)
                    {
                        v.PlayAll();
                        if ((laneKind == LaneKind.DrownWater) && vehicleStyleIndex == 1)
                        {
                            v.MaxSpeed = 5;
                        }
                    }
                    else if (laneKind == LaneKind.Car && vehicleStyleIndex >= 11)
                    {
                        v.PlayAll();
                    }

                    SetNextVehicleTime(totalGameTime);
                    result = true;
                }
            }
            return(result);
        }