Skip to content

Commit

Permalink
fix: adding sprite.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
DevinRosales committed Feb 18, 2024
1 parent b1cd241 commit 2db0a26
Show file tree
Hide file tree
Showing 3 changed files with 220 additions and 8 deletions.
2 changes: 1 addition & 1 deletion RainCloud/CloudMovement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using System;
using System.Diagnostics;

namespace RainCloud2
namespace PlayerMovement
{
public class CloudMovement
{
Expand Down
79 changes: 72 additions & 7 deletions RainCloud/Game1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
using Microsoft.Xna.Framework.Input;
using System.Diagnostics;
using System.Threading;
using RainCloud2;
using PlayerMovement;
using System.Collections.Generic;
using RainCloud.Sprites;

namespace RainCloud
{
Expand All @@ -17,6 +19,9 @@ public class Game1 : Game
private GraphicsDeviceManager _graphics;
private SpriteBatch _spriteBatch;

private List<Sprite> _sprites;


public Game1()
{
_graphics = new GraphicsDeviceManager(this);
Expand Down Expand Up @@ -50,6 +55,22 @@ protected override void LoadContent()
// TODO: use this.Content to load your game content here

cloudTexture = Content.Load<Texture2D>("cloud1");


_sprites = new List<Sprite>()
{
new Cloud(cloudTexture)
{
cloudPosition, //Position
null, //Source Rectangle
Color.White, //Color
0f, //Rotation
new Vector2(cloudTexture.Width / 2, cloudTexture.Height / 2), //Origin
cloudSize, //scale 0 - 1f
SpriteEffects.None, //Depth
.1f
}
};
}

protected override void Update(GameTime gameTime)
Expand All @@ -64,19 +85,21 @@ protected override void Update(GameTime gameTime)
cloudPosition = CloudMovement.KeyCloudMovement(cloudPosition, cloudSpeed, gameTime);

if (cloudSize <= 0.0)
{
{
Debug.WriteLine("GAME OVER!");
}
}

if (cloudSize <= 0.0)
{
{
Debug.WriteLine("GAME OVER!");
}
}

base.Update(gameTime);

}



protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.LightBlue);
Expand All @@ -89,12 +112,14 @@ protected override void Draw(GameTime gameTime)

_spriteBatch.Begin();



//Rectangle # meanings: Rectangle(int x, int y, int width, int height)
//Rectangle cloudPosition = new Rectangle(100, 100, 200, 100);

//_spriteBatch.Draw(cloudTexture, cloudPosition, Color.White);
//Vector2 cloudPosition = new Vector2(800, 100);

/*
_spriteBatch.Draw(
cloudTexture, //Texture
cloudPosition, //Position
Expand All @@ -104,7 +129,7 @@ protected override void Draw(GameTime gameTime)
new Vector2(cloudTexture.Width / 2, cloudTexture.Height / 2), //Origin
cloudSize, //scale 0 - 1f
SpriteEffects.None, //Depth
.1f
.1f
);
_spriteBatch.Draw(
Expand All @@ -130,11 +155,51 @@ protected override void Draw(GameTime gameTime)
SpriteEffects.None,
.1f
);
*/

foreach (var sprite in _sprites)
sprite.Draw(gameTime, spriteBatch);

_spriteBatch.End();

base.Draw(gameTime);
}
protected void PostUpdate(GameTime gameTime)
{
// 1. Check collision between all current "Sprites"
// 2. Add "Children" to the list of "_sprites" and clear
// 3. Remove all "IsRemoved" sprites

foreach (var spriteA in _sprites)
{
foreach (var spriteB in _sprites)
{
if (spriteA == spriteB)
continue;

if (spriteA.Intersects(spriteB))
spriteA.OnCollide(spriteB);
}
}

int count = _sprites.Count;
for (int i = 0; i < count; i++)
{
foreach (var child in _sprites[i].Children)
_sprites.Add(child);

_sprites[i].Children.Clear();
}

for (int i = 0; i < _sprites.Count; i++)
{
if (_sprites[i].IsRemoved)
{
_sprites.RemoveAt(i);
i--;
}
}
}

}
}
147 changes: 147 additions & 0 deletions RainCloud/Sprites/Sprite.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RainCloud.Sprites
{
public class Sprite : Component, ICloneable
{
protected Texture2D _texture;

protected float _rotation;

protected KeyboardState _currentKey;

protected KeyboardState _previousKey;

public Vector2 Origin;

public Vector2 Position;

public Rectangle Rectangle
{
get
{
return new Rectangle((int)Position.X - (int)Origin.X, (int)Position.Y - (int)Origin.Y, _texture.Width, _texture.Height);
}
}

public List<Sprite> Children { get; set; }

public Color Colour { get; set; }

public Vector2 Direction;

public float RotationVelocity = 3f;

public float LinearVelocity = 4f;

public Sprite Parent;

public float LifeSpan = 0f;

public bool IsRemoved = false;

public readonly Color[] TextureData;

public Matrix Transform
{
get
{
return Matrix.CreateTranslation(new Vector3(-Origin, 0)) *
Matrix.CreateRotationZ(_rotation) *
Matrix.CreateTranslation(new Vector3(Position, 0));
}
}

public Sprite(Texture2D texture)
{
_texture = texture;

// The default origin in the centre of the sprite
Origin = new Vector2(_texture.Width / 2, _texture.Height / 2);

Children = new List<Sprite>();

Colour = Color.White;

TextureData = new Color[_texture.Width * _texture.Height];
_texture.GetData(TextureData);
}

public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
{
spriteBatch.Draw(_texture, Position, null, Colour, _rotation, Origin, 1, SpriteEffects.None, 0);
}

public bool Intersects(Sprite sprite)
{
// Calculate a matrix which transforms from A's local space into
// world space and then into B's local space
var transformAToB = this.Transform * Matrix.Invert(sprite.Transform);

// When a point moves in A's local space, it moves in B's local space with a
// fixed direction and distance proportional to the movement in A.
// This algorithm steps through A one pixel at a time along A's X and Y axes
// Calculate the analogous steps in B:
var stepX = Vector2.TransformNormal(Vector2.UnitX, transformAToB);
var stepY = Vector2.TransformNormal(Vector2.UnitY, transformAToB);

// Calculate the top left corner of A in B's local space
// This variable will be reused to keep track of the start of each row
var yPosInB = Vector2.Transform(Vector2.Zero, transformAToB);

for (int yA = 0; yA < this.Rectangle.Height; yA++)
{
// Start at the beginning of the row
var posInB = yPosInB;

for (int xA = 0; xA < this.Rectangle.Width; xA++)
{
// Round to the nearest pixel
var xB = (int)Math.Round(posInB.X);
var yB = (int)Math.Round(posInB.Y);

if (0 <= xB && xB < sprite.Rectangle.Width &&
0 <= yB && yB < sprite.Rectangle.Height)
{
// Get the colors of the overlapping pixels
var colourA = this.TextureData[xA + yA * this.Rectangle.Width];
var colourB = sprite.TextureData[xB + yB * sprite.Rectangle.Width];

// If both pixel are not completely transparent
if (colourA.A != 0 && colourB.A != 0)
{
return true;
}
}

// Move to the next pixel in the row
posInB += stepX;
}

// Move to the next row
yPosInB += stepY;
}

// No intersection found
return false;
}

public virtual void OnCollide(Sprite sprite)
{

}

public object Clone()
{
return this.MemberwiseClone();
}
}
}

0 comments on commit 2db0a26

Please sign in to comment.