/// <summary> /// A Player without network component /// </summary> /// <param name="model"></param> /// <param name="gamePadIndex"></param> /// <param name="transformPos"></param> /// <param name="cameraPos"></param> /// <param name="cameraAspectRatio"></param> /// <param name="followPlayer"></param> /// <param name="texture"></param> /// <returns></returns> public static Entity NewLocalPlayer(String model, int gamePadIndex, Vector3 transformPos, Vector3 cameraPos, float cameraAspectRatio, bool followPlayer, Texture2D texture) { Entity player = NewBasePlayer(model, gamePadIndex, transformPos, texture, LOCAL_PLAYER); CameraComponent cameraComponent = new CameraComponent(player, cameraPos, cameraAspectRatio, followPlayer); KeyboardComponent keyboardComponent = new KeyboardComponent(player); GamePadComponent gamePadComponent = new GamePadComponent(player, gamePadIndex); FPSComponent fpsComponent = new FPSComponent(player); UIComponent spriteFPSCounterComponent = new UIComponent(player) { Position = new Vector2(10, 10), Text = fpsComponent.CurrentFramesPerSecond.ToString(), Color = Color.White, SpriteFont = AssetManager.Instance.GetContent <SpriteFont>("menu") }; NetworkDiagnosticComponent networkDiagnosticComponent = new NetworkDiagnosticComponent(player); ComponentManager.Instance.AddComponentToEntity(player, cameraComponent); ComponentManager.Instance.AddComponentToEntity(player, keyboardComponent); ComponentManager.Instance.AddComponentToEntity(player, gamePadComponent); ComponentManager.Instance.AddComponentToEntity(player, fpsComponent); ComponentManager.Instance.AddComponentToEntity(player, networkDiagnosticComponent); ComponentManager.Instance.AddComponentToEntity(player, spriteFPSCounterComponent); return(player); }
/// <summary> /// UpdateActionStates loops thru gamepadActions for a specific gamepadComponenet and applies appropriet action relative to buttonstate. /// </summary> /// <param name="gamepadComponent"></param> public void UpdateActionStates(GamePadComponent gamepadComponent) { foreach (ActionsEnum action in gamepadComponent.gamepadActions.Keys) { Buttons button = gamepadComponent.gamepadActions[action]; bool presentState = curState[(int)gamepadComponent.playerIndex].IsButtonDown(button); bool oldState = curState[(int)gamepadComponent.playerIndex].IsButtonDown(button); if (presentState && oldState) { gamepadComponent.gamepadStates[action] = ButtonStates.Pressed; } else if (presentState && !oldState) { gamepadComponent.gamepadStates[action] = ButtonStates.Hold; } else if (!presentState && oldState) { gamepadComponent.gamepadStates[action] = ButtonStates.Released; } else { gamepadComponent.gamepadStates[action] = ButtonStates.Not_Pressed; } } }
/// <summary> /// /// </summary> /// <param name="Id"></param> /// <param name="gameTime"></param> private void GamePadeMove(int Id, GameTime gameTime) { GamePadComponent kbc = ComponentManager.Instance.GetEntityComponent <GamePadComponent>(Id); PositionComponent p = ComponentManager.Instance.GetEntityComponent <PositionComponent>(Id); PlayerComponent pc = ComponentManager.Instance.GetEntityComponent <PlayerComponent>(Id); VelocityComponent v = ComponentManager.Instance.GetEntityComponent <VelocityComponent>(Id); DirectionComponent dc = ComponentManager.Instance.GetEntityComponent <DirectionComponent>(Id); JumpComponent jump = ComponentManager.Instance.GetEntityComponent <JumpComponent>(Id); p.prevPosition = p.position; if (dc != null && v != null) { v.velocity.X = sideMovement * (int)dc.directio; } if (p != null && v != null && kbc != null && jump != null && dc != null) { if (kbc.gamepadStates[ActionsEnum.Jump] == ButtonStates.Pressed && !pc.isFalling) { if (dc.directio == Direction.Still) { dc.directio = dc.preDir; } if (v.velocity.Y > -jump.maxJumpHeight) { v.velocity.Y -= jump.jumpHeight; ComponentManager.Instance.AddComponentToEntity(Id, new SoundEffectComponent("jump")); } } v.velocity.Y += gravity * (float)gameTime.ElapsedGameTime.TotalSeconds; p.position += v.velocity * (float)gameTime.ElapsedGameTime.TotalSeconds; } }
private void UpdateActionStates(GamePadComponent gamePadComp) { foreach (string key in gamePadComp.Actions.Keys) { foreach (Buttons button in gamePadComp.Actions[key]) { bool newState = gamePadComp.NewState.IsButtonDown(button); bool oldState = gamePadComp.OldState.IsButtonDown(button); if (newState && !oldState) { gamePadComp.ActionStates[key] = BUTTON_STATE.PRESSED; break; } else if (newState && oldState) { gamePadComp.ActionStates[key] = BUTTON_STATE.HELD; break; } else if (!newState && oldState) { gamePadComp.ActionStates[key] = BUTTON_STATE.RELEASED; break; } else { gamePadComp.ActionStates[key] = BUTTON_STATE.NOT_PRESSED; } } } }
/// <summary> /// Creates an new Player with Controlls /// </summary> /// <param name="pixlePer"> True if pixelPerfect shall be used </param> /// <param name="GamePade"> True if GamePad the player uses a gamepad </param> /// <param name="PadJump"> Key binding to gamePad </param> /// <param name="Jump"> key binding to keybord </param> /// <param name="position"> Player start Position </param> /// <param name="name"> The name off the player</param> /// <param name="dir"> The players starting direction</param> /// <param name="index"> Playerindex For GamePad </param> /// <returns></returns> public int CreatePlayer(bool pixlePer, bool GamePade, Buttons PadJump, Keys Jump, Vector2 position, string name, Direction dir, PlayerIndex index, Color colour) { SpriteEffects flip; GamePadComponent gam; KeyBoardComponent kcb; int id = ComponentManager.Instance.CreateID(); if (dir == Direction.Left) { flip = SpriteEffects.FlipHorizontally; } else { flip = SpriteEffects.None; } if (GamePade == true) { gam = new GamePadComponent(index); gam.gamepadActions.Add(ActionsEnum.Jump, PadJump); ComponentManager.Instance.AddComponentToEntity(id, gam); } else { kcb = new KeyBoardComponent(); kcb.keyBoardActions.Add(ActionsEnum.Jump, Jump); ComponentManager.Instance.AddComponentToEntity(id, kcb); } DirectionComponent dc = new DirectionComponent(dir); DrawableComponent comp = new DrawableComponent(Game.Instance.GetContent <Texture2D>("Pic/kanin1"), flip); PositionComponent pos = new PositionComponent(position); VelocityComponent vel = new VelocityComponent(new Vector2(200F, 0), 50F); JumpComponent jump = new JumpComponent(300F, 200F); CollisionRectangleComponent CRC = new CollisionRectangleComponent(new Rectangle((int)pos.position.X, (int)pos.position.Y, comp.texture.Width, comp.texture.Height)); CollisionComponent CC = new CollisionComponent(pixlePer); PlayerComponent pc = new PlayerComponent(name); DrawableTextComponent dtc = new DrawableTextComponent(name, Color.Black, Game.Instance.GetContent <SpriteFont>("Fonts/TestFont")); HUDComponent hudc = new HUDComponent(Game.Instance.GetContent <Texture2D>("Pic/PowerUp"), new Vector2(pos.position.X, pos.position.Y)); HUDComponent hudc2 = new HUDComponent(Game.Instance.GetContent <Texture2D>("Pic/PowerUp"), Vector2.One); HealthComponent hc = new HealthComponent(3, false); //AnimationComponent ani = new AnimationComponent(100, 114, comp.texture.Width, comp.texture.Height, 0.2); comp.colour = colour; ComponentManager.Instance.AddComponentToEntity(id, vel); ComponentManager.Instance.AddComponentToEntity(id, comp); ComponentManager.Instance.AddComponentToEntity(id, pos); ComponentManager.Instance.AddComponentToEntity(id, CRC); ComponentManager.Instance.AddComponentToEntity(id, CC); ComponentManager.Instance.AddComponentToEntity(id, pc); ComponentManager.Instance.AddComponentToEntity(id, dtc); ComponentManager.Instance.AddComponentToEntity(id, hudc); ComponentManager.Instance.AddComponentToEntity(id, hc); //ComponentManager.Instance.AddComponentToEntity(id, ani); ComponentManager.Instance.AddComponentToEntity(id, dc); ComponentManager.Instance.AddComponentToEntity(id, jump); return(id); }
/// <summary> /// update gets all gamepadComponent and loops thru all relvant components which is passed to UpdateActionStates. /// </summary> /// <param name="gameTime">Monogame specific variable for game time</param> public void update(GameTime gameTime) { updateStates(); List <int> entities = ComponentManager.Instance.GetAllEntitiesWithComponentType <GamePadComponent>(); if (entities != null) { foreach (var item in entities) { GamePadComponent gamepadComponent = ComponentManager.Instance.GetEntityComponent <GamePadComponent>(item); UpdateActionStates(gamepadComponent); } } }
internal static Buttons Component2Button(GamePadComponent com) { Buttons but = 0; //Redo so multiple GamePadComponents can be checked at the same time. switch (com) { case GamePadComponent.A: but = Buttons.A; break; case GamePadComponent.B: but = Buttons.B; break; case GamePadComponent.X: but = Buttons.X; break; case GamePadComponent.Y: but = Buttons.Y; break; case GamePadComponent.Back: but = Buttons.Back; break; case GamePadComponent.Start: but = Buttons.Start; break; case GamePadComponent.BigButton: but = Buttons.BigButton; break; case GamePadComponent.LeftShoulder: but = Buttons.LeftShoulder; break; case GamePadComponent.LeftStick: but = Buttons.LeftStick; break; case GamePadComponent.RightShoulder: but = Buttons.RightShoulder; break; case GamePadComponent.RightStick: but = Buttons.RightStick; break; case GamePadComponent.DPadUp: but = Buttons.DPadUp; break; case GamePadComponent.DPadRight: but = Buttons.DPadRight; break; case GamePadComponent.DPadDown: but = Buttons.DPadDown; break; case GamePadComponent.DPadLeft: but = Buttons.DPadLeft; break; } return but; }
/// <summary> /// Determine if the specified button is pressed. /// </summary> /// <param name="com">The button to check if pressed. Supports A, B, X, Y, Back, Start, BigButton, LeftShoulder, LeftStick, RightShoulder, RightStick, and the D-Pad.</param> /// <returns><code>true</code> if the button was pressed, <code>false</code> if otherwise.</returns> public bool IsButtonPressed(GamePadComponent com) { Buttons but = Component2Button(com); if (but == 0) { return false; } return this.manager.c_pad_states[(int)index].IsButtonDown(but); }
/// <summary> /// Get the difference of the trigger between the current and previous state. /// </summary> /// <param name="com">Which trigger to get, only support LeftTrigger, and RightTrigger.</param> /// <returns>The difference between the current and past state as a scalar value between 0 and 1.</returns> public float GetTriggerDifference(GamePadComponent com) { GamePadTriggers ctrigger = GetTriggers((int)index, true); GamePadTriggers otrigger = GetTriggers((int)index, false); switch (com) { case GamePadComponent.LeftStick: return ctrigger.Left - otrigger.Left; case GamePadComponent.RightStick: return ctrigger.Right - otrigger.Right; } return 0; }
/// <summary> /// Get the trigger position. /// </summary> /// <param name="com">Which trigger to get, only support LeftTrigger, and RightTrigger.</param> /// <returns>A scalar value from 0 to 1 defining the trigger position where 0 is a completely unpressed trigger.</returns> public float GetTrigger(GamePadComponent com) { GamePadTriggers trigger = GetTriggers((int)index, true); switch (com) { case GamePadComponent.LeftTrigger: return trigger.Left; case GamePadComponent.RightTrigger: return trigger.Right; } return 0; }
/// <summary> /// Get the difference of the thumbstick between the current and previous state. /// </summary> /// <param name="com">Which thumbstick to get, only support LeftStick, and RightStick.</param> /// <returns>The difference between the current and past state as a 2D vector.</returns> public Vector2 GetThumbstickPositionDifference(GamePadComponent com) { GamePadThumbSticks cstick = GetThumbStick((int)index, true); GamePadThumbSticks ostick = GetThumbStick((int)index, true); switch (com) { case GamePadComponent.LeftStick: return cstick.Left - ostick.Left; case GamePadComponent.RightStick: return cstick.Right - ostick.Right; } return Vector2.Zero; }
/// <summary> /// Get the past position of the thumbstick control as a 2D vector. /// </summary> /// <param name="com">Which thumbstick to get, only support LeftStick, and RightStick.</param> /// <returns>A 2D vector defining the thumbstick position from the previous state.</returns> public Vector2 GetPastThumbstickPosition(GamePadComponent com) { GamePadThumbSticks stick = GetThumbStick((int)index, false); switch (com) { case GamePadComponent.LeftStick: return stick.Left; case GamePadComponent.RightStick: return stick.Right; } return Vector2.Zero; }
private void UpdateStates(GamePadComponent gamePadComp) { gamePadComp.OldState = gamePadComp.NewState; gamePadComp.NewState = GamePad.GetState(gamePadComp.PlayerIndex); }
/* * Gets player keyboard input and takes appropriate action. */ public void ParsePlayerInput(GameTime gameTime, Entity playerEntity) { VelocityComponent velocityComponent = componentManager.ConcurrentGetComponentOfEntity <VelocityComponent>(playerEntity); KeyboardComponent keyboardComponent = componentManager.ConcurrentGetComponentOfEntity <KeyboardComponent>(playerEntity); GamePadComponent gamePadComponent = componentManager.ConcurrentGetComponentOfEntity <GamePadComponent>(playerEntity); GravityComponent gravityComponent = componentManager.ConcurrentGetComponentOfEntity <GravityComponent>(playerEntity); /* Keyboard actions */ if (keyboardComponent != null && velocityComponent != null) { KeyboardState state = Keyboard.GetState(); if (state.IsKeyDown(Keys.Up) && !state.IsKeyDown(Keys.Down)) { PlayerActions.AcceleratePlayerForwards(gameTime, velocityComponent); } if (state.IsKeyDown(Keys.Down) && !state.IsKeyDown(Keys.Up)) { PlayerActions.AcceleratePlayerBackwards(gameTime, velocityComponent); } if (state.IsKeyDown(Keys.Left) && !state.IsKeyDown(Keys.Right)) { PlayerActions.AcceleratePlayerLeftwards(gameTime, velocityComponent); } if (state.IsKeyDown(Keys.Right) && !state.IsKeyDown(Keys.Left)) { PlayerActions.AcceleratePlayerRightwards(gameTime, velocityComponent); } if (state.IsKeyDown(Keys.Space)) { if (!gravityComponent.HasJumped) { PlayerActions.PlayerJump(gameTime, velocityComponent, playerEntity); gravityComponent.HasJumped = true; } } } #region /* Gamepad actions */ if (gamePadComponent != null && velocityComponent != null) { GamePadState state = GamePad.GetState(gamePadComponent.Index); if (state.IsButtonDown(Buttons.A) && !state.IsButtonDown(Buttons.B)) { PlayerActions.AcceleratePlayerForwards(gameTime, velocityComponent); } if (state.IsButtonDown(Buttons.B) && !state.IsButtonDown(Buttons.A)) { PlayerActions.AcceleratePlayerBackwards(gameTime, velocityComponent); } if (state.IsButtonDown(Buttons.LeftThumbstickLeft) && !state.IsButtonDown(Buttons.LeftThumbstickRight)) { PlayerActions.AcceleratePlayerLeftwards(gameTime, velocityComponent); } if (state.IsButtonDown(Buttons.LeftThumbstickRight) && !state.IsButtonDown(Buttons.LeftThumbstickLeft)) { PlayerActions.AcceleratePlayerRightwards(gameTime, velocityComponent); } if (state.IsButtonDown(Buttons.Y) && !state.IsButtonDown(Buttons.A)) { if (!gravityComponent.HasJumped) { PlayerActions.PlayerJump(gameTime, velocityComponent, playerEntity); gravityComponent.HasJumped = true; } } } #endregion }