Download entity added and tested.

This commit is contained in:
Harrison Deng 2020-07-11 17:09:33 -05:00
parent 47bfeee127
commit fd8317c613
10 changed files with 224 additions and 41 deletions

107
Entities/DownloadEntity.cs Normal file
View File

@ -0,0 +1,107 @@
using System.Drawing;
using SkinnerBox.Utilities.Gameplay;
using SlatedGameToolkit.Framework.Graphics.Render;
using SlatedGameToolkit.Framework.Graphics.Textures;
using SlatedGameToolkit.Framework.Utilities.Collections.Pooling;
namespace SkinnerBox.Entities
{
public class DownloadEntity : Entity, IPoolable
{
private readonly float unitSize = 1/2f;
private readonly float unitPerProgressTexture = 0.5f;
public float stepSize;
public TransitionValue progressValue;
public RectangleMesh progressMesh;
public TransitionValue timeElapsed;
public float health;
int size;
public int Size {
get {
return size;
}
set {
this.size = value;
mesh.TextureBounds = new RectangleF(0, 0, size, 1f);
Width = unitSize * size;
}
}
public override RectangleF HitBox {
get {
RectangleF rect = base.HitBox;
rect.Width = Width * size;
return rect;
}
}
public DownloadEntity(ITexture promptTex, ITexture progressTex) : base(promptTex)
{
Size = 1;
Height = unitSize;
progressMesh = new RectangleMesh(progressTex, Color.White);
Reset();
}
private void UpdateProgressMesh() {
progressMesh.Width = progressValue.Value;
progressMesh.TextureBounds = new RectangleF(0, 0, progressValue.Value * unitPerProgressTexture, 1f);
progressMesh.Height = Height;
progressMesh.X = X;
progressMesh.Y = Y;
}
public void Reset()
{
Size = 1;
X = 0;
Y = 0;
mesh.X = X;
mesh.Y = Y;
progressValue.HardSet(0);
timeElapsed.HardSet(0);
stepSize = 0;
health = 0;
Color = Color.DarkCyan;
UpdateProgressMesh();
}
public void Input(float x) {
if (x <= progressValue.DesignatedValue + (1f/stepSize) && x > progressValue.DesignatedValue) {
progressValue.Value = x;
if (progressValue.Value > Width) progressValue.HardSet(Width);
}
}
public override void InterpolatePosition(float delta) {
progressValue.InterpolatePosition(delta);
timeElapsed.InterpolatePosition(delta);
float prog = timeElapsed.Value / health;
if (prog > 1) prog = 1;
if (prog < 0) prog = 0;
this.Color = Color.FromArgb((int)(byte.MaxValue * (1f - prog)), Color);
UpdateProgressMesh();
}
}
public struct DownloadSpawnInfo
{
public float period;
public float elapsedSinceSpawn;
public float health;
public float stepSize;
public int maximumAmount;
public int generalSize;
public int sizeRange;
public DownloadSpawnInfo(float cooldown, float health, float stepSize, int maxAmount, int generalSize, int sizeRange)
{
this.period = cooldown;
this.elapsedSinceSpawn = 0;
this.health = health;
this.stepSize = stepSize;
this.maximumAmount = maxAmount;
this.sizeRange = sizeRange;
this.generalSize = generalSize;
}
}
}

View File

@ -28,6 +28,28 @@ namespace SkinnerBox.Entities
public void Update(double delta) {
this.Y -= (float)(velocity * delta);
}
}
public struct PacketSpawnInfo
{
public float timeElapsed;
public int period;
public int perSpawn;
public float batchLocation;
public float distanceBetween;
public float jumpDistance;
public float speed;
public float lastSpawnLocation;
public PacketSpawnInfo(int period, int perSpawn, float location, float speed, float distance, float jump) {
timeElapsed = 0;
lastSpawnLocation = 0;
this.period = period;
this.perSpawn = perSpawn;
this.batchLocation = location;
this.distanceBetween = distance;
this.speed = speed;
this.jumpDistance = jump;
}
}
}

View File

@ -55,4 +55,5 @@ namespace SkinnerBox.Entities
}
}
}

View File

@ -1,11 +1,5 @@
using System;
using SkinnerBox.States.Gameplay;
using SkinnerBox.States.Main;
using SkinnerBox.States.Main;
using SlatedGameToolkit.Framework;
using SlatedGameToolkit.Framework.AssetSystem;
using SlatedGameToolkit.Framework.Graphics;
using SlatedGameToolkit.Framework.Graphics.Window;
using SlatedGameToolkit.Framework.Loaders;
namespace SkinnerBox
{

View File

@ -12,6 +12,7 @@ using SlatedGameToolkit.Framework.Input.Devices;
using SlatedGameToolkit.Framework.StateSystem;
using SlatedGameToolkit.Framework.StateSystem.States;
using SlatedGameToolkit.Framework.Utilities.Collections.Pooling;
using SlatedGameToolkit.Framework.Utilities;
namespace SkinnerBox.States.Gameplay
{
@ -24,8 +25,7 @@ namespace SkinnerBox.States.Gameplay
//Cursor information
private float widthFactor, heightFactor;
private float leftXPos; //Last left click position
private float serverTargetPos; //Last left click position
//Entities
private ServerEntity server;
@ -35,7 +35,15 @@ namespace SkinnerBox.States.Gameplay
private ObjectPool<PacketEntity> packetPool;
private List<PacketEntity> activePackets = new List<PacketEntity>();
private SpawnInfo packetSpawnInfo;
private PacketSpawnInfo packetSpawnInfo;
private const float packetSafeMargin = 1/2f;
private ObjectPool<DownloadEntity> downloadPool;
private List<DownloadEntity> activeDownloads = new List<DownloadEntity>();
private DownloadSpawnInfo downloadSpawnInfo;
private const float downloadSafeMargin = 1.5f;
private int viewHeight;
public GamePlayState(MeshBatchRenderer renderer, AssetManager asset)
{
@ -43,17 +51,19 @@ namespace SkinnerBox.States.Gameplay
this.renderer = renderer;
packetPool = new ObjectPool<PacketEntity>(CreatePacket);
warningPool = new ObjectPool<WarningEntity>(createWarning);
downloadPool = new ObjectPool<DownloadEntity>(createDownload);
}
public bool Activate()
{
Keyboard.keyboardUpdateEvent += KeyInputListener;
Mouse.mouseUpdateEvent += MouseInput;
leftXPos = 0.5f * Game.WIDTH_UNITS;
server = new ServerEntity((Texture)assets["serverunit.png"], leftXPos, 0.1f);
serverTargetPos = 0.5f * Game.WIDTH_UNITS;
server = new ServerEntity((Texture)assets["serverunit.png"], serverTargetPos, 0.1f);
random = new Random();
packetSpawnInfo = new SpawnInfo(2, 1, (float)(random.NextDouble() * Game.WIDTH_UNITS), 1f, 0.2f);
packetSpawnInfo = new PacketSpawnInfo(2, 1, (float)(random.NextDouble() * Game.WIDTH_UNITS), 1f, 0.2f, 3f);
downloadSpawnInfo = new DownloadSpawnInfo(4, 6, 3, 1, 4, 2);
return true;
}
@ -65,6 +75,10 @@ namespace SkinnerBox.States.Gameplay
return new WarningEntity((Texture)assets["warning.png"]);
}
public DownloadEntity createDownload() {
return new DownloadEntity((Texture)assets["drag.png"], (Texture)assets["downloadbar.png"]);
}
public bool Deactivate()
{
Keyboard.keyboardUpdateEvent -= KeyInputListener;
@ -102,6 +116,10 @@ namespace SkinnerBox.States.Gameplay
{
renderer.Draw(packet);
}
foreach(DownloadEntity download in activeDownloads) {
renderer.Draw(download);
renderer.Draw(download.progressMesh);
}
renderer.Draw(server);
renderer.End();
}
@ -110,17 +128,17 @@ namespace SkinnerBox.States.Gameplay
{
#region ServerUpdate
if (Mouse.LeftButtonPressed) {
leftXPos = widthFactor * Mouse.X;
serverTargetPos = widthFactor * Mouse.X;
}
if (leftXPos < server.CenterX)
if (serverTargetPos < server.CenterX)
{
server.CenterX -= ((float)timeStep * server.Speed);
if (server.CenterX < leftXPos) server.CenterX = leftXPos;
} else if (leftXPos > server.CenterX)
if (server.CenterX < serverTargetPos) server.CenterX = serverTargetPos;
} else if (serverTargetPos > server.CenterX)
{
server.CenterX += ((float)timeStep * server.Speed);
if (server.X > leftXPos) server.CenterX = leftXPos;
if (server.X > serverTargetPos) server.CenterX = serverTargetPos;
}
#endregion
#region PacketUpdate
@ -145,7 +163,10 @@ namespace SkinnerBox.States.Gameplay
activeWarnings.Add(warning);
//Prepare next batch
packetSpawnInfo.batchLocation = (float)(random.NextDouble() * Game.WIDTH_UNITS);
packetSpawnInfo.batchLocation = (float)(packetSpawnInfo.batchLocation + (random.NextDouble() - 1/2f) * packetSpawnInfo.jumpDistance * 2);
if (packetSpawnInfo.batchLocation > Game.WIDTH_UNITS - packetSafeMargin) {
packetSpawnInfo.batchLocation = Game.WIDTH_UNITS - packetSafeMargin;
} else if (packetSpawnInfo.batchLocation < packetSafeMargin) packetSpawnInfo.batchLocation = packetSafeMargin;
}
for (int i = 0; i < activePackets.Count; i++)
@ -169,6 +190,56 @@ namespace SkinnerBox.States.Gameplay
}
}
#endregion
#region DownloadUpdate
downloadSpawnInfo.elapsedSinceSpawn += (float)timeStep;
if (downloadSpawnInfo.elapsedSinceSpawn >= downloadSpawnInfo.period) {
downloadSpawnInfo.elapsedSinceSpawn = 0;
if (activeDownloads.Count < downloadSpawnInfo.maximumAmount) {
DownloadEntity download = downloadPool.Retrieve();
download.Size = (int)(downloadSpawnInfo.generalSize + ((random.NextDouble() - 1/2f) * 2f * downloadSpawnInfo.sizeRange));
download.X = (float)(random.NextDouble() * (Game.WIDTH_UNITS - download.Width));
download.Y = (float)(downloadSafeMargin + random.NextDouble() * (Game.HEIGHT_UNITS - 2 * downloadSafeMargin));
download.mesh.X = download.X;
download.mesh.Y = download.Y;
download.stepSize = downloadSpawnInfo.stepSize;
download.health = downloadSpawnInfo.health;
activeDownloads.Add(download);
}
}
for (int i = 0; i < activeDownloads.Count; i++)
{
DownloadEntity download = activeDownloads[i];
download.timeElapsed.Value += (float)timeStep;
if (Mouse.RightButtonPressed) {
Vector2 rightMousePos;
rightMousePos.X = widthFactor * Mouse.X;
rightMousePos.Y = heightFactor * (viewHeight - Mouse.Y);
if (download.HitBox.Contains(rightMousePos)) {
download.Input(rightMousePos.X - download.X);
}
}
if (download.progressValue.Value >= download.Width)
{
downloadPool.Release(download);
activeDownloads.RemoveAt(i);
i--;
Console.WriteLine("YAY");
continue;
}
if (download.timeElapsed.Value >= download.health)
{
downloadPool.Release(download);
activeDownloads.RemoveAt(i);
i--;
Console.WriteLine("AW");
continue;
}
}
#endregion
#region WarningCleanup
for (int i = 0; i < activeWarnings.Count; i++)
{
@ -197,7 +268,8 @@ namespace SkinnerBox.States.Gameplay
CalculateScaleFactors(vw, vh);
}
private void CalculateScaleFactors(float width, float height) {
private void CalculateScaleFactors(int width, int height) {
viewHeight = height;
this.widthFactor = Game.WIDTH_UNITS * (1f / width);
this.heightFactor = Game.HEIGHT_UNITS * (1f / height);
}

View File

@ -1,21 +0,0 @@
namespace SkinnerBox.States.Gameplay
{
public struct SpawnInfo
{
public float timeElapsed;
public int period;
public int perSpawn;
public float batchLocation;
public float distanceBetween;
public float speed;
public SpawnInfo(int period, int perSpawn, float location, float speed, float distance) {
timeElapsed = 0;
this.period = period;
this.perSpawn = perSpawn;
this.batchLocation = location;
this.distanceBetween = distance;
this.speed = speed;
}
}
}

View File

@ -69,6 +69,8 @@ namespace SkinnerBox.States.Main
this.assets.Load("serverunit.png");
this.assets.Load("packet.png");
this.assets.Load("warning.png");
this.assets.Load("downloadbar.png");
this.assets.Load("drag.png");
//Set up title TTF
this.titleFont = new BitmapFont("resources/BigShouldersDisplay-Regular.ttf", textureSizes: 512);

View File

@ -18,6 +18,12 @@ namespace SkinnerBox.Utilities.Gameplay
this.value = value;
}
}
public float DesignatedValue {
get {
return value;
}
}
public void InterpolatePosition(float delta)
{
this.current += (value - current) * delta;

BIN
resources/downloadbar.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

BIN
resources/drag.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 644 B