public TileCharacter(Engine engine, Vector2 tilePosition) : base(engine) { TilePosition = tilePosition; Position = new Vector2(TilePosition.X * Engine.Level.TileWidth, TilePosition.Y * Engine.Level.TileHeight); Moving = false; Direction = Global.Directions.Down; testArt = new Sprite(Engine, @"Characters\TestSquare"); testArt.Position = Position; MovementSpeed = Global.Configuration.GetFloatConfig("TileBasedVariables", "MovementSpeed"); MoveStartTime = 0.0f; Engine.AddComponent(this); }
public bool Move(Global.Directions direction) { Direction = direction; if (Moving) { return(false); } Vector2 NewTilePosition = new Vector2(); switch (direction) { case Global.Directions.Up: NewTilePosition = new Vector2((int)TilePosition.X, (int)TilePosition.Y - 1); break; case Global.Directions.Down: NewTilePosition = new Vector2((int)TilePosition.X, (int)TilePosition.Y + 1); break; case Global.Directions.Left: NewTilePosition = new Vector2((int)TilePosition.X - 1, (int)TilePosition.Y); break; case Global.Directions.Right: NewTilePosition = new Vector2((int)TilePosition.X + 1, (int)TilePosition.Y); break; } bool moveResult = Engine.Level.CheckMove((int)NewTilePosition.X, (int)NewTilePosition.Y); if (moveResult) { OldPosition = Position; TilePosition = NewTilePosition; TargetPosition = new Vector2(TilePosition.X * Engine.Level.TileWidth, TilePosition.Y * Engine.Level.TileHeight); } Moving = moveResult; return(moveResult); }
public override void Update(GameTime gameTime) { if (!Dead) { bulletTimer += gameTime.ElapsedGameTime.TotalMilliseconds; // update whether or not the PlayerEnemy can fire UpdateProjectiles(); Position = PhysicsComponent.Position; light.Position = Position + new Vector2(0, 25); if (OnGround) { airTimer = 0.0f; } else { airTimer += gameTime.ElapsedGameTime.TotalMilliseconds; } // check to see if the PlayerEnemy is even allowed to jump before we do anything if (!Crouching && OnGround) { if (random.Next(100) > 90) { // using world center as the point to apply force to; this makes the point of the force application the center of the fixture PhysicsComponent.MainFixture.Body.ApplyForce(new Vector2(0.0f, -80.0f), PhysicsComponent.MainFixture.Body.WorldCenter); } } if (Engine.Player.Position.X < Position.X && Math.Abs(Engine.Player.Position.X - Position.X) < 800) { LastDirection = Global.Directions.Left; if (!Crouching) { PhysicsComponent.MoveLeft(); if (!OnGround) { // apply a bit of force in the air if the air timer is still under the air movement window if (airTimer < airMovementWindow) { PhysicsComponent.MainFixture.Body.ApplyForce(-airMovementForce); } } // we're only walking if we're on the ground Walking = OnGround; } else { PhysicsComponent.StopMoving(); Walking = false; } } else if (Engine.Player.Position.X > Position.X && Math.Abs(Engine.Player.Position.X - Position.X) < 800) { LastDirection = Global.Directions.Right; if (!Crouching) { PhysicsComponent.MoveRight(); if (!OnGround) { // apply a bit of force in the air if the air timer is still under the air movement window if (airTimer < airMovementWindow) { PhysicsComponent.MainFixture.Body.ApplyForce(airMovementForce); } } // we're only walking if we're on the ground Walking = OnGround; } else { PhysicsComponent.StopMoving(); Walking = false; } } else { PhysicsComponent.StopMoving(); Walking = false; } UpdateAllArt(); } else { light.IsOn = false; HideAllArt(); PhysicsComponent.DestroyPlayerPhysicsObjects(); } base.Update(gameTime); }
public override void Update(GameTime gameTime) { if (Health <= 0) { // handle death HideAllArt(); UpdateDeath(); return; } else if (Health < 100) { Health += 0.1f; } UpdateBloom(); bulletTimer += gameTime.ElapsedGameTime.TotalMilliseconds; // update whether or not the player can fire UpdateProjectiles(); // check if the player needs to be reset if (Position.Y > 2000.0f) { ResetPlayer(); } if (Position.X > 16000.0f) { LoadingScreen.Load(Engine.ScreenManager, false, null, new BackgroundScreen(), new MainMenuScreen()); } Position = PhysicsComponent.Position; light.Position = Position + new Vector2(0, 25); AudioListener.Position = new Vector3(Position, 0); if (OnGround) { airTimer = 0.0f; Crouching = Engine.Input.IsButtonDown(Global.Configuration.GetButtonConfig("GameControls", "DownButton")) || Engine.Input.IsKeyDown(Global.Configuration.GetKeyConfig("GameControls", "DownKey")); } else { airTimer += gameTime.ElapsedGameTime.TotalMilliseconds; } // check to see if the player is even allowed to jump before we do anything if (!Crouching && OnGround) { if (Engine.Input.IsButtonDown(Global.Configuration.GetButtonConfig("GameControls", "JumpButton")) || Engine.Input.IsKeyDown(Global.Configuration.GetKeyConfig("GameControls", "JumpKey"))) { // using world center as the point to apply force to; this makes the point of the force application the center of the fixture PhysicsComponent.MainFixture.Body.ApplyForce(new Vector2(0.0f, -80.0f), PhysicsComponent.MainFixture.Body.WorldCenter); } } if (Engine.Input.IsButtonDown(Global.Configuration.GetButtonConfig("GameControls", "LeftButton")) || Engine.Input.IsKeyDown(Global.Configuration.GetKeyConfig("GameControls", "LeftKey"))) { LastDirection = Global.Directions.Left; if (!Crouching) { PhysicsComponent.MoveLeft(); if (!OnGround) { // apply a bit of force in the air if the air timer is still under the air movement window if (airTimer < airMovementWindow) { PhysicsComponent.MainFixture.Body.ApplyForce(-airMovementForce); } } // we're only walking if we're on the ground Walking = OnGround; } else { PhysicsComponent.StopMoving(); Walking = false; } } else if (Engine.Input.IsButtonDown(Global.Configuration.GetButtonConfig("GameControls", "RightButton")) || Engine.Input.IsKeyDown(Global.Configuration.GetKeyConfig("GameControls", "RightKey"))) { LastDirection = Global.Directions.Right; if (!Crouching) { PhysicsComponent.MoveRight(); if (!OnGround) { // apply a bit of force in the air if the air timer is still under the air movement window if (airTimer < airMovementWindow) { PhysicsComponent.MainFixture.Body.ApplyForce(airMovementForce); } } // we're only walking if we're on the ground Walking = OnGround; } else { PhysicsComponent.StopMoving(); Walking = false; } } else { PhysicsComponent.StopMoving(); Walking = false; } UpdateAllArt(); }