123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Graphics;
- namespace Asteroid_Belt_Assault
- {
- class Sprite
- {
- public Texture2D Texture;
- protected List<Rectangle> frames = new List<Rectangle>();
- private int frameWidth = 0;
- private int frameHeight = 0;
- private int currentFrame;
- private float frameTime = 0.1f;
- private float timeForCurrentFrame = 0.0f;
- private Color tintColor = Color.White;
- private float rotation = 0.0f;
- public int CollisionRadius = 0;
- public int BoundingXPadding = 0;
- public int BoundingYPadding = 0;
- protected Vector2 location = Vector2.Zero;
- protected Vector2 velocity = Vector2.Zero;
- public Sprite(
- Vector2 location,
- Texture2D texture,
- Rectangle initialFrame,
- Vector2 velocity)
- {
- this.location = location;
- Texture = texture;
- this.velocity = velocity;
- frames.Add(initialFrame);
- frameWidth = initialFrame.Width;
- frameHeight = initialFrame.Height;
- }
- public Vector2 Location
- {
- get { return location; }
- set { location = value; }
- }
- public Vector2 Velocity
- {
- get { return velocity; }
- set { velocity = value; }
- }
- public Color TintColor
- {
- get { return tintColor; }
- set { tintColor = value; }
- }
- public float Rotation
- {
- get { return rotation; }
- set { rotation = value % MathHelper.TwoPi; }
- }
- public int Frame
- {
- get { return currentFrame; }
- set
- {
- currentFrame = (int)MathHelper.Clamp(value, 0,
- frames.Count - 1);
- }
- }
- public float FrameTime
- {
- get { return frameTime; }
- set { frameTime = MathHelper.Max(0, value); }
- }
- public Rectangle Source
- {
- get { return frames[currentFrame]; }
- }
- public Rectangle Destination
- {
- get
- {
- return new Rectangle(
- (int)location.X,
- (int)location.Y,
- frameWidth,
- frameHeight);
- }
- }
- public Vector2 Center
- {
- get
- {
- return location +
- new Vector2(frameWidth / 2, frameHeight / 2);
- }
- }
- public Rectangle BoundingBoxRect
- {
- get
- {
- return new Rectangle(
- (int)location.X + BoundingXPadding,
- (int)location.Y + BoundingYPadding,
- frameWidth - (BoundingXPadding * 2),
- frameHeight - (BoundingYPadding * 2));
- }
- }
- public bool IsBoxColliding(Rectangle OtherBox)
- {
- return BoundingBoxRect.Intersects(OtherBox);
- }
- public bool IsCircleColliding(Vector2 otherCenter, float otherRadius)
- {
- if (Vector2.Distance(Center, otherCenter) <
- (CollisionRadius + otherRadius))
- return true;
- else
- return false;
- }
- public void AddFrame(Rectangle frameRectangle)
- {
- frames.Add(frameRectangle);
- }
- public virtual void Update(GameTime gameTime)
- {
- float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;
- timeForCurrentFrame += elapsed;
- if (timeForCurrentFrame >= FrameTime)
- {
- currentFrame = (currentFrame + 1) % (frames.Count);
- timeForCurrentFrame = 0.0f;
- }
- location += (velocity * elapsed);
- }
- public virtual void Draw(SpriteBatch spriteBatch)
- {
- spriteBatch.Draw(
- Texture,
- Center,
- Source,
- tintColor,
- rotation,
- new Vector2(frameWidth / 2, frameHeight / 2),
- 1.0f,
- SpriteEffects.None,
- 0.0f);
- }
- }
- }
|