Werwolf696:If I remember correctly, that example already has classes defined for both the player and enemy, you would just need to add a new property (attribute) to the existing class(es) to represent a health value, and update it accordingly in the collision logic.
Yeah, you're right, they do. On the surface it seems pretty trivial to add. I'm trying to implement this, but I might have overlooked something. In player.cs I've set
| public int health { get; set; } |
In the player class. Then set health to 100 in Reset, as this seemed the best place for it.
| public void Reset(Vector2 position) |
| { |
| powerUpTime = 0.0f; |
| Position = position; |
| Velocity = Vector2.Zero; |
| isAlive = true; |
| sprite.PlayAnimation(idleAnimation); |
| health = 100; |
| } |
Then over to level.cs I edited the UpdateEnemies class that handles the collision with the player.
| private void UpdateEnemies(GameTime gameTime) |
| { |
| foreach (Enemy enemy in enemies) |
| { |
| enemy.Update(gameTime); |
| |
| // Touching an enemy instantly kills the player unless powerup |
| if (enemy.IsAlive && enemy.BoundingRectangle.Intersects(Player.BoundingRectangle)) |
| { |
| if (Player.IsPoweredUp) |
| { |
| OnEnemyKilled(enemy, Player); |
| } |
| else if (player.health > 50) |
| { |
| player.health -= 50; |
| } |
| else |
| { |
| OnPlayerKilled(enemy); |
| } |
| } |
| |
| } |
| } |
However when I collide I just die like normal. Maybe i'm overlooking something, maybe because it's 1am? ;) Thanks for any help in advance!