Skip to content

Instantly share code, notes, and snippets.

@hamoungh
Created January 30, 2015 20:34
Show Gist options
  • Save hamoungh/820e98a2222649491ec0 to your computer and use it in GitHub Desktop.
Save hamoungh/820e98a2222649491ec0 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace ShipFighterClasses
{
public class Player
{
// This variable will hold our position - make it a property so game class
//can use it to change position when mouse moved
protected Vector2 position;
public Vector2 Position
{
get { return position; }
set { position = value; }
}
//make these protected so derived classes can use it
public Texture2D TextureImage { get; set; }
//origin of sprite, either null or the center of the image
protected Vector2 spriteOrigin;
public Vector2 SpriteOrigin
{
get { return spriteOrigin; }
set { spriteOrigin = value; }
}
public virtual Rectangle CollisionRectangle
{
get
{
return new Rectangle(
(int)(position.X - spriteOrigin.X * Scale),
(int)(position.Y - spriteOrigin.Y * Scale),
Convert.ToInt32(TextureImage.Width * Scale) ,
Convert.ToInt32(TextureImage.Height * Scale)
);
}
}
//vector so it has independant x and y values
protected Vector2 velocity;
public Vector2 Velocity
{
get { return velocity; }
set { velocity = value; }
}
protected Vector2 acceleration;
public Vector2 Acceleration
{
get { return acceleration; }
set { acceleration = value; }
}
//whether to use the Origin or not
public bool SetOrigin { get; set; }
public float Scale { get; set; }
//is he active or not (should he be updated and drawn?)
public bool Alive { get; set; }
//simpler than Sprite, missing a few properties
// base version
public Player(Texture2D textureImage, Vector2 position, Vector2 velocity, Vector2 acceleration,
bool setOrigin, float scale)
{
Position = position;
TextureImage = textureImage;
Acceleration = acceleration;
Velocity = velocity;
SetOrigin = setOrigin;
if (SetOrigin)
{
SpriteOrigin = new Vector2(TextureImage.Width / 2, TextureImage.Height / 2);
}
Scale = scale;
Alive = true;
}
public bool CollisionSprite(Player sprite)
{
return this.CollisionRectangle.Intersects(sprite.CollisionRectangle);
}
//version that does not keep sprite on screen
public virtual void Update(GameTime gameTime)
{
if (Alive)
{
// time between frames
float timeLapse = (float)(gameTime.ElapsedGameTime.TotalSeconds);
//move the sprite
position += Velocity * timeLapse;
}
}
//version that keeps sprite on screen
public virtual void Update(GameTime gameTime, GraphicsDevice Device)
{
if (Alive)
{
//call overload to do rotation and basic movement
Update(gameTime);
//keep on screen
if (Position.X > Device.Viewport.Width - SpriteOrigin.X * Scale)
{
position.X = Device.Viewport.Width - SpriteOrigin.X * Scale;
velocity.X = -Velocity.X;
}
else if (Position.X < SpriteOrigin.X * Scale)
{
position.X = SpriteOrigin.X * Scale;
velocity.X = -Velocity.X;
}
if (Position.Y > Device.Viewport.Height - SpriteOrigin.Y * Scale)
{
position.Y = Device.Viewport.Height - SpriteOrigin.Y * Scale;
velocity.Y = -Velocity.Y;
}
else if (Position.Y < SpriteOrigin.Y * Scale)
{
position.Y = SpriteOrigin.Y * Scale;
velocity.Y = -Velocity.Y;
}
}
}
public virtual void Draw(SpriteBatch spriteBatch)
{
if (Alive)
{
spriteBatch.Draw(TextureImage,
position,
null,
Color.White,
0f,
SpriteOrigin,
Scale,
SpriteEffects.None,
0);
}
}
public void Up()
{
velocity.Y -= Acceleration.Y;
}
public void Left()
{
velocity.X -= Acceleration.X;
}
public void Down()
{
velocity.Y += Acceleration.Y;
}
public void Right()
{
velocity.X += Acceleration.X;
}
public virtual void Idle()
{
Velocity *= .95f;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment