Skip to content

Commit

Permalink
Added progressbar render system.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kotáb Petr committed Jan 8, 2023
1 parent a504dc0 commit ffe8f60
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 0 deletions.
1 change: 1 addition & 0 deletions TestGame/Scenes/LevelScene.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ protected override void CreateSystems()
.AddSystem(new RenderSystem(Game.SpriteBatch, Game.Camera))
.AddSystem(new PlantPlacementSystem(Game.SpriteBatch, Game.Camera, this))
.AddSystem(new PlantSystem(this));
//.AddSystem(new ProgressBarRenderSystem(Game.SpriteBatch, Game.Camera, Game.GraphicsDevice));
}

protected override void CreateEntities()
Expand Down
9 changes: 9 additions & 0 deletions TestGame/Systems/PlantPlacementSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,18 @@ public override void Draw(Apperance apperance, FarmPlot farmPlot, GameTime gameT
var plant = plantType.CreatePlant();
plant.FarmPlot = farmPlot;

var progressBar = new ProgressBar()
{
StartValue = 0,
EndValue = plant.MaxWater,
CurrentValue = plant.CurrentWater,
Color = Color.Blue,
};

var plantEntity = CreateEntity();
plantEntity.Attach(plantApperance);
plantEntity.Attach(plant);
plantEntity.Attach(progressBar);

farmPlot.Occupied = true;
inventory.SelectedItem.Quantity--;
Expand Down
63 changes: 63 additions & 0 deletions TestGame/Systems/ProgressBarRenderSystem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using TestGame.Components;

namespace TestGame.Systems
{
internal class ProgressBarRenderSystem : EntityDrawSystem<Apperance, ProgressBar>
{
private readonly Camera camera;
private readonly Texture2D texture;

public ProgressBarRenderSystem(
SpriteBatch spriteBatch,
Camera camera,
GraphicsDevice device
)
: base(spriteBatch)
{
this.camera = camera;

texture = new Texture2D(device, 1, 1);
texture.SetData(new Color[] { Color.White });
}

public override void PreDraw(GameTime gameTime)
=> SpriteBatch.Begin(transformMatrix: camera.GetTransform());

public override void PostDraw(GameTime gameTime)
=> SpriteBatch.End();

public override void Draw(Apperance apperance, ProgressBar progressBar, GameTime gameTime)
{
float relativeWidth = 0.9f;
float relativeHeight = 0.05f;

var position = apperance.Position + new Vector2()
{
X = apperance.Size.X * (1.0f - relativeWidth),
Y = apperance.Size.Y * (1.0f - relativeHeight),
};
Vector2 scale = apperance.Size * new Vector2(relativeWidth, relativeHeight);

SpriteBatch.Draw(
texture,
position,
null,
progressBar.Color,
0,
Vector2.Zero,
scale,
SpriteEffects.None,
0
);
}
}
}

0 comments on commit ffe8f60

Please sign in to comment.