Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sprites #106

Merged
merged 3 commits into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions src/Client/Components/Sprite.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,36 @@ namespace Client.Components
{
public class Sprite : Shared.Components.Component
{
public Sprite(Texture2D texture)
public Sprite(Texture2D texture, bool isAnimated = false, int row = 0)
{
this.texture = texture;
center = new Vector2(texture.Width / 2, texture.Height / 2);
this.isAnimated = isAnimated;
if (isAnimated)
{
Row = row;
FrameCount = 4;
RowCount = 8;
FrameSpeed = 1.1f;
IsLooping = true;
CurrentFrame = 0;
Timer = 0f;
SourceRect = new Rectangle(0, Row * texture.Height/ RowCount , texture.Width / FrameCount, texture.Height/ RowCount);
}
}

// For normal sprites
public Texture2D texture { get; private set; }
public Vector2 center { get; private set; }
// For animated sprites
public int Row { get; private set; }
public int RowCount { get; private set; }
public int FrameCount { get; private set; }
public bool isAnimated { get; private set; }
public Rectangle SourceRect { get; set; }
public int CurrentFrame { get; set; }
public float FrameSpeed { get; set; }
public float Timer { get; set; }
public bool IsLooping { get; set; }

}
}
14 changes: 14 additions & 0 deletions src/Client/Content/Content.mgcb
Original file line number Diff line number Diff line change
Expand Up @@ -274,3 +274,17 @@
/processorParam:TextureFormat=Color
/build:Textures/wall.png

#begin Textures/rotating_orbs.png
/importer:TextureImporter
/processor:TextureProcessor
/processorParam:ColorKeyColor=255,0,255,255
/processorParam:ColorKeyEnabled=True
/processorParam:GenerateMipmaps=False
/processorParam:PremultiplyAlpha=True
/processorParam:ResizeToPowerOfTwo=False
/processorParam:MakeSquare=False
/processorParam:TextureFormat=Color
/build:Textures/rotating_orbs.png



Binary file added src/Client/Content/Textures/rotating_orbs.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
42 changes: 29 additions & 13 deletions src/Client/GameModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,18 +69,10 @@ public void update(TimeSpan elapsedTime)
m_systemWormMovement.update(elapsedTime);
m_systemInterpolation.update(elapsedTime);
m_systemCamera.update(elapsedTime);
m_renderer.update(elapsedTime);
GameTime gameTime = new GameTime(totalGameTime: TimeSpan.Zero, elapsedGameTime: elapsedTime);

if (eatParticleSystem != null)
{
eatParticleSystem.update(gameTime);
}

if (deathParticleSystem != null)
{
deathParticleSystem.update(gameTime);
}

eatParticleSystem.update(gameTime);
deathParticleSystem.update(gameTime);
}

/// <summary>
Expand Down Expand Up @@ -149,8 +141,32 @@ private Entity createEntity(Shared.Messages.NewEntity message)

if (message.hasAppearance)
{
Texture2D texture = m_contentManager.Load<Texture2D>(message.texture);
entity.add(new Components.Sprite(texture));
var isSpice = message.hasSpicePower && !message.hasWorm;
if (isSpice)
{
int row = 0;
if (message.spicePower > 7)
{
row = 2;
} else if (message.spicePower > 5)
{
row = 3;
} else if (message.spicePower > 3)
{
row = 1;
} else if (message.spicePower > 1)
{
row = 6;
}

Texture2D texture = m_contentManager.Load<Texture2D>("Textures/rotating_orbs");
entity.add(new Components.Sprite(texture, true, row));
}
else
{
Texture2D texture = m_contentManager.Load<Texture2D>(message.texture);
entity.add(new Components.Sprite(texture));
}
}
if (message.hasPosition)
{
Expand Down
47 changes: 0 additions & 47 deletions src/Client/Systems/AnimatedSprite.cs

This file was deleted.

32 changes: 0 additions & 32 deletions src/Client/Systems/AnimatedSpriteObject.cs

This file was deleted.

89 changes: 63 additions & 26 deletions src/Client/Systems/Renderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,34 @@ public Renderer(Systems.Camera camera, GraphicsDeviceManager graphics, SpriteFon

public override void update(TimeSpan elapsedTime)
{
foreach (var entity in m_entities.Values)
{
var sprite = entity.get<Sprite>();
if (!sprite.isAnimated) continue;
var animation = sprite;
animation.Timer += (float)elapsedTime.TotalMilliseconds;

if (animation.Timer > animation.FrameSpeed)
{
animation.Timer = 0f;
animation.CurrentFrame++;

if (animation.CurrentFrame >= animation.FrameCount)
{
animation.CurrentFrame = animation.IsLooping ? 0 : animation.FrameCount - 1;
}

// Update the sprite's source rectangle for the new frame
sprite.SourceRect = new Rectangle(
sprite.SourceRect.Width * animation.CurrentFrame, // X position of frame
sprite.SourceRect.Y, // Y is constant
sprite.SourceRect.Width, // Frame width
sprite.SourceRect.Height // Frame height
);
}
}
}


public void render(TimeSpan elapsedTime, SpriteBatch spriteBatch, PlayerData playerData, ParticleSystem eatParticles, ParticleSystem deathParticles)
{
Expand All @@ -66,17 +93,18 @@ public void render(TimeSpan elapsedTime, SpriteBatch spriteBatch, PlayerData pla
matrix *= Matrix.CreateTranslation(new Vector3(offset, 0));
matrix *= Matrix.CreateScale(scaleX, scaleY, 1);

// Begin drawing
spriteBatch.Begin(transformMatrix: matrix);
drawBackgroundTiles(spriteBatch);
var heads = new List<Entity>();
var walls = new List<Entity>();
var spice = new List<Entity>();
sortEntities(heads, spice, walls);
foreach (Entity entity in spice)
renderEntity(elapsedTime, spriteBatch, entity);
foreach (Entity entity in walls)
renderEntity(elapsedTime, spriteBatch, entity);
var others = new List<Entity>();
sortEntities(heads, spice, walls, others);

// Begin drawing
spriteBatch.Begin(transformMatrix: matrix);
drawBackgroundTiles(spriteBatch);
foreach (Entity entity in others) renderEntity(elapsedTime, spriteBatch, entity);
foreach (Entity entity in spice) renderSpice(elapsedTime, spriteBatch, entity);
foreach (Entity entity in walls) renderEntity(elapsedTime, spriteBatch, entity);
drawWorms(elapsedTime, spriteBatch, heads);
eatRenderer.draw(spriteBatch, eatParticles );
deathRenderer.draw(spriteBatch, deathParticles);
Expand All @@ -93,18 +121,14 @@ public void render(TimeSpan elapsedTime, SpriteBatch spriteBatch, PlayerData pla
drawStats(spriteBatch, head);
}
}

if (isGameOver)
{
drawGameOverScreen(spriteBatch, playerData);
}

drawGameOverScreen(spriteBatch, playerData, isGameOver);
drawLeaderboard(spriteBatch, heads);
spriteBatch.End();
}

private void drawGameOverScreen(SpriteBatch spriteBatch, PlayerData playerData)
private void drawGameOverScreen(SpriteBatch spriteBatch, PlayerData playerData, bool isGameOver)
{
if (!isGameOver) return;
Drawing.DrawBlurredRectangle(spriteBatch, new Vector2(m_graphics.PreferredBackBufferWidth / 2 - 200, m_graphics.PreferredBackBufferHeight / 2 - 150), new Vector2(400, 300), 7, transparency:0.6f);
Drawing.CustomDrawString(m_font, "Game Over", new Vector2(m_graphics.PreferredBackBufferWidth / 2, m_graphics.PreferredBackBufferHeight / 2 - 100), Color.White, spriteBatch, centered: true);
Drawing.CustomDrawString(m_font, "Final Score: " + playerData.score, new Vector2(m_graphics.PreferredBackBufferWidth / 2, m_graphics.PreferredBackBufferHeight / 2 - 50), Color.White, spriteBatch, centered: true);
Expand All @@ -123,14 +147,16 @@ private void drawWorms(TimeSpan elapsedTime, SpriteBatch spriteBatch, List<Entit
}
}

private void sortEntities(List<Entity> heads, List<Entity> others, List<Entity> walls)
private void sortEntities(List<Entity> heads, List<Entity> animatedSprites, List<Entity> walls, List<Entity> others)
{
foreach (Entity entity in m_entities.Values)
{
if (entity.contains<Head>())
heads.Add(entity);
else if (entity.contains<Worm>())
continue;
else if (entity.contains<Sprite>() && entity.get<Sprite>().isAnimated)
animatedSprites.Add(entity);
else if (entity.contains<Shared.Components.Wall>())
walls.Add(entity);
else
Expand Down Expand Up @@ -208,6 +234,26 @@ private void drawStats(SpriteBatch spriteBatch, Entity head)
boxed: true
);
}

private void renderSpice(TimeSpan elapsedTime, SpriteBatch spriteBatch, Entity entity)
{
var sprite = entity.get<Sprite>();
var position = entity.get<Position>().position;
var rotation = entity.contains<Position>() ? entity.get<Position>().orientation : 0f;
var scale = entity.get<SpicePower>().power / 3;

spriteBatch.Draw(
sprite.texture,
position,
sprite.SourceRect, // Use the source rectangle which is set by the AnimationSystem
Color.White, // Assuming white means "use texture colors"
rotation,
new Vector2(sprite.SourceRect.Width / 2, sprite.SourceRect.Height / 2), // Origin of the rotation
scale,
SpriteEffects.None,
0f
);
}

private void renderEntity(TimeSpan elapsedTime, SpriteBatch spriteBatch, Entity entity)
{
Expand All @@ -222,16 +268,7 @@ private void renderEntity(TimeSpan elapsedTime, SpriteBatch spriteBatch, Entity
{
color = Color.Coral;
}

if (entity.contains<SpicePower>() && !entity.contains<Worm>())
{
var spicePower = entity.get<SpicePower>();
if (spicePower.power > 7)
color = Color.DarkBlue;
else if (spicePower.power > 4)
color = Color.LightBlue;
}


// Build a rectangle centered at position, with width/height of size
Rectangle rectangle = new(
(int)position.X,
Expand Down
Loading
Loading