/// <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() { // TODO: Add your initialization logic here this.IsMouseVisible = true; this.followCamera = new FollowCamera(GraphicsDevice.Viewport); this.playingState = new PlayingState(); base.Initialize(); }
public static void DrawPlayingStateDisplayEntities(PlayingState playingState, FollowCamera followCamera, SpriteBatch spriteBatch, Texture2D spriteSheet, GraphicsDeviceManager graphicsDevice) { IEnumerable<Guid> drawableEntities = playingState.Entities.Where(x => (x.ComponentFlags & ComponentMasks.Drawable) == ComponentMasks.Drawable).Select(x => x.ID); foreach (Guid id in drawableEntities) { Rectangle source = playingState.DisplayComponents[id].Source; Vector2 origin = new Vector2(source.Width / 2, source.Height / 2); spriteBatch.Draw(spriteSheet, playingState.PositionComponents[id].Position, source, Color.White, playingState.DirectionComponents[id].Direction, origin, 1f, SpriteEffects.None, 0); } }
public void Draw(SpriteBatch spriteBatch, Texture2D spriteSheet, FollowCamera camera) { for (int i = 0; i < TileMap.RowCount; i++) { for (int j = 0; j < TileMap.ColumnCount; j++) { spriteBatch.Draw(spriteSheet, TileMap.Map[i, j].TilePosition, TileMap.Map[i, j].Source, Color.White); //spriteBatch.DrawString(Font, "(" + i + "," + j + ")", TileMap.Map[i, j].TilePosition, Color.White); } } }
public static void HandlePlayerMovement(PlayingState playingState, GraphicsDeviceManager graphics, GameTime gameTime, KeyboardState previousKeyboardState, MouseState previousMouseState, GamePadState previousGamepadState, FollowCamera followCam, TileMap tileMap, LevelCollisionDetection levelCollisionDetection) { List<Guid> moveableEntities = playingState.Entities.Where(x => (x.ComponentFlags & ComponentMasks.PlayerInput) == ComponentMasks.PlayerInput).Select(x => x.ID).ToList(); foreach (Guid id in moveableEntities) { var directionComponent = playingState.DirectionComponents[id]; var positionComponent = playingState.PositionComponents[id]; var aabbComponent = playingState.AABBComponents[id]; Vector2 position = positionComponent.Position; Rectangle boundedBox = aabbComponent.BoundedBox; VelocityComponent velocityComponent = playingState.VelocityComponents[id]; AccelerationComponent accelerationComponent = playingState.AccelerationComponents[id]; #region gamepad controls GamePadState currentGamePadState = GamePad.GetState(PlayerIndex.One); //position.X += currentGamePadState.ThumbSticks.Left.X * playerMoveSpeed; //position.Y -= currentGamePadState.ThumbSticks.Left.Y * playerMoveSpeed; //previousGamepadState = currentGamePadState; #endregion #region keyboard controls KeyboardState CurrentKeyboardState = Keyboard.GetState(); if (CurrentKeyboardState.IsKeyDown(Keys.Left) || CurrentKeyboardState.IsKeyDown(Keys.A) || currentGamePadState.DPad.Left == ButtonState.Pressed) { velocityComponent.xVelocity -= accelerationComponent.xAcceleration; velocityComponent.xVelocity = MathHelper.Clamp(velocityComponent.xVelocity, -1 * velocityComponent.xTerminalVelocity, 0); boundedBox.X += (int)velocityComponent.xVelocity; boundedBox = levelCollisionDetection.CheckWallCollision(boundedBox, Direction.Left); } if (CurrentKeyboardState.IsKeyDown(Keys.Right) || CurrentKeyboardState.IsKeyDown(Keys.D) || currentGamePadState.DPad.Right == ButtonState.Pressed) { velocityComponent.xVelocity += accelerationComponent.xAcceleration; velocityComponent.xVelocity = MathHelper.Clamp(velocityComponent.xVelocity, 0, velocityComponent.xTerminalVelocity); boundedBox.X += (int)velocityComponent.xVelocity; boundedBox = levelCollisionDetection.CheckWallCollision(boundedBox, Direction.Right); } //move player x axis if (CurrentKeyboardState.IsKeyDown(Keys.Up) || CurrentKeyboardState.IsKeyDown(Keys.W) || currentGamePadState.DPad.Up == ButtonState.Pressed) { velocityComponent.yVelocity -= accelerationComponent.yAcceleration; velocityComponent.yVelocity = MathHelper.Clamp(velocityComponent.yVelocity, -1 * velocityComponent.yTerminalVelocity, 0 ); boundedBox.Y += (int)velocityComponent.yVelocity; boundedBox = levelCollisionDetection.CheckWallCollision(boundedBox, Direction.Up); } if (CurrentKeyboardState.IsKeyDown(Keys.Down) || CurrentKeyboardState.IsKeyDown(Keys.S) || currentGamePadState.DPad.Down == ButtonState.Pressed) { velocityComponent.yVelocity += accelerationComponent.yAcceleration; velocityComponent.yVelocity = MathHelper.Clamp(velocityComponent.yVelocity, 0, velocityComponent.yTerminalVelocity); position.Y += velocityComponent.yVelocity; boundedBox.Y += (int)velocityComponent.yVelocity; boundedBox = levelCollisionDetection.CheckWallCollision(boundedBox, Direction.Down); } if (CurrentKeyboardState.IsKeyUp(Keys.Down) && CurrentKeyboardState.IsKeyUp(Keys.S) && CurrentKeyboardState.IsKeyUp(Keys.Up) && CurrentKeyboardState.IsKeyUp(Keys.W)) { velocityComponent.yVelocity = 0; } if (CurrentKeyboardState.IsKeyUp(Keys.Left) && CurrentKeyboardState.IsKeyUp(Keys.A) && CurrentKeyboardState.IsKeyUp(Keys.D) && CurrentKeyboardState.IsKeyUp(Keys.Right)) { velocityComponent.xVelocity = 0; } previousKeyboardState = CurrentKeyboardState; #endregion #region mouse controls //face player towards mouse MouseState currentMouseState = Mouse.GetState(); position = new Vector2(boundedBox.X + (boundedBox.Width / 2), boundedBox.Y + (boundedBox.Height / 2)); Vector2 mouseLocation = new Vector2(currentMouseState.X + followCam.Center.X, currentMouseState.Y + followCam.Center.Y); Vector2 direction = mouseLocation - position; direction.Normalize(); directionComponent.Direction = (float)Math.Atan2((double)direction.Y, (double)direction.X); //if (currentMouseState.LeftButton == ButtonState.Pressed) //{ // List<Guid> weaponEntities = playingState.Entities.Where(x => (x.ComponentFlags & ComponentMasks.Weapon) == ComponentMasks.Weapon).Select(x => x.ID).ToList(); // foreach (Guid wid in weaponEntities) // { // if (playingState.OwnerComponents.ContainsKey(wid)) // { // OwnerComponent ownerComponent = playingState.OwnerComponents[wid]; // if (ownerComponent.OwnerID == id) // { // Guid projectileId = playingState.CreateEntity(); // playingState.Entities.Where(x => x.ID == projectileId).First().ComponentFlags = ComponentMasks.Projectile; // playingState.DirectionComponents[projectileId] = new DirectionComponent() { Direction = directionComponent.Direction }; // playingState.DisplayComponents[projectileId] = new DisplayComponent() { Source = new Rectangle(21, 504, destination.Width, destination.Height) }; // playingState.SpeedComponents[projectileId] = new SpeedComponent() { Speed = 15f }; // playingState.PositionComponents[projectileId] = new PositionComponent() { Position = position, Destination = new Rectangle((int)position.X, (int)position.Y, destination.Width, destination.Height) }; // playingState.DamageComponents[projectileId] = new DamageComponent() { Damage = 10 }; // playingState.OwnerComponents[projectileId] = new OwnerComponent() { OwnerID = wid }; // } // } // } //} previousMouseState = currentMouseState; #endregion playingState.DirectionComponents[id] = directionComponent; positionComponent.Position = position; aabbComponent.BoundedBox = boundedBox; playingState.PositionComponents[id] = positionComponent; playingState.AABBComponents[id] = aabbComponent; playingState.VelocityComponents[id] = velocityComponent; playingState.AccelerationComponents[id] = accelerationComponent; } }