Added DDOS entity. Tuned values. Game Over now restarts.

This commit is contained in:
Harrison Deng 2020-07-12 03:33:09 -05:00
parent 0139832d19
commit 0633e7880f
6 changed files with 152 additions and 25 deletions

52
Entities/DDOSEntity.cs Normal file
View File

@ -0,0 +1,52 @@
using SlatedGameToolkit.Framework.Graphics.Textures;
using SlatedGameToolkit.Framework.Utilities.Collections.Pooling;
using System.Drawing;
namespace SkinnerBox.Entities
{
public class DDOSEntity : Entity, IPoolable
{
private float speed;
public DDOSEntity(ITexture texture) : base(texture)
{
Width = 0.5f;
this.Color = Color.Red;
}
public void Initialize(float xPos, float length, float speed, float delay)
{
Height = length;
X = xPos;
this.mesh.X = X;
this.speed = speed;
this.Y += delay * speed;
mesh.Y = Y;
}
public void Update(float delta) {
Y -= speed * delta;
}
public void Reset()
{
Height = 0;
Y = Game.HEIGHT_UNITS;
mesh.Y = Y;
speed = 0;
}
}
public struct DDOSSPawnInfo
{
public float speed;
public float interval;
public float intervalDeviation;
public float timeRemaining;
public DDOSSPawnInfo(float speed, float interval, float intervalDeviation, float timeRemaining)
{
this.speed = speed;
this.interval = interval;
this.intervalDeviation = intervalDeviation;
this.timeRemaining = timeRemaining;
}
}
}

View File

@ -9,7 +9,7 @@ namespace SkinnerBox.Entities
public class DownloadEntity : Entity, IPoolable public class DownloadEntity : Entity, IPoolable
{ {
private readonly float unitSize = 1/2f; private readonly float unitSize = 1/2f;
private readonly float unitPerProgressTexture = 0.5f; private readonly float unitPerProgressTexture = 0.4f;
public float stepSize; public float stepSize;
public TransitionValue progressValue; public TransitionValue progressValue;
public RectangleMesh progressMesh; public RectangleMesh progressMesh;
@ -53,7 +53,7 @@ namespace SkinnerBox.Entities
public void Reset() public void Reset()
{ {
Size = 1; Size = 1;
progressValue.HardSet(0); progressValue.HardSet(0.1f);
timeElapsed.HardSet(0); timeElapsed.HardSet(0);
stepSize = 0; stepSize = 0;
upTime = 0; upTime = 0;

View File

@ -1,9 +1,11 @@
using System; using System;
using System.Drawing; using System.Drawing;
using System.Numerics; using System.Numerics;
using SDL2;
using SlatedGameToolkit.Framework.AssetSystem; using SlatedGameToolkit.Framework.AssetSystem;
using SlatedGameToolkit.Framework.Graphics.Render; using SlatedGameToolkit.Framework.Graphics.Render;
using SlatedGameToolkit.Framework.Graphics.Text; using SlatedGameToolkit.Framework.Graphics.Text;
using SlatedGameToolkit.Framework.Input.Devices;
using SlatedGameToolkit.Framework.StateSystem; using SlatedGameToolkit.Framework.StateSystem;
using SlatedGameToolkit.Framework.StateSystem.States; using SlatedGameToolkit.Framework.StateSystem.States;
@ -35,11 +37,13 @@ namespace SkinnerBox.States
titleFont.PrepareCharacterGroup("GameOvr!".ToCharArray()); titleFont.PrepareCharacterGroup("GameOvr!".ToCharArray());
this.font.PixelHeight = 48; this.font.PixelHeight = 48;
font.PrepareCharacterGroup("01234567890.Your Stats: Score,ServerUp-timedownloadservedpacketssentWebsitePDRLN%que".ToCharArray()); font.PrepareCharacterGroup("01234567890.Your Stats: Score,ServerUp-timedownloadservedpacketssentWebsitePDRLN%que".ToCharArray());
Keyboard.keyboardUpdateEvent += KeyChanged;
return true; return true;
} }
public bool Deactivate() public bool Deactivate()
{ {
Keyboard.keyboardUpdateEvent -= KeyChanged;
return true; return true;
} }
@ -66,7 +70,7 @@ namespace SkinnerBox.States
font.WriteLine(renderer, 1.95f, Game.HEIGHT_UNITS * 0.46f, "Packets Received: " + totalPackets + " Packet Loss: " + Math.Round((100f * (1f - ((float) packetsReceived / totalPackets))), 1) + "%", Color.Black); font.WriteLine(renderer, 1.95f, Game.HEIGHT_UNITS * 0.46f, "Packets Received: " + totalPackets + " Packet Loss: " + Math.Round((100f * (1f - ((float) packetsReceived / totalPackets))), 1) + "%", Color.Black);
font.WriteLine(renderer, 1.95f, Game.HEIGHT_UNITS * 0.39f, "Downloads Served: " + downloadsServed, Color.Black); font.WriteLine(renderer, 1.95f, Game.HEIGHT_UNITS * 0.39f, "Downloads Served: " + downloadsServed, Color.Black);
font.WriteLine(renderer, 1.95f, Game.HEIGHT_UNITS * 0.32f, "Downloads Requested: " + totalDownloads, Color.Black); font.WriteLine(renderer, 1.95f, Game.HEIGHT_UNITS * 0.32f, "Downloads Requested: " + totalDownloads, Color.Black);
font.WriteLine(renderer, 1.5f, 1.5f, "Press space to reset...", Color.Black);
renderer.End(); renderer.End();
} }
@ -82,5 +86,11 @@ namespace SkinnerBox.States
public void Update(double timeStep) public void Update(double timeStep)
{ {
} }
public void KeyChanged(SDL.SDL_Keycode keycode, bool pressed) {
if (keycode == SDL.SDL_Keycode.SDLK_SPACE && !pressed) {
manager.ChangeState("Main");
}
}
} }
} }

View File

@ -53,6 +53,11 @@ namespace SkinnerBox.States
private const float downloadSafeMargin = 1.5f; private const float downloadSafeMargin = 1.5f;
#endregion #endregion
//DDOS entities.
private ObjectPool<DDOSEntity> ddosPool;
private List<DDOSEntity> activeDDOS = new List<DDOSEntity>();
private DDOSSPawnInfo dDOSSpawnInfo;
#region PlayerStats #region PlayerStats
private int speedBoost = 0; private int speedBoost = 0;
private int bandwithBoost = 0; private int bandwithBoost = 0;
@ -76,6 +81,7 @@ namespace SkinnerBox.States
packetPool = new ObjectPool<PacketEntity>(CreatePacket); packetPool = new ObjectPool<PacketEntity>(CreatePacket);
warningPool = new ObjectPool<WarningEntity>(createWarning); warningPool = new ObjectPool<WarningEntity>(createWarning);
downloadPool = new ObjectPool<DownloadEntity>(createDownload); downloadPool = new ObjectPool<DownloadEntity>(createDownload);
ddosPool = new ObjectPool<DDOSEntity>(createDDOS);
this.font = font; this.font = font;
this.gameOverState = gameOverState; this.gameOverState = gameOverState;
} }
@ -91,13 +97,14 @@ namespace SkinnerBox.States
speedMesh = new RectangleMesh(bandwithMesh.Bounds, (ITexture)assets["ram.png"], Color.White); speedMesh = new RectangleMesh(bandwithMesh.Bounds, (ITexture)assets["ram.png"], Color.White);
random = new Random(); random = new Random();
packetSpawnInfo = new PacketSpawnInfo(2, 1, (float)(random.NextDouble() * Game.WIDTH_UNITS), 1f, 0.2f, 2f); packetSpawnInfo = new PacketSpawnInfo(2, 1, (float)(random.NextDouble() * Game.WIDTH_UNITS), 1f, 0.2f, 0.75f);
downloadSpawnInfo = new DownloadSpawnInfo(4, 6, 3, 1, 4, 2); downloadSpawnInfo = new DownloadSpawnInfo(4, 6, 3, 1, 4, 2);
dDOSSpawnInfo = new DDOSSPawnInfo(28, 3f, 2f, 245f);
score = 0; score = 0;
timeElapsed.HardSet(0); timeElapsed.HardSet(0);
server.Size = 4; server.Speed = 4;
bandwithBoost = server.Size; bandwithBoost = 0;
speedBoost = 0; speedBoost = 3;
stability = totalStability; stability = totalStability;
packetsReceived = 0; packetsReceived = 0;
totalPackets = 0; totalPackets = 0;
@ -121,6 +128,10 @@ namespace SkinnerBox.States
return new DownloadEntity((Texture)assets["drag.png"], (Texture)assets["downloadbar.png"]); return new DownloadEntity((Texture)assets["drag.png"], (Texture)assets["downloadbar.png"]);
} }
public DDOSEntity createDDOS() {
return new DDOSEntity((Texture)assets["beam.png"]);
}
public bool Deactivate() public bool Deactivate()
{ {
Keyboard.keyboardUpdateEvent -= KeyInputListener; Keyboard.keyboardUpdateEvent -= KeyInputListener;
@ -168,7 +179,12 @@ namespace SkinnerBox.States
} }
#endregion #endregion
renderer.Draw(server); renderer.Draw(server);
#region DDOSRender
foreach (DDOSEntity ddos in activeDDOS)
{
renderer.Draw(ddos);
}
#endregion
#region StatusRender #region StatusRender
for (int i = 0; i < bandwithBoost + speedBoost; i++) for (int i = 0; i < bandwithBoost + speedBoost; i++)
{ {
@ -220,7 +236,7 @@ namespace SkinnerBox.States
for(int i = 0; i < packetSpawnInfo.perSpawn; i++) { for(int i = 0; i < packetSpawnInfo.perSpawn; i++) {
PacketEntity packet = packetPool.Retrieve(); PacketEntity packet = packetPool.Retrieve();
packet.CenterX = packetSpawnInfo.batchLocation; packet.CenterX = packetSpawnInfo.batchLocation;
packet.Y = i * packet.Height + packetSpawnInfo.range + Game.HEIGHT_UNITS + packetSpawnInfo.speed * (2/3f); packet.Y = i * packet.Height + packetSpawnInfo.range + Game.HEIGHT_UNITS + packetSpawnInfo.speed;
packet.velocity = packetSpawnInfo.speed; packet.velocity = packetSpawnInfo.speed;
packet.Color = Color.Blue; packet.Color = Color.Blue;
totalPackets++; totalPackets++;
@ -230,12 +246,12 @@ namespace SkinnerBox.States
//Spawn Warning //Spawn Warning
WarningEntity warning = warningPool.Retrieve(); WarningEntity warning = warningPool.Retrieve();
warning.CenterX = packetSpawnInfo.batchLocation; warning.CenterX = packetSpawnInfo.batchLocation;
warning.LifeTime = packetSpawnInfo.interval * (2/3f); warning.LifeTime = 1f;
warning.Y = Game.HEIGHT_UNITS - warning.Height; warning.Y = Game.HEIGHT_UNITS - warning.Height;
activeWarnings.Add(warning); activeWarnings.Add(warning);
//Prepare next batch //Prepare next batch
float change = (float)((float)(random.NextDouble() - 1/2f) * packetSpawnInfo.jumpDistance * 2); float change = (float)((random.NextDouble() - 1/2f) * packetSpawnInfo.jumpDistance * 2);
if (packetSpawnInfo.batchLocation + change > Game.WIDTH_UNITS - packetSafeMargin || packetSpawnInfo.batchLocation + change < packetSafeMargin) { if (packetSpawnInfo.batchLocation + change > Game.WIDTH_UNITS - packetSafeMargin || packetSpawnInfo.batchLocation + change < packetSafeMargin) {
packetSpawnInfo.batchLocation -= change; packetSpawnInfo.batchLocation -= change;
} else { } else {
@ -261,7 +277,7 @@ namespace SkinnerBox.States
if (packet.Y >= Game.HEIGHT_UNITS && packet.velocity < 0) { if (packet.Y >= Game.HEIGHT_UNITS && packet.velocity < 0) {
score += -2 * packet.velocity; score += -2 * packet.velocity;
packetsReceived++; packetsReceived++;
stability += 0.05f; stability += 0.025f;
packetPool.Release(packet); packetPool.Release(packet);
activePackets.RemoveAt(i); activePackets.RemoveAt(i);
i--; i--;
@ -276,7 +292,7 @@ namespace SkinnerBox.States
if (activeDownloads.Count < downloadSpawnInfo.maximumAmount) { if (activeDownloads.Count < downloadSpawnInfo.maximumAmount) {
DownloadEntity download = downloadPool.Retrieve(); DownloadEntity download = downloadPool.Retrieve();
download.Size = (int)(downloadSpawnInfo.generalSize + ((random.NextDouble() - 1/2f) * 2f * downloadSpawnInfo.sizeRange)); download.Size = (int)(downloadSpawnInfo.generalSize + ((random.NextDouble() - 1/2f) * 2f * downloadSpawnInfo.sizeRange));
download.X = (float)(random.NextDouble() * (Game.WIDTH_UNITS - download.Width)); download.X = (float)(random.NextDouble() * (Game.WIDTH_UNITS - download.Width - 2 * downloadSafeMargin) + downloadSafeMargin);
download.Y = (float)(downloadSafeMargin + random.NextDouble() * (Game.HEIGHT_UNITS - 2 * downloadSafeMargin)); download.Y = (float)(downloadSafeMargin + random.NextDouble() * (Game.HEIGHT_UNITS - 2 * downloadSafeMargin));
download.stepSize = downloadSpawnInfo.stepSize; download.stepSize = downloadSpawnInfo.stepSize;
download.upTime = downloadSpawnInfo.upTime; download.upTime = downloadSpawnInfo.upTime;
@ -326,6 +342,50 @@ namespace SkinnerBox.States
} }
} }
#endregion #endregion
#region DDOSUpdate
dDOSSpawnInfo.timeRemaining -= (float)timeStep;
if (dDOSSpawnInfo.timeRemaining <= 0) {
dDOSSpawnInfo.timeRemaining = (float)(dDOSSpawnInfo.interval + random.NextDouble() * dDOSSpawnInfo.intervalDeviation);
DDOSEntity ddos = ddosPool.Retrieve();
ddos.Initialize(server.CenterX, 1.5f * Game.HEIGHT_UNITS, dDOSSpawnInfo.speed, 2f);
activeDDOS.Add(ddos);
for (int i = 0; i < 4; i++)
{
//Spawn Warning
WarningEntity warning = warningPool.Retrieve();
warning.CenterX = ddos.CenterX;
warning.LifeTime = 1f;
warning.Y = Game.HEIGHT_UNITS - i * (warning.Height + 1f);
activeWarnings.Add(warning);
}
}
for (int i = 0; i < activeDDOS.Count; i++)
{
DDOSEntity ddos = activeDDOS[i];
ddos.Update((float)timeStep);
if (ddos.HitBox.IntersectsWith(server.HitBox)) {
stability -= (float) (10f * timeStep);
}
for (int p = 0; p < activePackets.Count; p++)
{
PacketEntity packet = activePackets[p];
if (packet.velocity > 0 && ddos.HitBox.IntersectsWith(packet.HitBox)) {
activePackets.RemoveAt(p);
packetPool.Release(packet);
p--;
}
}
if (ddos.Y <= 0 - ddos.Height) {
ddosPool.Release(ddos);
activeDDOS.RemoveAt(i);
i--;
continue;
}
}
#endregion
#region WarningCleanup #region WarningCleanup
for (int i = 0; i < activeWarnings.Count; i++) for (int i = 0; i < activeWarnings.Count; i++)
{ {
@ -342,27 +402,29 @@ namespace SkinnerBox.States
//packet curve //packet curve
packetSpawnInfo.perSpawn = (int)(0.5f * (Math.Pow(timeElapsed.Value, 0.5f) + 1)); packetSpawnInfo.perSpawn = (int)(0.5f * (Math.Pow(timeElapsed.Value, 0.5f) + 1));
packetSpawnInfo.speed = (float)((0.025f * Math.Pow(timeElapsed.Value, 1.1f)) + 1f); packetSpawnInfo.speed = (float)((0.025f * Math.Pow(timeElapsed.Value, 1.1f)) + 1f);
if (packetSpawnInfo.range < 4) { if (packetSpawnInfo.jumpDistance < 2f) {
packetSpawnInfo.range = (float)(0.1f * (Math.Pow(timeElapsed.Value, 1.15f)) + 2f); packetSpawnInfo.jumpDistance = (float)(0.0018f * (Math.Pow(timeElapsed.Value, 1.1f)) + 0.75f);
if (packetSpawnInfo.range > 4) packetSpawnInfo.range = 4; if (packetSpawnInfo.jumpDistance > 2f) packetSpawnInfo.jumpDistance = 2f;
} }
if (packetSpawnInfo.interval > 0.3f) { if (packetSpawnInfo.interval > 0f) {
packetSpawnInfo.interval = (float) (-0.0055 * timeElapsed.Value) + 2f; packetSpawnInfo.interval = (float) (-0.0075 * timeElapsed.Value) + 2f;
if (packetSpawnInfo.interval < 0.3f) packetSpawnInfo.interval = 0.3f; if (packetSpawnInfo.interval < 0f) packetSpawnInfo.interval = 0f;
} }
//download curve //download curve
if (downloadSpawnInfo.maximumAmount < 4) { if (downloadSpawnInfo.maximumAmount < 5) {
downloadSpawnInfo.maximumAmount = (int)(0.02f * timeElapsed.Value + 1); downloadSpawnInfo.maximumAmount = (int)(0.02f * timeElapsed.Value + 1);
} }
if (downloadSpawnInfo.upTime > 3) { if (downloadSpawnInfo.upTime > 3) {
downloadSpawnInfo.upTime = (float)(8 + (-0.1f * Math.Pow(timeElapsed.Value, 0.8f))); downloadSpawnInfo.upTime = (float)(8 + (-0.1f * Math.Pow(timeElapsed.Value, 0.8f)));
if (downloadSpawnInfo.upTime < 3) downloadSpawnInfo.upTime = 3; if (downloadSpawnInfo.upTime < 3) downloadSpawnInfo.upTime = 3;
} }
if (downloadSpawnInfo.period > 1.5f) { if (downloadSpawnInfo.period > 1f) {
downloadSpawnInfo.period = (float) (-0.006 * timeElapsed.Value) + 4; downloadSpawnInfo.period = (float) (-0.006 * timeElapsed.Value) + 4;
if (packetSpawnInfo.interval < 1.5f) packetSpawnInfo.interval = 1.5f; if (packetSpawnInfo.interval < 1f) packetSpawnInfo.interval = 1f;
} }
//ddos curve
#endregion #endregion
#region BoundaryChecking #region BoundaryChecking
if (stability > totalStability) { if (stability > totalStability) {

View File

@ -33,7 +33,7 @@ namespace SkinnerBox.States
this.titleFont.PixelHeight = 40; this.titleFont.PixelHeight = 40;
this.titleFont.PrepareCharacterGroup("By: Reslate".ToCharArray()); this.titleFont.PrepareCharacterGroup("By: Reslate".ToCharArray());
boldFont.PixelHeight = 60; boldFont.PixelHeight = 60;
boldFont.PrepareCharacterGroup("Press any key to start...".ToCharArray()); boldFont.PrepareCharacterGroup("Press space to start...".ToCharArray());
return true; return true;
} }
@ -87,8 +87,11 @@ namespace SkinnerBox.States
assets.Load("drag.png"); assets.Load("drag.png");
assets.Load("usage.png"); assets.Load("usage.png");
assets.Load("health.png"); assets.Load("health.png");
assets.Load("beam.png");
assets.Load("ram.png"); assets.Load("ram.png");
Texture downloadBarTex = (Texture)assets["downloadbar.png"];
downloadBarTex.SetNearestFilter(true, true);
//Set up bold TTF //Set up bold TTF
boldFont = new BitmapFont("resources/BigShouldersDisplay-Black.ttf", textureSizes: 512); boldFont = new BitmapFont("resources/BigShouldersDisplay-Black.ttf", textureSizes: 512);
@ -114,7 +117,7 @@ namespace SkinnerBox.States
renderer.Draw(serverUnit); renderer.Draw(serverUnit);
this.boldFont.WriteLine(renderer, 1.15f, Game.HEIGHT_UNITS / 2, "Press any key to start...", Color.Black); this.boldFont.WriteLine(renderer, 1.15f, Game.HEIGHT_UNITS / 2, "Press space to start...", Color.Black);
renderer.End(); renderer.End();
} }
@ -124,7 +127,7 @@ namespace SkinnerBox.States
public void KeyInput(SDL.SDL_Keycode keys, bool pressed) public void KeyInput(SDL.SDL_Keycode keys, bool pressed)
{ {
if (pressed) if (!pressed && keys == SDL.SDL_Keycode.SDLK_SPACE)
{ {
manager.ChangeState("GamePlayState"); manager.ChangeState("GamePlayState");
} }

BIN
resources/beam.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.8 KiB