public Level(Entity player, Texture2D map, Entity[] npcs, GameObject[] objects) { this.player = player; this.map = map; this.npcs = new List<Entity>(npcs.Length); this.npcs.AddRange(npcs); this.objects = new List<GameObject>(objects.Length); this.objects.AddRange(objects); projectiles = new List<Projectile>(); this.selectedObject = null; }
public InputManager(Game1 game, Entity player, Level level, List<Screen> screens) { this.game = game; this.player = player; this.selectedObject = null; this.level = level; collisionManager = new CollisionManager(player, level); screenManager = new ScreenManager(screens[1], screens); stepSize = game.getStepSize(); midX = game.getMidX(); midY = game.getMidY(); }
public void setSelectedObject(int index) { this.selectedObject = objects[index]; }
/* * Handles all user input to the game, depending on the keyboard/mouse states and the screen's state. */ public void update(GameTime time) { KeyboardState kbs = Keyboard.GetState(); Vector2 destination; /* Normal gameplay (player has control of character's movement*/ if (screenManager.getActiveScreen().getName() == "Normal") { if (kbs.IsKeyDown(Keys.Escape)) { game.Exit(); } else if (kbs.IsKeyDown(Keys.W)) { Console.WriteLine("player: " + player.getLocation().ToString()); player.setDirection(Direction.NORTH); destination = new Vector2(player.getLocation().X, player.getLocation().Y - stepSize); if (player.getLocation().Y + stepSize > 0 && collisionManager.isValid(destination, player)) { player.deriveY(-stepSize); } else { Console.WriteLine("u wanna go north? nty"); } } else if (kbs.IsKeyDown(Keys.S)) { Console.WriteLine("player: " + player.getLocation().ToString()); player.setDirection(Direction.SOUTH); destination = new Vector2(player.getLocation().X, player.getLocation().Y + stepSize); if (player.getLocation().Y + stepSize < midY * 2 && collisionManager.isValid(destination, player)) { player.deriveY(stepSize); } else { Console.WriteLine("u wanna go south? nty"); } } else if (kbs.IsKeyDown(Keys.A)) { Console.WriteLine("player: " + player.getLocation().ToString()); player.setDirection(Direction.WEST); destination = new Vector2(player.getLocation().X - stepSize, player.getLocation().Y); if (player.getLocation().X + stepSize > 0 && collisionManager.isValid(destination, player)) { player.deriveX(-stepSize); } else { Console.WriteLine("u wanna go west? nty"); } } else if (kbs.IsKeyDown(Keys.D)) { Console.WriteLine("player: " + player.getLocation().ToString()); player.setDirection(Direction.EAST); destination = new Vector2(player.getLocation().X + stepSize, player.getLocation().Y); if (player.getLocation().X + stepSize < midX * 2 && collisionManager.isValid(destination, player)) { player.deriveX(stepSize); } else { Console.WriteLine("u wanna go east? nty"); } } if (kbs.IsKeyDown(Keys.Space)) { double totalMilliseconds = time.TotalGameTime.TotalMilliseconds; if (player.getLastFired() == -1 || totalMilliseconds - player.getLastFired() >= player.getProjectile().getCooldown()) { level.addProjectile(player.createProjectile(totalMilliseconds)); } /*foreach (Entity e in level.getNpcs()) { if (e != null) { npc.deriveHealth(-50); // handle projectile interaction with npcs // if hit, derive npc health by -1 * skilltree power Console.WriteLine("Health: " + npc.getHealth()); } }*/ } else if (kbs.IsKeyDown(Keys.X)) { //switch to telekinesis-select mode (player clicks a liftable object to select it) screenManager.setActiveScreen(2); Console.WriteLine("Entered telekinesis mode!"); } } /* Just entered telekinesis mode (player uses mouse to select a liftable object)*/ else if (screenManager.getActiveScreen().getName() == "Telekinesis-Select") { lastState = state; state = Mouse.GetState().LeftButton; if (lastState.Equals(ButtonState.Pressed) && state.Equals(ButtonState.Released)) { foreach (GameObject obj in level.getObjects()) { if (obj != null && obj.isLiftable()) { Point p = new Point(Mouse.GetState().X, Mouse.GetState().Y); if (p != null && obj.getBounds().Contains(p)) { //select object obj.setSelected(true); selectedObject = obj; //switch screen state to telekinesis-move (control over selected object) screenManager.setActiveScreen(3); Console.WriteLine("obj contains mouse!"); } } } Console.WriteLine("Mouse pressed"); } else if (kbs.IsKeyDown(Keys.X)) { //switch to telekinesis-select mode (player clicks a liftable object to select it) screenManager.setActiveScreen(1); Console.WriteLine("Entered telekinesis mode!"); } } /* Telekinetic lifting mode (player controls the selected object's movement)*/ else if (screenManager.getActiveScreen().getName() == "Telekinesis-Move") { if (kbs.IsKeyDown(Keys.Escape)) { game.Exit(); } else if (kbs.IsKeyDown(Keys.W)) { Console.WriteLine("selectedObject: " + selectedObject.getLocation().ToString()); selectedObject.setDirection(Direction.NORTH); //destination = new Vector2(selectedObject.getLocation().X, selectedObject.getLocation().Y + stepSize); if (selectedObject.getLocation().Y + stepSize > 0/* && collisionManager.isValid(destination)*/) { selectedObject.deriveY(-stepSize); } else { Console.WriteLine("u wanna go north? nty"); } } else if (kbs.IsKeyDown(Keys.S)) { Console.WriteLine("selectedObject: " + selectedObject.getLocation().ToString()); selectedObject.setDirection(Direction.SOUTH); //destination = new Vector2(selectedObject.getLocation().X, selectedObject.getLocation().Y - stepSize); if (selectedObject.getLocation().Y - stepSize < midY * 2/* && collisionManager.isValid(destination)*/) { selectedObject.deriveY(stepSize); } else { Console.WriteLine("u wanna go south? nty"); } } else if (kbs.IsKeyDown(Keys.A)) { Console.WriteLine("selectedObject: " + selectedObject.getLocation().ToString()); selectedObject.setDirection(Direction.WEST); //destination = new Vector2(selectedObject.getLocation().X + stepSize, selectedObject.getLocation().Y); if (selectedObject.getLocation().X + stepSize > 0/* && collisionManager.isValid(destination)*/) { selectedObject.deriveX(-stepSize); } else { Console.WriteLine("u wanna go west? nty"); } } else if (kbs.IsKeyDown(Keys.D)) { Console.WriteLine("selectedObject: " + selectedObject.getLocation().ToString()); selectedObject.setDirection(Direction.EAST); //destination = new Vector2(selectedObject.getLocation().X - stepSize, selectedObject.getLocation().Y); //Console.WriteLine("dest: " + destination.ToString()); if (selectedObject.getLocation().X - stepSize < midX * 2/* && collisionManager.isValid(destination)*/) { selectedObject.deriveX(stepSize); } else { Console.WriteLine("u wanna go east? nty"); } } if (kbs.IsKeyDown(Keys.Space)) { } else if (kbs.IsKeyDown(Keys.X)) { //deselect object selectedObject.setSelected(false); selectedObject = null; //switch back to normal screen state (control over character) screenManager.setActiveScreen(1); Console.WriteLine("Exited telekinesis mode."); } } }
/// <summary> /// LoadContent will be called once per game and is the place to load /// all of your content. /// </summary> protected override void LoadContent() { base.LoadContent(); // Create a new SpriteBatch, which can be used to draw textures. spriteBatch = new SpriteBatch(GraphicsDevice); playerTexture = Content.Load<Texture2D>("player"); midX = (graphics.PreferredBackBufferWidth - playerTexture.Width) / 2; midY = (graphics.PreferredBackBufferHeight - playerTexture.Height) / 2; player = new Entity(playerTexture, new Projectile(Content.Load<Texture2D>("bullet"), new Vector2(midX, midY), 7, 250, 0.5f), new Vector2(midX, midY), Direction.EAST, GraphicsDevice.Viewport.Bounds, 50, 5); npc = new Entity(Content.Load<Texture2D>("npc"), null, new Vector2(midX + 148, midY + 148), Direction.EAST, GraphicsDevice.Viewport.Bounds, 50, 5); obj = new GameObject(Content.Load<Texture2D>("sprite"), null, new Vector2(midX + 100, midY + 100), GraphicsDevice.Viewport.Bounds, true); level = new Level(player, Content.Load<Texture2D>("map"), new Entity[] { npc }, new GameObject[] { obj }); // Initialize list of game screens, and add screens. Menu should be the first screen active. screens = new List<Screen>(); screens.Add(new Screen("Menu", false)); screens.Add(new Screen("Normal", true)); screens.Add(new Screen("Telekinesis-Select", false)); screens.Add(new Screen("Telekinesis-Move", false)); inputManager = new InputManager(this, player, level, screens); cursor = Content.Load<Texture2D>("cursor"); // TODO: use this.Content to load your game content here }