/// <summary> /// Allows the game to run logic such as updating the world, /// checking for collisions, gathering input, and playing audio. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> protected override void Update(GameTime gameTime) { if (currentScreen == Menu.Play) { if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) { Exit(); } GamePadState gamePadState = GamePad.GetState(PlayerIndex.One); KeyboardState keyState = Keyboard.GetState(); //ProcessMovement(keyState, gamePadState); //store current position to move back too if collision int tempx = map.ObjectGroups["Objects"].Objects["Player"].X; int tempy = map.ObjectGroups["Objects"].Objects["Player"].Y; Position = new Vector2(map.ObjectGroups["Objects"].Objects["Player"].X, map.ObjectGroups["Objects"].Objects["Player"].Y); ProcessMovement(keyState, gamePadState); //foreach(Enemy E in EnemyList) //{ //E.Update(gameTime, Position); //} //MoveEnemies(Position); //now we have moved checkbounds if (CheckBounds(map.ObjectGroups["Objects"].Objects["Player"])) { map.ObjectGroups["Objects"].Objects["Player"].X = tempx; map.ObjectGroups["Objects"].Objects["Player"].Y = tempy; } var p = map.ObjectGroups["Objects"].Objects["Player"]; Rectangle playerRec = new Rectangle(p.X, p.Y, p.Width, p.Height); CheckCoins(playerRec); Vector2 Test = (viewportPosition + new Vector2(0, 100) - new Vector2((graphics.PreferredBackBufferWidth / 2), (graphics.PreferredBackBufferHeight / 2))); foreach (var sprite in _sprites) { sprite.Update(gameTime, Position, Test); } int i = 1; foreach (Enemy E in EnemyList) { Vector2 temp = E.Position; E.Update(gameTime, Position, viewportPosition, true); if (CheckBounds(map.ObjectGroups["Objects"].Objects["Enemy" + i]) || CheckEnemy(map.ObjectGroups["Objects"].Objects["Enemy" + i], E)) { E.Position = temp; Console.WriteLine("Enemy collision"); } else { map.ObjectGroups["Objects"].Objects["Enemy" + i].X = (int)E.Position.X; map.ObjectGroups["Objects"].Objects["Enemy" + i].Y = (int)E.Position.Y; } i++; } elapsedTime += (int)gameTime.ElapsedGameTime.TotalMilliseconds; // If the elapsed time is larger than the frame time // we need to switch frames if (elapsedTime > 100) { // Move to the next frame currentFrame++; // If the currentFrame is equal to frameCount reset currentFrame to zero if (currentFrame == frameCount) { currentFrame = 0; } // Reset the elapsed time to zero elapsedTime = 0; } // Grab the correct frame in the image strip by multiplying the currentFrame index by the Frame width sourceRect = new Rectangle(currentFrame * FrameWidth, 0, FrameWidth, FrameHeight); _sprites[0].Position = new Vector2(map.ObjectGroups["Objects"].Objects["Player"].X, map.ObjectGroups["Objects"].Objects["Player"].Y); // Grab the correct frame in the image strip by multiplying the currentFrame index by the frame width //viewportPosition = new Vector2(map.ObjectGroups["Objects"].Objects["Player"].X - (graphics.PreferredBackBufferWidth / 2), map.ObjectGroups["Objects"].Objects["Player"].Y - (graphics.PreferredBackBufferHeight / 2)); viewportPosition = new Vector2(map.ObjectGroups["Objects"].Objects["Player"].X, map.ObjectGroups["Objects"].Objects["Player"].Y); KeyboardState keys = Keyboard.GetState(); // Takes to main menu if (keys.IsKeyDown(Keys.Tab)) { currentScreen = Menu.Main; } else if (keys.IsKeyDown(Keys.I)) { currentScreen = Menu.Inventory; } } else if (currentScreen == Menu.Main) { KeyboardState keys = Keyboard.GetState(); bool change = true; if (!keytimer.Enabled) { if (keys.IsKeyDown(Keys.Up)) { current.Move(Direction.Up); } else if (keys.IsKeyDown(Keys.Down)) { current.Move(Direction.Down); } else if (keys.IsKeyDown(Keys.Enter)) { string test = current.GetCurrentCaption(); if (current == menu) { if (test == "Exit") { Exit(); } else if (test == "Options") { current = options; } else if (test == "Continue") { currentScreen = Menu.Play; } } else if (current == options) { if (test == "Back") { current = menu; } } else if (current == options) { if (test == "FullScreen") { graphics.IsFullScreen = false; } } } else { change = false; } if (change) { keytimer = new Timer(); keytimer.Interval = 200; keytimer.Elapsed += new ElapsedEventHandler(OnTimedEvent); keytimer.Enabled = true; } } } base.Update(gameTime); }
protected override void Update(GameTime gameTime) { if (currentScreen == Menu.Play) { if (songplaying == false) { backsong = Content.Load <Song>("Game of Thrones"); MediaPlayer.Play(backsong); songplaying = true; } else if (MediaPlayer.State == MediaState.Stopped) { songplaying = false; } if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) { Exit(); } clean(); // get player input GamePadState gamePadState = GamePad.GetState(PlayerIndex.One); KeyboardState keyState = Keyboard.GetState(); // record current position before movement int tempx = map.ObjectGroups["Objects"].Objects["Player"].X; int tempy = map.ObjectGroups["Objects"].Objects["Player"].Y; Position = new Vector2(map.ObjectGroups["Objects"].Objects["Player"].X, map.ObjectGroups["Objects"].Objects["Player"].Y); // process movement input move player object ProcessMovement(keyState, gamePadState); //now we have moved checkbounds if (CheckBounds(map.ObjectGroups["Objects"].Objects["Player"])) { map.ObjectGroups["Objects"].Objects["Player"].X = tempx; map.ObjectGroups["Objects"].Objects["Player"].Y = tempy; } // check for player interaction with coins and exits within the game var p = map.ObjectGroups["Objects"].Objects["Player"]; Rectangle playerRec = new Rectangle(p.X, p.Y, p.Width, p.Height); CheckCoins(playerRec); CheckExits(playerRec); CheckKey(playerRec); CheckEnd(playerRec); CheckDoor(playerRec); // update player object with position and viewport Vector2 Test = (viewportPosition + new Vector2(0, 100) - new Vector2((graphics.PreferredBackBufferWidth / 2), (graphics.PreferredBackBufferHeight / 2))); foreach (var sprite in _sprites) { sprite.Update(gameTime, Position, Test); foreach (Projectile P in sprite.PlayerProjectiles) { Rectangle proj = new Rectangle((int)P.Position.X, (int)P.Position.Y, P.mapobj.Width, P.mapobj.Height); foreach (Enemy E in EnemyList) { Rectangle Erec = new Rectangle((int)E.mapobj.X, (int)E.mapobj.Y, E.mapobj.Width, E.mapobj.Height); if (proj.Intersects(Erec) && P.active) { E.Health -= 1; P.mapobj.Texture = P.Blank; P.active = false; Console.WriteLine("shot: " + proj + " -- " + Erec + " -- " + E.Health); } } } sprite.Position = new Vector2(map.ObjectGroups["Objects"].Objects["Player"].X, map.ObjectGroups["Objects"].Objects["Player"].Y); } // update each enemy int i = 1; foreach (Enemy E in EnemyList) { if (E.Health > 0) { // get position of enemy Vector2 temp = E.Position; // update the enemy to move E.Update(gameTime, Position); foreach (Projectile P in E.PlayerProjectiles) { Rectangle proj = new Rectangle((int)P.Position.X, (int)P.Position.Y, P.mapobj.Width, P.mapobj.Height); Squared.Tiled.Object obj = map.ObjectGroups["Objects"].Objects["Player"]; Rectangle objrec = new Rectangle( obj.X, obj.Y, obj.Width, obj.Height ); if (proj.Intersects(objrec) && P.active) { _sprites[0].Health -= 1; P.mapobj.Texture = P.Blank; P.active = false; } } // check for enemy collision between the bounds and other enemies if (CheckBounds(map.ObjectGroups["Objects"].Objects["Enemy" + i]) || CheckEnemy(map.ObjectGroups["Objects"].Objects["Enemy" + i], E)) { // collision so set position back to temp value E.Position = temp; } else { // no collision so update the enemy position in map map.ObjectGroups["Objects"].Objects["Enemy" + i].X = (int)E.Position.X; map.ObjectGroups["Objects"].Objects["Enemy" + i].Y = (int)E.Position.Y; if (true) { Squared.Tiled.Object tempp = new Squared.Tiled.Object(); tempp.X = (int)E.Position.X; tempp.Y = (int)E.Position.Y; tempp.Width = 30; tempp.Height = 30; tempp.Type = "proj"; tempp.Texture = projTexture; if (E.Fire(E.Position, tempp, blankTexture)) { map.ObjectGroups["Objects"].Objects.Add("proj" + projcount, tempp); projcount++; } } } i++; } } viewportPosition = new Vector2(map.ObjectGroups["Objects"].Objects["Player"].X, map.ObjectGroups["Objects"].Objects["Player"].Y); KeyboardState keys = Keyboard.GetState(); if (_sprites[0].Health <= 0) { currentScreen = Menu.Main; LoadContent(); } // Takes to main menu or inventory if (keys.IsKeyDown(Keys.Tab)) { currentScreen = Menu.Main; } else if (keys.IsKeyDown(Keys.I)) { currentScreen = Menu.Inventory; string temp = "No"; if (key_collected) { temp = "Yes"; } inventory = new SimpleTextUI(this, big, new[] { "Coins: " + coin_collected, "Key: " + temp, "Back" }) { TextColor = Color.Black, SelectedElement = new TextElement(">", Color.White), Align = Alignment.Left }; current = inventory; } } // code for the controlling menu screens else if (currentScreen == Menu.Main) { KeyboardState keys = Keyboard.GetState(); bool change = true; if (!keytimer.Enabled) { if (keys.IsKeyDown(Keys.Up)) { current.Move(Direction.Up); } else if (keys.IsKeyDown(Keys.Down)) { current.Move(Direction.Down); } else if (keys.IsKeyDown(Keys.Left)) { current.Move(Direction.Left); if (current.GetCurrentCaption() == "Video") { graphics.IsFullScreen = (current.GetCurrentValue() == "FullScreen"); graphics.ApplyChanges(); } } else if (keys.IsKeyDown(Keys.Right)) { current.Move(Direction.Right); if (current.GetCurrentCaption() == "Video") { graphics.IsFullScreen = (current.GetCurrentValue() == "FullScreen"); graphics.ApplyChanges(); } } else if (keys.IsKeyDown(Keys.Enter)) { string test = current.GetCurrentCaption(); if (current == menu) { if (test == "Exit") { Exit(); } else if (test == "Options") { current = options; } else if (test == "Continue") { currentScreen = Menu.Play; } else if (test == "Credits") { current = credits; } } else if (current == credits) { if (test == "Back") { current = menu; } } else if (current == options) { if (test == "Back") { current = menu; } } } else { change = false; } if (change) { keytimer = new Timer(); keytimer.Interval = 200; keytimer.Elapsed += new ElapsedEventHandler(OnTimedEvent); keytimer.Enabled = true; } } } else if (currentScreen == Menu.Inventory) { KeyboardState keys = Keyboard.GetState(); bool change = true; if (!keytimer.Enabled) { if (keys.IsKeyDown(Keys.Up)) { current.Move(Direction.Up); } else if (keys.IsKeyDown(Keys.Down)) { current.Move(Direction.Down); } else if (keys.IsKeyDown(Keys.Enter)) { string test = current.GetCurrentCaption(); if (current == inventory) { if (test == "Back") { currentScreen = Menu.Play; } } } } else { change = false; } if (change) { keytimer = new Timer(); keytimer.Interval = 200; keytimer.Elapsed += new ElapsedEventHandler(OnTimedEvent); keytimer.Enabled = true; } } base.Update(gameTime); }
/// <summary> /// LoadContent will be called once per game and is the place to load /// all of your content. /// </summary> protected override void LoadContent() { // Create a new SpriteBatch, which can be used to draw textures. spriteBatch = new SpriteBatch(GraphicsDevice); map1 = Map.Load(Path.Combine(Content.RootDirectory, "SimpleRPG.tmx"), Content); map2 = Map.Load(Path.Combine(Content.RootDirectory, "SimpleRPG.tmx"), Content); map3 = Map.Load(Path.Combine(Content.RootDirectory, "SimpleRPG.tmx"), Content); map4 = Map.Load(Path.Combine(Content.RootDirectory, "SimpleRPG.tmx"), Content); map = map1; collision = map.Layers["Collision"]; tilepixel = map.TileWidth; var animations = new Dictionary <string, Animation>() { { "Player Forward", new Animation(Content.Load <Texture2D>("Player/Player Forward"), 3) }, { "Player Backwards", new Animation(Content.Load <Texture2D>("Player/Player Backwards"), 3) }, { "Player Left", new Animation(Content.Load <Texture2D>("Player/Player Left"), 3) }, { "Player Right", new Animation(Content.Load <Texture2D>("Player/Player Right"), 3) }, }; var animations2 = new Dictionary <string, Animation>() { { "Enemy Forward", new Animation(Content.Load <Texture2D>("Enemy/Enemy Forward"), 3) }, { "Enemy Backwards", new Animation(Content.Load <Texture2D>("Enemy/Enemy Backwards"), 3) }, { "Enemy Left", new Animation(Content.Load <Texture2D>("Enemy/Enemy Left"), 3) }, { "Enemy Right", new Animation(Content.Load <Texture2D>("Enemy/Enemy Right"), 3) }, }; _sprites = new List <Sprite>() { new Sprite(new Dictionary <string, Animation>() { { "Player Forward", new Animation(Content.Load <Texture2D>("Player/Player Forward"), 3) }, { "Player Backwards", new Animation(Content.Load <Texture2D>("Player/Player Backwards"), 3) }, { "Player Left", new Animation(Content.Load <Texture2D>("Player/Player Left"), 3) }, { "Player Right", new Animation(Content.Load <Texture2D>("Player/Player Right"), 3) }, }, true) { Position = new Vector2(100, 100), Input = new Input() { Up = Keys.Up, Down = Keys.Down, Left = Keys.Left, Right = Keys.Right, } }, }; big = Content.Load <SpriteFont>("Big"); small = Content.Load <SpriteFont>("Small"); // Set menus and screens menu = new SimpleTextUI(this, big, new[] { "Continue", "Options", "Credits", "Exit" }) { TextColor = Color.Black, SelectedElement = new TextElement(">", Color.White), Align = Alignment.Left }; options = new SimpleTextUI(this, big, new TextElement[] { new SelectElement("Video", new[] { "FullScreen", "Windowed" }), new NumericElement("Music", 1, 3, 0f, 10f, 1f), new TextElement("Back") }); current = menu; keytimer = new Timer(); inventory = new SimpleTextUI(this, big, new[] { "Coins", "Items", "Keys", "Exit" }) { TextColor = Color.Black, SelectedElement = new TextElement(">", Color.White), Align = Alignment.Left }; EnemyTexture = Content.Load <Texture2D>("coinTexture"); coinTexture = Content.Load <Texture2D>("coinTexture"); blankTexture = Content.Load <Texture2D>("Transparent"); int coinCount = Convert.ToInt32(map.ObjectGroups["Objects"].Properties["Coin_Count"]); for (int i = 1; i <= coinCount; i++) { map.ObjectGroups["Objects"].Objects["Coin" + i].Texture = coinTexture; } int EnemyCount = Convert.ToInt32(map.ObjectGroups["Objects"].Properties["Enemy_Count"]); EnemyList = new List <Enemy>(); for (int i = 1; i <= EnemyCount; i++) { Enemy temp = new Enemy(animations2, true); temp.Position = new Vector2(map.ObjectGroups["Objects"].Objects["Enemy" + i].X, map.ObjectGroups["Objects"].Objects["Enemy" + i].Y); EnemyList.Add(temp); } map = map1; Inventory = new List <Squared.Tiled.Object>(); // TODO: use this.Content to load your game content here }
/// <summary> /// LoadContent will be called once per game and is the place to load /// all of your content. /// </summary> protected override void LoadContent() { // Create a new SpriteBatch, which can be used to draw textures. spriteBatch = new SpriteBatch(GraphicsDevice); // texture for coin ojects, blank once collected coinTexture = Content.Load <Texture2D>("coinTexture"); keyTexture = Content.Load <Texture2D>("key"); projTexture = Content.Load <Texture2D>("Magic"); blankTexture = Content.Load <Texture2D>("Transparent"); menuBackground = Content.Load <Texture2D>("Age Of Heroes Menu"); // set and load maps map1 = Map.Load(Path.Combine(Content.RootDirectory, "SimpleRPG.tmx"), Content); map2 = Map.Load(Path.Combine(Content.RootDirectory, "SimpleRPG.tmx"), Content); map3 = Map.Load(Path.Combine(Content.RootDirectory, "SimpleRPG.tmx"), Content); map4 = Map.Load(Path.Combine(Content.RootDirectory, "SimpleRPG.tmx"), Content); // set first map to map 1 map = map1; // load animations for the player var animations = new Dictionary <string, Animation>() { { "Player Forward", new Animation(Content.Load <Texture2D>("Player/Player Forward"), 3) }, { "Player Backwards", new Animation(Content.Load <Texture2D>("Player/Player Backwards"), 3) }, { "Player Left", new Animation(Content.Load <Texture2D>("Player/Player Left"), 3) }, { "Player Right", new Animation(Content.Load <Texture2D>("Player/Player Right"), 3) }, }; // load animations for the enemy var animations2 = new Dictionary <string, Animation>() { { "Enemy Forward", new Animation(Content.Load <Texture2D>("Enemy/Enemy Forward"), 3) }, { "Enemy Backwards", new Animation(Content.Load <Texture2D>("Enemy/Enemy Backwards"), 3) }, { "Enemy Left", new Animation(Content.Load <Texture2D>("Enemy/Enemy Left"), 3) }, { "Enemy Right", new Animation(Content.Load <Texture2D>("Enemy/Enemy Right"), 3) }, }; // create list of sprites for player _sprites = new List <Sprite>() { new Sprite(animations, true, projTexture, false) { Position = new Vector2(100, 100), Input = new Input() { Up = Keys.Up, Down = Keys.Down, Left = Keys.Left, Right = Keys.Right, } }, }; // load spritefonts to draw text to the screen big = Content.Load <SpriteFont>("Big"); small = Content.Load <SpriteFont>("Small"); // Set menus and screens menu = new SimpleTextUI(this, big, new[] { "Continue", "Options", "Credits", "Exit" }) { TextColor = Color.Black, SelectedElement = new TextElement(">", Color.White), Align = Alignment.LowerLeft }; options = new SimpleTextUI(this, big, new TextElement[] { new SelectElement("Video", new[] { "FullScreen", "Windowed" }), new NumericElement("Music", 1, 3, 0f, 10f, 1f), new TextElement("Back") }); credits = new SimpleTextUI(this, big, new[] { "By The Great", "George", "Barber", "Back" }) { TextColor = Color.Black, SelectedElement = new TextElement(">", Color.White), Align = Alignment.Left }; inventory = new SimpleTextUI(this, big, new[] { "Coins", "Keys", "Back" }) { TextColor = Color.Black, SelectedElement = new TextElement(">", Color.White), Align = Alignment.Left }; // starting menu is menu current = menu; // timer to control input on menu screens keytimer = new Timer(); // timer to control use of exits exittimer = new Timer(); // get coin count int coinCount = Convert.ToInt32(map.ObjectGroups["Objects"].Properties["Coin_Count"]); // for each coin add the texture to map for (int i = 1; i <= coinCount; i++) { map.ObjectGroups["Objects"].Objects["Coin" + i].Texture = coinTexture; } map.ObjectGroups["Objects"].Objects["Key"].Texture = keyTexture; // get enemy count int EnemyCount = Convert.ToInt32(map.ObjectGroups["Objects"].Properties["Enemy_Count"]); // set up list of enemies EnemyList = new List <Enemy>(); // add an enemy to the list for each enemy for (int i = 1; i <= EnemyCount; i++) { //create a temp enemy Enemy temp = new Enemy(animations2, true, map.ObjectGroups["Objects"].Objects["Enemy" + i]); temp.Position = new Vector2(map.ObjectGroups["Objects"].Objects["Enemy" + i].X, map.ObjectGroups["Objects"].Objects["Enemy" + i].Y); // add temp enemy to list EnemyList.Add(temp); } // create inventory list Inventory = new List <Squared.Tiled.Object>(); // set the current map to map1 CurrentMap = map1; // TODO: use this.Content to load your game content here }