/// <summary> /// The parameterized constructor for the player class /// </summary> /// <param name="pTexture">The texture that the player will use</param> /// <param name="g">The GraphicsDeviceManager that will be used</param> public Player(Texture2D pTexture, GraphicsDeviceManager gdm, World w, Texture2D fTexture) { //Load the texture and set the vectors graphic = gdm; screenWidth = gdm.PreferredBackBufferWidth; screenHeight = gdm.PreferredBackBufferHeight; position = new Vector2(150, 50); body = new Rectangle((int)position.X, (int)position.Y, 50, 50); //TODO: add content manager burning = w.game.Content.Load<SoundEffect>("Audio/WAVs/fire"); burning2 = burning.CreateInstance(); texture = pTexture; flameTexture = fTexture; velocity = new Vector2(0, 0); world = w; world.Player = this; //Set the player state hState = HorizontalState.standing; //Make an array of three torches torches = new Torch[999]; for (int i = 0; i < torches.Length; i++) { torches[i] = new Torch(this, fTexture, w); } }
void changeDirection() { if (inputManager.playerInputs.StartRun) { _eventBroker.emitDirectionChanged(_movement.getChosenOption() - 1); InitializeRun(); currentHState = HorizontalState.running; } if (inputManager.playerInputs.MoveSide < 0.0f) { if (_movement.getChosenOption() - 1 >= 0) { _movement.setChosenOption(_movement.getChosenOption() - 1); currentHState = HorizontalState.changingOrientation; } } if (inputManager.playerInputs.MoveSide > 0.0f) { if (_movement.getChosenOption() + 1 < _movement.getOptionsLength()) { _movement.setChosenOption(_movement.getChosenOption() + 1); currentHState = HorizontalState.changingOrientation; } } }
// --------------------------------- // HORIZONTAL STATE MACHINE METHODS // --------------------------------- void checkSwitchLanes() { /*if (Input.GetKey(KeyCode.Q)) * { * _movement.FastSwitchLeft(); * currentHState = HorizontalState.switchingLane; * //movement = new Vector3(switchDir*switchingVelocity, 0.0f, forwardVelocity); * } * else if (Input.GetKey(KeyCode.E)) * { * _movement.FastSwitchRight(); * currentHState = HorizontalState.switchingLane; * //movement = new Vector3(switchDir*switchingVelocity, 0.0f, forwardVelocity); * } * else * {*/ int switchDir = 0; if (currentIState.Right) { switchDir = 1; } if (currentIState.Left) { switchDir = -1; } bool switching = _movement.SwitchLane(switchDir); if (switching) { currentHState = HorizontalState.switchingLane; } //} }
public void RightAction() { HorizontalState.RightAction(this); if (VerticalState is CrouchingAvatar) { VerticalState = new IdleAvatar(); } }
private void SetStateH(HorizontalState s) { if (prevStateH != curStateH) { walkDown.resetEnd(); } prevStateH = curStateH; curStateH = s; }
// Use this for initialization void Start() { InitializeRun(); currentVState = VerticalState.running; //Initially running currentHState = HorizontalState.running; //currentVIState = InputState.None; //currentHIState = InputState.None; targetLane = _movement.currentLane; StartCoroutine(queueEvents()); }
void SwitchingLanes() { _movement.SetSwitchingMovement(); // If the player has arrived to the target lane if (_movement.CheckIfMovementFinishing()) { checkSwitchLanes(); //Input buffering if (_movement.CheckIfMovementFinishing()) { currentHState = HorizontalState.running; _movement.SetForwardMovement(); } } }
/// <summary> /// The parameterized constructor for the player class /// </summary> /// <param name="pTexture">The texture that the player will use</param> /// <param name="g">The GraphicsDeviceManager that will be used</param> public Player(Texture2D pTexture, GraphicsDeviceManager gdm, World w) { //Load the texture and set the vectors graphic = gdm; screenWidth = gdm.PreferredBackBufferWidth; screenHeight = gdm.PreferredBackBufferHeight; position = new Vector2(screenWidth / 2, screenHeight / 2); body = new Rectangle((int)position.X, (int)position.Y, screenWidth, screenHeight); texture = pTexture; velocity = new Vector2(0, 0); world = w; //Set the player state hState = HorizontalState.standing; }
public void FacePlayer(HorizontalState playerH, VerticalState playerV) { moveTimer = 0; if (playerH == HorizontalState.Left) //face right { Console.WriteLine("left"); offsetX = 9; flipped = false; } else if (playerH == HorizontalState.Right) //face left { Console.WriteLine("right"); flipped = true; offsetX = 9; } else { Console.WriteLine("hNone"); offsetX = 0; } if (playerV == VerticalState.Down) //face up { Console.WriteLine("down"); offsetY = 54; } else if (playerV == VerticalState.Up) //face down { Console.WriteLine("up"); offsetY = 0; } else { Console.WriteLine("vNone"); offsetY = 27; } offsetY += 27 * 3; Console.WriteLine("OffsetX: " + offsetX); Console.WriteLine("OffsetY: " + offsetY); }
public Player(float x, float y) { Rectangle src = Sprites.GetSlice(Element.Player); Position = new Vector2(x, y); Velocity = Vector2.Zero; Dst = new Rectangle((int)x, (int)y, src.Width, src.Height); ElementType = Element.Player; Visible = true; XState = HorizontalState.Stopped; YState = VerticalState.Falling; HState = HealthState.Normal; PlayerAnimation = new Animation( TimeSpan.FromMilliseconds(200f), src.Width, src.Height, Sprites.GetTexture(Element.Player).Width / src.Width, true); Direction = 1; }
/// <summary> /// The parameterized constructor for the player class /// </summary> /// <param name="pTexture">The texture that the player will use</param> /// <param name="g">The GraphicsDeviceManager that will be used</param> public Player(Texture2D pTexture, GraphicsDeviceManager gdm, World w, Texture2D fTexture) { //Load the texture and set the vectors graphic = gdm; screenWidth = gdm.PreferredBackBufferWidth; screenHeight = gdm.PreferredBackBufferHeight; position = new Vector2(150, 50); body = new Rectangle((int)position.X, (int)position.Y, 50, 50); texture = pTexture; flameTexture = fTexture; velocity = new Vector2(0, 0); world = w; world.Player = this; //Set the player state hState = HorizontalState.standing; //Make an array of three torches torches = new Torch[999]; for (int i = 0; i < torches.Length; i++) { torches[i] = new Torch(this, fTexture, w); } }
// --------------------------------- // HORIZONTAL STATE MACHINE METHODS // --------------------------------- void checkSwitchLanes() { if (inputManager.playerInputs.FastMoveLeft) { _movement.FastSwitchLeft(); currentHState = HorizontalState.switchingLane; //movement = new Vector3(switchDir*switchingVelocity, 0.0f, forwardVelocity); } else if (inputManager.playerInputs.FastMoveRight) { _movement.FastSwitchRight(); currentHState = HorizontalState.switchingLane; //movement = new Vector3(switchDir*switchingVelocity, 0.0f, forwardVelocity); } else { bool switching = _movement.SwitchLane(inputManager.playerInputs.MoveSide); if (switching) { currentHState = HorizontalState.switchingLane; } } }
internal void Push() { XState = HorizontalState.Pushing; }
// Avoid weird movements in the event of fps loss void FixedUpdate() { // VERTICAL STATE MACHINE if (currentVState != VerticalState.dead && currentHState != HorizontalState.changingDirection && currentHState != HorizontalState.changingOrientation && currentHState != HorizontalState.slowingDown) { switch (currentVState) { case VerticalState.running: if (inputManager.playerInputs.Jump) { currentVState = VerticalState.jumping; _movement.Jump(); break; } if (inputManager.playerInputs.Slide) { currentVState = VerticalState.sliding; _movement.Slide(); break; } break; case VerticalState.jumping: bool jumpFinished = _movement.Jumping(); if (jumpFinished) { currentVState = VerticalState.running; } break; case VerticalState.sliding: if (inputManager.playerInputs.Jump) { currentVState = VerticalState.springing; _movement.SpringMovement(); } else // Maybe erase this else if things start to go bad { bool slideFinishing = _movement.Sliding(inputManager.playerInputs.Slide); if (slideFinishing) { currentVState = VerticalState.finishSliding; } } break; case VerticalState.springing: bool springFinished = _movement.FinishSliding(); if (springFinished) { currentVState = VerticalState.jumping; } break; case VerticalState.finishSliding: if (inputManager.playerInputs.Jump) { currentVState = VerticalState.springing; _movement.SpringMovement(); } else { bool isFinished = _movement.FinishSliding(); if (isFinished) { currentVState = VerticalState.running; } } break; default: break; } } // HORIZONTAL STATE MACHINE if (currentHState != HorizontalState.dead) { switch (currentHState) { case HorizontalState.running: _movement.CorrectPosition(); if (_movement.CheckIfCrossRoads()) { currentHState = HorizontalState.slowingDown; } else if (_movement.MoveToCenter()) { currentHState = HorizontalState.movingToCenter; } checkSwitchLanes(); _movement.SetForwardSpeed(inputManager.playerInputs.ChangeSpeed); _movement.SetForwardMovement(); break; case HorizontalState.movingToCenter: bool finishedMovingToCenter = _movement.MovingToCenter(); if (finishedMovingToCenter) { currentHState = HorizontalState.running; } break; case HorizontalState.slowingDown: bool finishedSlowDown = _movement.SlowDown(); if (finishedSlowDown) { _eventBroker.emitChoosingLane(); currentHState = HorizontalState.changingDirection; } break; case HorizontalState.changingDirection: changeDirection(); break; case HorizontalState.changingOrientation: bool finishedChange = _movement.changeOrientation(); if (finishedChange) { currentHState = HorizontalState.changingDirection; } break; case HorizontalState.switchingLane: if (_movement.CheckIfCrossRoads()) { currentHState = HorizontalState.slowingDown; } SwitchingLanes(); break; default: break; } //Move player _movement.Move(); } }
// Use this for initialization void Start() { InitializeRun(); currentVState = VerticalState.running; //Initially running currentHState = HorizontalState.running; }
public override void Death() { currentHState = HorizontalState.dead; currentVState = VerticalState.dead; }
internal void Release(Tile tileToPush) { XState = HorizontalState.Stopped; }
/// <summary> /// This method updates the state of the player based on position and keys pressed. /// </summary> /// <param name="kbState">This is the current KeyBoardState</param> /// <param name="prevState">This is the previouse KeyBoardState</param> public void move(KeyboardState kbState, KeyboardState prevState) { //Print out debugging info //Console.WriteLine("VSTATE: " + vState); //Console.WriteLine("HSTATE: " + hState); //Console.WriteLine("POSITION: " + " ( " + position.X + ", " + position.Y + " ) "); //Console.WriteLine("VELOCITY: " + " ( " + velocity.X + ", " + velocity.Y + " ) "); //Set the x velocity to 0 automatically and set the horizontal state to standing by default velocity.X = 0; hState = HorizontalState.standing; //Check key presses to move player if (kbState.IsKeyDown(Keys.Left) && kbState.IsKeyUp(Keys.Right)) { //Move left as long as there is room if (position.X > 0 + xSpeed) { hState = HorizontalState.walkingLeft; //Move the player to the left velocity.X = -1 * xSpeed; } } if (kbState.IsKeyDown(Keys.Right) && kbState.IsKeyUp(Keys.Left)) { //Move right as long as there is room if (position.X < ((float)screenWidth - xSpeed) / 2 - texture.Width / 2) { hState = HorizontalState.walkingRight; //Move the player to the right velocity.X = xSpeed; } else { world.movePlatforms((int)xSpeed); for (int i = 0; i < torches.Length; i++) { if (torches[i].Falling == false) { torches[i].Location = new Vector2(torches[i].Location.X - xSpeed, torches[i].Location.Y); } } hState = HorizontalState.walkingRight; } } // Jumping/Falling if (kbState.IsKeyDown(Keys.Up)) // If the player is currently pressing up. { if (vState == VerticalState.none || vState == VerticalState.fallingPlatform) // If the player's veritcal state is none (not falling or jumping) or the player is on a Falling Platform. { currentPlatform = null; vState = VerticalState.jumping; velocity.Y = -2 * ySpeed; } } else if (vState == VerticalState.jumping) // If the player is currently not pressing up and is jumping. { // Stop the jump. vState = VerticalState.falling; velocity.Y = -0.50f * velocity.Y; } //Check for collision detection if (vState == VerticalState.falling) { foreach (Platform p in world.getPlatforms()) { if (body.Intersects(p.Bounds)) { // && (position.X > p.Bounds.Left - 50 *.75) && (position.X < p.Bounds.Right - 50 * .75) if (position.Y + 50 < p.Bounds.Top + 18) { position.Y = p.Bounds.Top - 49; vState = VerticalState.none; velocity.Y = 0; currentPlatform = p; } } } } if (vState == VerticalState.none) { bool isColliding = false; foreach (Platform p in world.getPlatforms()) { if (body.Intersects(p.Bounds)) { isColliding = true; } } if (!isColliding) { vState = VerticalState.falling; currentPlatform = null; } } if (vState == VerticalState.fallingPlatform) // Check to see if the player is still on the platform horizontally. { if (position.X + 50 < currentPlatform.Bounds.Left || position.X > currentPlatform.Bounds.Right) { vState = VerticalState.falling; currentPlatform = null; } } if (position.Y >= screenHeight - 50) // If the player is at the bottom of the screen. { if (vState == VerticalState.falling || vState == VerticalState.fallingPlatform) // If the player's vertical state is falling or fallingPlatform. { position.Y = screenHeight - 50; vState = VerticalState.none; velocity.Y = 0; } } //Add our velocity to our position vector position.X += velocity.X; position.Y += velocity.Y; body.X = (int)position.X; body.Y = (int)position.Y; //Check to see if player is throwing torches throwTorches(kbState, prevState); //Update all torches, if any for (int i = 0; i < torches.Length; i++) { torches[i].update(); } }
public NPC(World world, ContentManager content, Player player, int steps, bool vertical, int posX, int posY, KeyboardState prevState, string[] messages, int textWidth = 26, int textHeight = 3, int width = 17, int height = 27) { this.width = width; this.height = height; if (vertical) { offsetX = 0; offsetY = 0; } else { offsetX = 9; //multiplied in draw by width offsetY = 27; } if (vertical) { curStateH = HorizontalState.None; } else { curStateV = VerticalState.None; } this.textWidth = textWidth; this.textHeight = textHeight; this.messages = messages; speaking = false; backwards = false; curStep = 1; this.steps = steps; this.vertical = vertical; animSpeed = 0.25f; isMoving = false; this.player = player; flipped = false; //body = new Body(world, new Vector2(0, 0)); bodyWidth = ConvertUnits.ToSimUnits(width); bodyHeight = ConvertUnits.ToSimUnits(height / 2); body = BodyFactory.CreateRectangle(world, bodyWidth, bodyHeight, 0.1f); body.UserData = this; //body = BodyFactory.CreateRectangle(world, 10, 10, 1, new Vector2(0,0)); //body.Position = new Vector2(8 * 31, 23*16); body.BodyType = BodyType.Kinematic; body.Position = ConvertUnits.ToSimUnits(posX * 16, posY * 16); body.OnCollision += OnCollisionHandler; //body.OnSeparation += EndContactHandler; leftFixt = FixtureFactory.AttachEdge(new Vector2(-bodyWidth / 2 - 0.02f, -bodyHeight / 2 + 0.01f), new Vector2(-bodyWidth / 2 - 0.02f, bodyHeight / 2 - 0.01f), body); leftFixt.IsSensor = true; leftFixt.OnCollision += leftHandler; leftFixt.OnSeparation += leftHandlerEnd; rightFixt = FixtureFactory.AttachEdge(new Vector2(bodyWidth / 2 + 0.02f, -bodyHeight / 2 + 0.01f), new Vector2(bodyWidth / 2 + 0.02f, bodyHeight / 2 - 0.01f), body); rightFixt.IsSensor = true; rightFixt.OnCollision += rightHandler; rightFixt.OnSeparation += rightHandlerEnd; upFixt = FixtureFactory.AttachEdge(new Vector2(-bodyWidth / 2 + 0.01f, -bodyHeight / 2 - 0.02f), new Vector2(bodyWidth / 2 - 0.01f, -bodyHeight / 2 - 0.02f), body); upFixt.IsSensor = true; upFixt.OnCollision += upHandler; rightFixt.OnSeparation += rightHandlerEnd; downFixt = FixtureFactory.AttachEdge(new Vector2(-bodyWidth / 2 + 0.01f, bodyHeight / 2 + 0.02f), new Vector2(bodyWidth / 2 - 0.01f, bodyHeight / 2 + 0.02f), body); downFixt.IsSensor = true; downFixt.OnCollision += downHandler; downFixt.OnSeparation += downHandlerEnd; x = posX; y = posY; tex = content.Load <Texture2D>("Map/Tazmily/Hinawa/Hinawa"); timer = 0; moveTimer = 0; tempStopped = false; walkDown = new Animation(0, 3, 0); posOffY = (int)(-height + ConvertUnits.ToDisplayUnits(bodyHeight) / 2); posOffX = (int)(-ConvertUnits.ToDisplayUnits(bodyWidth) / 2); }
public void reset(Powerup tp) { position = new Vector2(150, 50); body = new Rectangle((int)position.X, (int)position.Y, 50, 50); velocity = new Vector2(0, 0); hState = HorizontalState.standing; //Make an array of three torches torches = new Torch[999]; for (int i = 0; i < torches.Length; i++) { torches[i] = new Torch(this, flameTexture, world); } vState = VerticalState.none; tp.setUses(3); }
public override void Death() { currentHState = HorizontalState.dead; currentVState = VerticalState.dead; Destroy(gameObject); }
public static bool SetPlayerHorizontalState(HorizontalState nextState) { if (player == null) { return false; } CharacterMotor motor = player.GetComponentInChildren<CharacterMotor>(); if (motor == null) { return false; } return motor.ChangeState(nextState); }
/// <summary> /// Will attempt to change this character's present HorizontalState to the specified state. /// Will not do anything if the state could not be changed. /// </summary> /// <param name="nextState">The state to try and change to. </param> /// <returns>TRUE if it was successful, else false.</returns> public bool ChangeState(HorizontalState nextState) { currentHorizontalState = nextState; return nextState == currentHorizontalState; }
public void Update(GameTime gameTime) { var keyboardState = Keyboard.GetState(); switch (verticalState) { case VerticalState.Ground: if (keyboardState.IsKeyDown(Keys.Space)) { verticalState = VerticalState.Jumping; jumpTimer = new TimeSpan(0); } break; case VerticalState.Jumping: jumpTimer += gameTime.ElapsedGameTime; // Simple jumping with platformer physics Bounds.Y -= (250 / (float)jumpTimer.TotalMilliseconds); if (jumpTimer.TotalMilliseconds >= JUMP_TIME) { verticalState = VerticalState.Falling; } break; case VerticalState.Falling: Bounds.Y += vSpeed; // TODO: This needs to be replaced with collision logic if (Bounds.Y > game.GraphicsDevice.Viewport.Height || Bounds.Y == game.GraphicsDevice.Viewport.Height) { verticalState = VerticalState.Ground; Bounds.Y = game.GraphicsDevice.Viewport.Height; } break; } // horizontal movement if (keyboardState.IsKeyDown(Keys.Left)) { // move left horizontalState = HorizontalState.Left; Bounds.X -= speed * (float)gameTime.ElapsedGameTime.TotalMilliseconds; } else if (keyboardState.IsKeyDown(Keys.Right)) { // move right horizontalState = HorizontalState.Right; Bounds.X += speed * (float)gameTime.ElapsedGameTime.TotalMilliseconds; } else { horizontalState = HorizontalState.Idle; } // Stop the ship from going off-screen /* * if (Bounds.X < 0) * { * Bounds.X = 0; * } * if (Bounds.X > game.GraphicsDevice.Viewport.Width - Bounds.Width) * { * Bounds.X = game.GraphicsDevice.Viewport.Width - Bounds.Width; * } */ }
/// <summary> /// This method updates the state of the player based on position and keys pressed. /// </summary> /// <param name="kbState">This is the current KeyBoardState</param> /// <param name="prevState">This is the previouse KeyBoardState</param> public void move(KeyboardState kbState, KeyboardState prevState) { //Print out debugging info Console.WriteLine("VSTATE: " + vState); Console.WriteLine("HSTATE: " + hState); //Console.WriteLine("POSITION: " + " ( " + position.X + ", " + position.Y + " ) "); //Console.WriteLine("VELOCITY: " + " ( " + velocity.X + ", " + velocity.Y + " ) "); //Set the x velocity to 0 automatically and set the horizontal state to standing by default velocity.X = 0; hState = HorizontalState.standing; //Check key presses to move player if (kbState.IsKeyDown(Keys.Left)) { //Move left as long as there is room if (position.X > 0 + speed) { hState = HorizontalState.walkingLeft; //Move the player to the left velocity.X = -1 * speed; } } if (kbState.IsKeyDown(Keys.Right)) { //Move right as long as there is room if (position.X < ((float)screenWidth - speed)/2 - texture.Width / 2) { hState = HorizontalState.walkingRight; //Move the player to the right velocity.X = speed; } else { world.movePlatforms((int)speed); } } //Have the player jump if (kbState.IsKeyDown(Keys.Up) && prevState.IsKeyUp(Keys.Up) && vState == VerticalState.none) { //Set our new player state vState = VerticalState.jumping; velocity.Y = -2 * speed; } //Check to see if our player is standing on anything if (position.Y >= screenHeight - 50 && vState == VerticalState.falling) { position.Y = screenHeight - 50; vState = VerticalState.none; velocity.Y = 0; } //Add our velocity to our position vector position.X += velocity.X; position.Y += velocity.Y; }
internal void Update(float delta, List<Tile> tiles) { TryingToMove = false; bool pushing = false; bool carrying = XState.Equals(HorizontalState.Carrying); #region Trying to move if (Math.Abs(Velocity.X) == 0) TryingToMove = true; #endregion #region Gravity Velocity.Y += WorldSettings.GRAVITY; #endregion #region Update Position/Velocity and Resolve Collision ResolveXCollision(delta, tiles); ResolveYCollision(delta, tiles); #endregion #region Air Friction Velocity.X *= WorldSettings.AIR_FRICTION; #endregion #region Rounding if (Math.Abs(Velocity.X) < WorldSettings.SLIDING_CUTOFF) Velocity.X = 0; #endregion #region UpdateDst UpdateDst(); #endregion #region Next Vertical State Rectangle nextDst = Dst; bool onGround = false; nextDst.Y += (int)Math.Ceiling(Velocity.Y + WorldSettings.GRAVITY * delta); foreach (Tile t in tiles) if (t.Solid) if (nextDst.Intersects(t.Dst)) onGround = true; #endregion #region Next Horizontal State nextDst = Dst; nextDst.X += (int)Math.Round((Velocity.X + MoveSpeed * Direction) * delta); Tile tileToPush = null; foreach (Tile t in tiles) if (t.Solid) if (nextDst.Intersects(t.Dst)) if (t.Pushable) tileToPush = t; if (tileToPush != null) { if (!XState.Equals(HorizontalState.Carrying)) { pushing = true; Game1.Events.Notify(Event.PlayerPushesTile, new object[] { this, tileToPush }); } } #endregion // Update States if (onGround) YState = VerticalState.OnGround; else if (Velocity.Y < 0) YState = VerticalState.Jumping; else if (Velocity.Y > 0) YState = VerticalState.Falling; if (HState.Equals(HealthState.Hurt)) { HurtCounter--; if (HurtCounter <= 0) HState = HealthState.Normal; } if (carrying || pushing) { } else if (Velocity.X == 0) XState = HorizontalState.Stopped; else XState = HorizontalState.Walking; // Lock on moving platforms if (LockOnTileTile != null) { Vector2 tileDisplacement = LockOnTileTile.Movement.GetDisplacement(); if (LockToHorizontalTile) Position.X += tileDisplacement.X; if (LockToVerticalTile) { Position.Y = LockOnTileTile.Position.Y - Dst.Height - 3; // Cancel Out Gravity Velocity.Y -= WorldSettings.GRAVITY * delta; } UpdateDst(); if (!YState.Equals(VerticalState.OnGround)) ReleaseLocks(); } }
/// <summary> /// This method updates the state of the player based on position and keys pressed. /// </summary> /// <param name="kbState">This is the current KeyBoardState</param> /// <param name="prevState">This is the previouse KeyBoardState</param> public void move(KeyboardState kbState, KeyboardState prevState, Powerup tPower) { //Print out debugging info //Console.WriteLine("VSTATE: " + vState); //Console.WriteLine("HSTATE: " + hState); //Console.WriteLine("POSITION: " + " ( " + position.X + ", " + position.Y + " ) "); //Console.WriteLine("VELOCITY: " + " ( " + velocity.X + ", " + velocity.Y + " ) "); //Set the x velocity to 0 automatically and set the horizontal state to standing by default velocity.X = 0; hState = HorizontalState.standing; //Check key presses to move player if ((kbState.IsKeyDown(Keys.Left) && kbState.IsKeyUp(Keys.Right)) || (kbState.IsKeyDown(Keys.A) && kbState.IsKeyUp(Keys.D))) { //Move left as long as there is room if (position.X > 0 + xSpeed) { hState = HorizontalState.walkingLeft; //Move the player to the left velocity.X = -1 * xSpeed; } animCycle.lastFrame(); } if ((kbState.IsKeyDown(Keys.Right) && kbState.IsKeyUp(Keys.Left)) || (kbState.IsKeyDown(Keys.D) && kbState.IsKeyUp(Keys.A))) { //Move right as long as there is room if (position.X < ((float)screenWidth - xSpeed)/2 - texture.Width / 2) { hState = HorizontalState.walkingRight; //Move the player to the right velocity.X = xSpeed; } else { world.bg.scroll((int)xSpeed-3); world.movePlatforms((int)xSpeed); for (int i = 0; i < torches.Length; i++) { if (torches[i].Falling == false) { torches[i].Location = new Vector2(torches[i].Location.X - xSpeed, torches[i].Location.Y); } } hState = HorizontalState.walkingRight; world.rocks.scroll((int)xSpeed - 1); } animCycle.nextFrame(); } // Jumping/Falling if (kbState.IsKeyDown(Keys.Up) || kbState.IsKeyDown(Keys.Space) || kbState.IsKeyDown(Keys.W)) // If the player is currently pressing up. { if (vState == VerticalState.none || vState == VerticalState.fallingPlatform) // If the player's veritcal state is none (not falling or jumping) or the player is on a Falling Platform. { currentPlatform = null; vState = VerticalState.jumping; velocity.Y = -2 * ySpeed; } } else if (vState == VerticalState.jumping) // If the player is currently not pressing up and is jumping. { // Stop the jump. vState = VerticalState.falling; velocity.Y = -0.5f * velocity.Y; } //Check for collision detection if(vState == VerticalState.falling) { foreach (Platform p in world.getPlatforms()) { if (body.Intersects(p.Bounds)) { // && (position.X > p.Bounds.Left - 50 *.75) && (position.X < p.Bounds.Right - 50 * .75) if (position.Y + 44 < p.Bounds.Top + 18) { position.Y = p.Bounds.Top - 43; vState = VerticalState.none; velocity.Y = 0; currentPlatform = p; } } } } if (vState == VerticalState.none) { bool isColliding = false; foreach (Platform p in world.getPlatforms()) { if (body.Intersects(p.Bounds)) { isColliding = true; } } if (!isColliding) { vState = VerticalState.falling; currentPlatform = null; } } if (vState == VerticalState.fallingPlatform) // Check to see if the player is still on the platform horizontally. { if (position.X + 50 < currentPlatform.Bounds.Left || position.X > currentPlatform.Bounds.Right) { vState = VerticalState.falling; currentPlatform = null; } } if (position.Y >= screenHeight - 44) // If the player is at the bottom of the screen. { if (vState == VerticalState.falling || vState == VerticalState.fallingPlatform) // If the player's vertical state is falling or fallingPlatform. { world.reset(tPower); } } //Add our velocity to our position vector position.X += velocity.X; position.Y += velocity.Y; body.X = (int)position.X; body.Y = (int)position.Y; //Check to see if player is throwing torches throwTorches(kbState, prevState, tPower); //Update all torches, if any for (int i = 0; i < torches.Length; i++) { torches[i].update(); } }
internal void Carry(Tile tileToPush) { XState = HorizontalState.Carrying; }