void GeneratePlatforms() { CamToWorldUtility.CameraBounds camBounds = CamToWorldUtility.GetCameraBoundsInWorld(cam); float startY = camBounds.down.y; // left player float p1LeftCorner = camBounds.left.x; float p1RightCorner = camBounds.up.x; p1PlatformsGen = gameObject.AddComponent <PlatformGenerator>(); p1PlatformsGen.Initialize(cam, platforms, platformsContainer, deathRays, deathRaysContainer, p1LeftCorner, p1RightCorner, startY, deathRaysProbability, deathRaysOffset, initialVelocity, velocityIncrement); p1PlatformsGen.Generate(); // right player float p2LeftCorner = camBounds.up.x; float p2RightCorner = camBounds.right.x; p2PlatformsGen = gameObject.AddComponent <PlatformGenerator>(); p2PlatformsGen.Initialize(cam, platforms, platformsContainer, deathRays, deathRaysContainer, p2LeftCorner, p2RightCorner, startY, deathRaysProbability, deathRaysOffset, initialVelocity, velocityIncrement); p2PlatformsGen.Generate(); }
/// <summary> /// Allows the game to perform any initialization it needs to before starting to run. /// This is where it can query for any required services and load any non-graphic /// related content. Calling base.Initialize will enumerate through any components /// and initialize them as well. /// </summary> protected override void Initialize() { //Init AI State platformGenerator.Initialize(); //Init Keyboard State oldState = Keyboard.GetState(); //Create Camera camera = new Camera(); camera.Viewport = new Vector2(GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height); camera.Position = new Vector2(0); //Init Game State countdownVal = countdownStart; //Create the Player Component player = new Player(new RigidBody2D(new Vector2(0), new Vector2(0.3f, 0.3f), new Vector3(0, 0, 0), 0.5f, false, new RigidBody2D.FrictionCoefficients() { StaticCoefficient = 0.0f, DynamicCoefficient = 0.0f }, 0.8f), 75.0f, 7500.0f); player.BoxCollider = new Vector2(31.5f, 45); player.Tag = "Player"; player.CameraRef = camera; //Add to the collections physics.RigidBodies.Add(player); gameObjects.Add(player); //Create Enemy Component enemy = new Enemy(new RigidBody2D(new Vector2(0), new Vector2(0.3f, 0.3f), new Vector3(0, 0, 0), 0.5f, false, new RigidBody2D.FrictionCoefficients() { StaticCoefficient = 0.0f, DynamicCoefficient = 0.0f }, 0.8f), 90.0f, 7700.0f, updateStep); enemy.AI = ai; enemy.BoxCollider = new Vector2(31.5f, 45); enemy.Tag = "Enemy"; enemy.CameraRef = camera; physics.RigidBodies.Add(enemy); gameObjects.Add(enemy); platformGenerator.EnemyReference = enemy; //Create the Floor Object Platform ground = new Platform(); ground.Mass = 200; ground.IsStaticHorizontal = true; ground.Position = new Vector2(0, camera.Viewport.Y - 20); ground.Scale = new Vector2(0.2f, 0.2f); ground.Size = new Vector2(camera.Viewport.X, 20); ground.BoxCollider = new Vector2(camera.Viewport.X, 20); ground.Friction = new RigidBody2D.FrictionCoefficients() { StaticCoefficient = 0.7f, DynamicCoefficient = 0.7f }; ground.Bounciness = 0.0f; WaypointNode leftGround = new WaypointNode(); leftGround.Position = new Vector2(ground.Position.X + PlatformGenerator.WaypointEdgeBuffer, ground.Position.Y - player.BoxCollider.Y); leftGround.ConnectedPlatform = ground; WaypointNode rightGround = new WaypointNode(); rightGround.Position = new Vector2(ground.Position.X + ground.Size.X - PlatformGenerator.WaypointEdgeBuffer, ground.Position.Y - player.BoxCollider.Y); rightGround.ConnectedPlatform = ground; leftGround.ConnectedNodes.Add(rightGround); rightGround.ConnectedNodes.Add(leftGround); ground.ConnectedWaypoints.AddRange(new WaypointNode[] { leftGround, rightGround }); ai.WaypointNetwork.AddRange(new WaypointNode[] { leftGround, rightGround }); previousPlatformRow = new Platform[] { ground }; //Set initial enemy & player positions player.Position = leftGround.Position; enemy.Position = new Vector2(rightGround.Position.X - enemy.BoxCollider.X, rightGround.Position.Y); enemy.NextNode = rightGround; enemy.PreviousNode = rightGround; //Create Background Object Background bg = new Background(); bg.SceneCamera = camera; bg.TextureFile = "blue_grass.png"; gameObjects.Add(ground); physics.RigidBodies.Add(ground); gameObjects.Add(bg); //Create the initial on-screen platforms List <Platform> platforms = new List <Platform>(); for (int i = (int)camera.Viewport.Y - PlatformDistance; i > 0 - YPlatformBuffer; i -= PlatformDistance) { Platform[] platformRow = platformGenerator.GeneratePlatforms(new Vector2(0, i), camera.Viewport.X); platforms.AddRange(platformRow); WaypointNode[] genPlatforms = platformGenerator.GenerateWaypoints(platformRow, previousPlatformRow, enemy.BoxCollider.Y); previousPlatformRow = platformRow; ai.WaypointNetwork.AddRange(genPlatforms); } for (int i = 0; i < platforms.Count; i++) { platforms[i].Initialize(); gameObjects.Add(platforms[i]); physics.RigidBodies.Add(platforms[i]); } nextPlatformY = camera.Position.Y - YPlatformBuffer; //Initialise all our game objects for (int i = 0; i < gameObjects.Count; i++) { gameObjects[i].Initialize(); } base.Initialize(); //Set the initial game state currentGameState = GameState.Countdown; }