Permalink
Browse files

step 6 - collision detection

  • Loading branch information...
becdetat committed May 25, 2014
1 parent 144bf66 commit 7398a2628bffb1c931e6755d9ba3a1737c320e36
@@ -1,39 +1,34 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
namespace Win8ShooterGame
{
public class Enemy : ISprite
{
private int _health = 10;
private readonly Animation _animation = new Animation();
public bool IsActive { get; private set; }
public int Damage{get { return 10; }}
private int _health = 10;
private Vector2 _position;
private int _damage = 10;
private float _speed = 6.0f;
private int _scoreValue = 100;
public int Width { get { return _animation.FrameWidth; } }
private float _speed = 6.0f;
public Enemy()
{
IsActive = true;
}
public void Initialize(Func<string, Texture2D> getTexture, Viewport viewport)
public bool IsActive { get; private set; }
public int Width
{
var texture = getTexture("Graphics/mineAnimation");
_animation.Initialize(texture, 47, 30, 8);
get { return _animation.FrameWidth; }
}
public void SetPosition(Vector2 position)
public void Initialize(Func<string, Texture2D> getTexture, Viewport viewport)
{
_position = position;
var texture = getTexture("Graphics/mineAnimation");
_animation.Initialize(texture, 47, 30, 8);
}
public void Update(ShooterGameInputState input)
@@ -51,12 +46,24 @@ public void Draw(GameTime gameTime, SpriteBatch batch)
_animation.Update(gameTime, _position);
_animation.Draw(batch);
}
}
public interface ISprite
{
void Initialize(Func<string, Texture2D> getTexture, Viewport viewport);
void Update(ShooterGameInputState input);
void Draw(GameTime gameTime, SpriteBatch batch);
public void SetPosition(Vector2 position)
{
_position = position;
}
public Rectangle GetBounds()
{
return new Rectangle(
(int)_position.X,
(int)_position.Y,
_animation.FrameHeight,
_animation.FrameWidth);
}
public void Destroy()
{
_health = 0;
}
}
}
}
@@ -0,0 +1,14 @@
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace Win8ShooterGame
{
public interface ISprite
{
void Initialize(Func<string, Texture2D> getTexture, Viewport viewport);
void Update(ShooterGameInputState input);
void Draw(GameTime gameTime, SpriteBatch batch);
Rectangle GetBounds();
}
}
@@ -12,6 +12,8 @@ public class Player : ISprite
private const float Speed = 8.0f;
private readonly Animation _animation = new Animation();
private Vector2 _position;
private bool _active = true;
private int _health = 100;
private int Width
{
@@ -40,6 +42,11 @@ public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
public void Update(ShooterGameInputState gameInputState)
{
if (_health <= 0)
{
_active = false;
}
UpdatePosition(gameInputState);
_animation.Update(gameInputState.GameTime, _position);
}
@@ -98,5 +105,19 @@ private void UpdatePosition(ShooterGameInputState gameInputState)
_position.X = MathHelper.Clamp(_position.X + dx, 0, gameInputState.Viewport.Width - Width);
_position.Y = MathHelper.Clamp(_position.Y + dy, 0, gameInputState.Viewport.Height - Height);
}
public Rectangle GetBounds()
{
return new Rectangle(
(int) _position.X,
(int) _position.Y,
_animation.FrameHeight,
_animation.FrameWidth);
}
public void ReceiveDamage(int points)
{
_health -= points;
}
}
}
@@ -97,14 +97,30 @@ protected override void Update(GameTime gameTime)
var currentGameState = GetCurrentState(gameTime);
_player.Update(currentGameState);
_background1.Update(gameTime);
_background2.Update(gameTime);
_player.Update(currentGameState);
UpdateEnemies(currentGameState);
UpdateCollisions();
base.Update(gameTime);
}
private void UpdateCollisions()
{
var playerRectangle = _player.GetBounds();
foreach (var enemy in _enemies)
{
if (playerRectangle.Intersects(enemy.GetBounds()))
{
_player.ReceiveDamage(enemy.Damage);
enemy.Destroy();
}
}
}
private void UpdateEnemies(ShooterGameInputState input)
{
if (input.GameTime.TotalGameTime - _previousEnemySpawnTime > _enemySpawnTime)
@@ -130,6 +130,7 @@
<Compile Include="Animation.cs" />
<Compile Include="Enemy.cs" />
<Compile Include="Extensions\CollectionExtensions.cs" />
<Compile Include="ISprite.cs" />
<Compile Include="ParallaxingBackground.cs" />
<Compile Include="Player.cs" />
<Compile Include="Program.cs" />

0 comments on commit 7398a26

Please sign in to comment.