Polished, began adding score, added warning to download.
This commit is contained in:
parent
fd8317c613
commit
dce1c03ba9
@ -14,7 +14,7 @@ namespace SkinnerBox.Entities
|
||||
public TransitionValue progressValue;
|
||||
public RectangleMesh progressMesh;
|
||||
public TransitionValue timeElapsed;
|
||||
public float health;
|
||||
public float upTime;
|
||||
int size;
|
||||
public int Size {
|
||||
get {
|
||||
@ -30,7 +30,7 @@ namespace SkinnerBox.Entities
|
||||
public override RectangleF HitBox {
|
||||
get {
|
||||
RectangleF rect = base.HitBox;
|
||||
rect.Width = Width * size;
|
||||
rect.Width = Width + 0.5f;
|
||||
return rect;
|
||||
}
|
||||
}
|
||||
@ -53,15 +53,11 @@ namespace SkinnerBox.Entities
|
||||
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;
|
||||
upTime = 0;
|
||||
Color = Color.Cyan;
|
||||
UpdateProgressMesh();
|
||||
}
|
||||
|
||||
@ -75,11 +71,13 @@ namespace SkinnerBox.Entities
|
||||
public override void InterpolatePosition(float delta) {
|
||||
progressValue.InterpolatePosition(delta);
|
||||
timeElapsed.InterpolatePosition(delta);
|
||||
float prog = timeElapsed.Value / health;
|
||||
float prog = timeElapsed.Value / upTime;
|
||||
if (prog > 1) prog = 1;
|
||||
if (prog < 0) prog = 0;
|
||||
this.Color = Color.FromArgb((int)(byte.MaxValue * (1f - prog)), Color);
|
||||
progressMesh.Color = Color.FromArgb((int)(byte.MaxValue * (1f - prog)), progressMesh.Color);
|
||||
UpdateProgressMesh();
|
||||
base.InterpolatePosition(delta);
|
||||
}
|
||||
}
|
||||
|
||||
@ -87,7 +85,7 @@ namespace SkinnerBox.Entities
|
||||
{
|
||||
public float period;
|
||||
public float elapsedSinceSpawn;
|
||||
public float health;
|
||||
public float upTime;
|
||||
public float stepSize;
|
||||
public int maximumAmount;
|
||||
public int generalSize;
|
||||
@ -97,7 +95,7 @@ namespace SkinnerBox.Entities
|
||||
{
|
||||
this.period = cooldown;
|
||||
this.elapsedSinceSpawn = 0;
|
||||
this.health = health;
|
||||
this.upTime = health;
|
||||
this.stepSize = stepSize;
|
||||
this.maximumAmount = maxAmount;
|
||||
this.sizeRange = sizeRange;
|
||||
|
@ -7,6 +7,8 @@ namespace SkinnerBox.Entities
|
||||
{
|
||||
public class ServerEntity : Entity
|
||||
{
|
||||
public const float MIN_SPEED = 1f;
|
||||
public const float SPEED_STEP = 1f;
|
||||
private readonly float length = 4/8f;
|
||||
public float Speed { get; set; }
|
||||
public override float CenterX {
|
||||
@ -35,7 +37,7 @@ namespace SkinnerBox.Entities
|
||||
public override RectangleF HitBox {
|
||||
get {
|
||||
RectangleF hitbox = base.HitBox;
|
||||
hitbox.Width = hitbox.Width * size;
|
||||
hitbox.Width = Width;
|
||||
return hitbox;
|
||||
}
|
||||
}
|
||||
@ -46,7 +48,7 @@ namespace SkinnerBox.Entities
|
||||
|
||||
Size = 1;
|
||||
|
||||
this.Speed = 2f;
|
||||
this.Speed = MIN_SPEED;
|
||||
|
||||
CenterX = initialX;
|
||||
mesh.X = X;
|
||||
|
@ -12,8 +12,8 @@ namespace SkinnerBox.Entities
|
||||
public TransitionValue aliveTime;
|
||||
|
||||
public WarningEntity(ITexture texture) : base(texture) {
|
||||
this.Width = 2;
|
||||
this.Height = 2;
|
||||
this.Width = 1f;
|
||||
this.Height = 1f;
|
||||
Reset();
|
||||
}
|
||||
public void Reset()
|
||||
@ -21,7 +21,7 @@ namespace SkinnerBox.Entities
|
||||
LifeTime = 0;
|
||||
X = 0 - Width;
|
||||
mesh.X = X;
|
||||
Y = Game.HEIGHT_UNITS - Height;
|
||||
Y = - Height;
|
||||
mesh.Y = Y;
|
||||
aliveTime.HardSet(0);
|
||||
this.Color = Color.Red;
|
||||
|
3
Game.cs
3
Game.cs
@ -1,5 +1,7 @@
|
||||
using SkinnerBox.States.Main;
|
||||
using SkinnerBox.Utilities;
|
||||
using SlatedGameToolkit.Framework;
|
||||
using SlatedGameToolkit.Framework.Logging;
|
||||
|
||||
namespace SkinnerBox
|
||||
{
|
||||
@ -11,6 +13,7 @@ namespace SkinnerBox
|
||||
{
|
||||
GameEngine.targetFPS = 0;
|
||||
GameEngine.UpdatesPerSecond = 20;
|
||||
Logger.AddLogListener(new ConsoleLogger());
|
||||
GameEngine.Ignite(new MenuState());
|
||||
}
|
||||
|
||||
|
@ -13,6 +13,7 @@ using SlatedGameToolkit.Framework.StateSystem;
|
||||
using SlatedGameToolkit.Framework.StateSystem.States;
|
||||
using SlatedGameToolkit.Framework.Utilities.Collections.Pooling;
|
||||
using SlatedGameToolkit.Framework.Utilities;
|
||||
using SlatedGameToolkit.Framework.Graphics.Text;
|
||||
|
||||
namespace SkinnerBox.States.Gameplay
|
||||
{
|
||||
@ -21,37 +22,52 @@ namespace SkinnerBox.States.Gameplay
|
||||
private MeshBatchRenderer renderer;
|
||||
private AssetManager assets;
|
||||
private StateManager stateManager;
|
||||
private BitmapFont font;
|
||||
private Random random;
|
||||
|
||||
//Cursor information
|
||||
private float widthFactor, heightFactor;
|
||||
#region CursorVars
|
||||
private float cursorWidthScale, cursorHeightScale;
|
||||
private float serverTargetPos; //Last left click position
|
||||
private int viewHeight; // The viewports height for inverting Y value.
|
||||
#endregion
|
||||
|
||||
//Entities
|
||||
private ServerEntity server;
|
||||
#region EntitiesVariables
|
||||
private ServerEntity server; //The player
|
||||
|
||||
// Warning entities
|
||||
private ObjectPool<WarningEntity> warningPool;
|
||||
private List<WarningEntity> activeWarnings = new List<WarningEntity>();
|
||||
|
||||
// Packet entities
|
||||
private ObjectPool<PacketEntity> packetPool;
|
||||
private List<PacketEntity> activePackets = new List<PacketEntity>();
|
||||
private PacketSpawnInfo packetSpawnInfo;
|
||||
private const float packetSafeMargin = 1/2f;
|
||||
|
||||
//Download entities.
|
||||
private ObjectPool<DownloadEntity> downloadPool;
|
||||
private List<DownloadEntity> activeDownloads = new List<DownloadEntity>();
|
||||
private DownloadSpawnInfo downloadSpawnInfo;
|
||||
private const float downloadSafeMargin = 1.5f;
|
||||
private int viewHeight;
|
||||
#endregion
|
||||
|
||||
#region PlayerStats
|
||||
private readonly int totalUsage = 3;
|
||||
private int usedUsage = 0;
|
||||
private RectangleMesh usageMesh;
|
||||
private int score;
|
||||
private float timeElapsed;
|
||||
#endregion
|
||||
|
||||
public GamePlayState(MeshBatchRenderer renderer, AssetManager asset)
|
||||
public GamePlayState(MeshBatchRenderer renderer, AssetManager asset, BitmapFont font)
|
||||
{
|
||||
this.assets = asset;
|
||||
this.renderer = renderer;
|
||||
packetPool = new ObjectPool<PacketEntity>(CreatePacket);
|
||||
warningPool = new ObjectPool<WarningEntity>(createWarning);
|
||||
downloadPool = new ObjectPool<DownloadEntity>(createDownload);
|
||||
this.font = font;
|
||||
font.PrepareCharacterGroup("score:0123456789timlapsd".ToCharArray());
|
||||
}
|
||||
|
||||
public bool Activate()
|
||||
@ -60,6 +76,7 @@ namespace SkinnerBox.States.Gameplay
|
||||
Mouse.mouseUpdateEvent += MouseInput;
|
||||
serverTargetPos = 0.5f * Game.WIDTH_UNITS;
|
||||
server = new ServerEntity((Texture)assets["serverunit.png"], serverTargetPos, 0.1f);
|
||||
usageMesh = new RectangleMesh(new RectangleF(0, Game.HEIGHT_UNITS - 0.75f, 0.5f, 0.5f), (ITexture)assets["usage.png"], Color.White);
|
||||
random = new Random();
|
||||
|
||||
packetSpawnInfo = new PacketSpawnInfo(2, 1, (float)(random.NextDouble() * Game.WIDTH_UNITS), 1f, 0.2f, 3f);
|
||||
@ -102,25 +119,45 @@ namespace SkinnerBox.States.Gameplay
|
||||
int vw, vh, vx, vy;
|
||||
WindowContextsManager.CurrentGL.GetViewport(out vx, out vy, out vw, out vh);
|
||||
CalculateScaleFactors(vw, vh);
|
||||
|
||||
}
|
||||
|
||||
public void Render(double delta)
|
||||
{
|
||||
renderer.Begin(Matrix4x4.Identity, delta);
|
||||
#region WarningRender
|
||||
foreach (WarningEntity warn in activeWarnings)
|
||||
{
|
||||
renderer.Draw(warn);
|
||||
}
|
||||
#endregion
|
||||
#region PacketRender
|
||||
foreach (PacketEntity packet in activePackets)
|
||||
{
|
||||
renderer.Draw(packet);
|
||||
}
|
||||
#endregion
|
||||
#region DownloadRender
|
||||
foreach(DownloadEntity download in activeDownloads) {
|
||||
renderer.Draw(download);
|
||||
renderer.Draw(download.progressMesh);
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
renderer.Draw(server);
|
||||
|
||||
#region StatusRender
|
||||
for (int i = 0; i < totalUsage; i++)
|
||||
{
|
||||
usageMesh.X = 0.25f + (i * (usageMesh.Width + 0.2f));
|
||||
if (i >= usedUsage) {
|
||||
usageMesh.Color = Color.Yellow;
|
||||
} else {
|
||||
usageMesh.Color = Color.White;
|
||||
}
|
||||
renderer.Draw(usageMesh);
|
||||
}
|
||||
|
||||
font.WriteLine(renderer, 0.05f, Game.HEIGHT_UNITS - 2f, "score: " + score, Color.Black);
|
||||
#endregion
|
||||
renderer.End();
|
||||
}
|
||||
|
||||
@ -128,7 +165,7 @@ namespace SkinnerBox.States.Gameplay
|
||||
{
|
||||
#region ServerUpdate
|
||||
if (Mouse.LeftButtonPressed) {
|
||||
serverTargetPos = widthFactor * Mouse.X;
|
||||
serverTargetPos = cursorWidthScale * Mouse.X;
|
||||
}
|
||||
|
||||
if (serverTargetPos < server.CenterX)
|
||||
@ -174,7 +211,7 @@ namespace SkinnerBox.States.Gameplay
|
||||
PacketEntity packet = activePackets[i];
|
||||
packet.Update(timeStep);
|
||||
if (packet.HitBox.IntersectsWith(server.HitBox) && packet.velocity > 0) {
|
||||
packet.velocity *= -2f;
|
||||
packet.velocity *= -2.5f;
|
||||
packet.Color = Color.Cyan;
|
||||
}
|
||||
if (packet.Y <= 0 - packet.Height) {
|
||||
@ -199,11 +236,16 @@ namespace SkinnerBox.States.Gameplay
|
||||
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;
|
||||
download.upTime = downloadSpawnInfo.upTime;
|
||||
activeDownloads.Add(download);
|
||||
|
||||
WarningEntity warning = warningPool.Retrieve();
|
||||
warning.CenterX = download.CenterX;
|
||||
warning.LifeTime = download.upTime * (1/3f);
|
||||
warning.Y = download.Y - warning.Height;
|
||||
warning.mesh.Y = warning.Y;
|
||||
activeWarnings.Add(warning);
|
||||
}
|
||||
}
|
||||
|
||||
@ -215,8 +257,8 @@ namespace SkinnerBox.States.Gameplay
|
||||
download.timeElapsed.Value += (float)timeStep;
|
||||
if (Mouse.RightButtonPressed) {
|
||||
Vector2 rightMousePos;
|
||||
rightMousePos.X = widthFactor * Mouse.X;
|
||||
rightMousePos.Y = heightFactor * (viewHeight - Mouse.Y);
|
||||
rightMousePos.X = cursorWidthScale * Mouse.X;
|
||||
rightMousePos.Y = cursorHeightScale * (viewHeight - Mouse.Y);
|
||||
if (download.HitBox.Contains(rightMousePos)) {
|
||||
download.Input(rightMousePos.X - download.X);
|
||||
}
|
||||
@ -227,15 +269,13 @@ namespace SkinnerBox.States.Gameplay
|
||||
downloadPool.Release(download);
|
||||
activeDownloads.RemoveAt(i);
|
||||
i--;
|
||||
Console.WriteLine("YAY");
|
||||
continue;
|
||||
}
|
||||
if (download.timeElapsed.Value >= download.health)
|
||||
if (download.timeElapsed.Value >= download.upTime)
|
||||
{
|
||||
downloadPool.Release(download);
|
||||
activeDownloads.RemoveAt(i);
|
||||
i--;
|
||||
Console.WriteLine("AW");
|
||||
continue;
|
||||
}
|
||||
}
|
||||
@ -255,6 +295,31 @@ namespace SkinnerBox.States.Gameplay
|
||||
}
|
||||
|
||||
public void KeyInputListener(SDL.SDL_Keycode keycode, bool down) {
|
||||
if (!down) return;
|
||||
if (keycode == SDL.SDL_Keycode.SDLK_a) {
|
||||
if (usedUsage > 0 && server.Size > 1) {
|
||||
usedUsage--;
|
||||
server.Size--;
|
||||
}
|
||||
}
|
||||
if (keycode == SDL.SDL_Keycode.SDLK_d) {
|
||||
if (usedUsage < totalUsage) {
|
||||
usedUsage++;
|
||||
server.Size++;
|
||||
}
|
||||
}
|
||||
if (keycode == SDL.SDL_Keycode.SDLK_s) {
|
||||
if (usedUsage > 0 && server.Speed > ServerEntity.MIN_SPEED) {
|
||||
usedUsage--;
|
||||
server.Speed -= ServerEntity.SPEED_STEP;
|
||||
}
|
||||
}
|
||||
if (keycode == SDL.SDL_Keycode.SDLK_w) {
|
||||
if (usedUsage < totalUsage) {
|
||||
usedUsage++;
|
||||
server.Speed += ServerEntity.SPEED_STEP;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void MouseInput(bool leftDown, bool rightDown, bool middle, int x, int y, int scrollX, int scrollY) {
|
||||
@ -270,8 +335,8 @@ namespace SkinnerBox.States.Gameplay
|
||||
|
||||
private void CalculateScaleFactors(int width, int height) {
|
||||
viewHeight = height;
|
||||
this.widthFactor = Game.WIDTH_UNITS * (1f / width);
|
||||
this.heightFactor = Game.HEIGHT_UNITS * (1f / height);
|
||||
this.cursorWidthScale = Game.WIDTH_UNITS * (1f / width);
|
||||
this.cursorHeightScale = Game.HEIGHT_UNITS * (1f / height);
|
||||
}
|
||||
}
|
||||
}
|
@ -53,7 +53,7 @@ namespace SkinnerBox.States.Main
|
||||
{
|
||||
this.manager = manager;
|
||||
this.manager.backgroundColour = Color.White;
|
||||
this.context = new WindowContext("You Are the Website", width: 640, height: 640, options: SDL.SDL_WindowFlags.SDL_WINDOW_HIDDEN); // Creates the window context.
|
||||
this.context = new WindowContext("You Are the Website", width: 640, height: 640, options: SDL.SDL_WindowFlags.SDL_WINDOW_HIDDEN);
|
||||
this.assets = new AssetManager();
|
||||
this.assets.DefaultPathModifier = (p) => "resources/" + p;
|
||||
this.assets.Loaders.TryAdd("png", TextureLoader.Load2DTexture);
|
||||
@ -63,14 +63,19 @@ namespace SkinnerBox.States.Main
|
||||
this.renderer = new MeshBatchRenderer(camera);
|
||||
|
||||
//Add additional states
|
||||
manager.AddState(new GamePlayState(renderer, this.assets));
|
||||
BitmapFont genericFont = new BitmapFont("resources/BigShouldersDisplay-Light.ttf");
|
||||
genericFont.PixelsPerUnitHeight = 80;
|
||||
genericFont.PixelsPerUnitWidth = 80;
|
||||
genericFont.PixelHeight = 32;
|
||||
manager.AddState(new GamePlayState(renderer, this.assets, genericFont));
|
||||
|
||||
//Load assets
|
||||
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");
|
||||
assets.Load("serverunit.png");
|
||||
assets.Load("packet.png");
|
||||
assets.Load("warning.png");
|
||||
assets.Load("downloadbar.png");
|
||||
assets.Load("drag.png");
|
||||
assets.Load("usage.png");
|
||||
|
||||
//Set up title TTF
|
||||
this.titleFont = new BitmapFont("resources/BigShouldersDisplay-Regular.ttf", textureSizes: 512);
|
||||
|
15
Utilities/ConsoleLogger.cs
Normal file
15
Utilities/ConsoleLogger.cs
Normal file
@ -0,0 +1,15 @@
|
||||
using System;
|
||||
using SlatedGameToolkit.Framework.Logging;
|
||||
|
||||
namespace SkinnerBox.Utilities
|
||||
{
|
||||
public class ConsoleLogger : ILogListener
|
||||
{
|
||||
public LogLevel Level => LogLevel.DEBUG;
|
||||
|
||||
public void LogMessage(string message, DateTime time, LogLevel level)
|
||||
{
|
||||
Console.WriteLine(string.Format("[{0}] [{1}]: {2}", time.ToString("H:mm:ss"), level, message));
|
||||
}
|
||||
}
|
||||
}
|
BIN
resources/BigShouldersDisplay-Light.ttf
Normal file
BIN
resources/BigShouldersDisplay-Light.ttf
Normal file
Binary file not shown.
Binary file not shown.
Before Width: | Height: | Size: 644 B After Width: | Height: | Size: 917 B |
BIN
resources/usage.png
Normal file
BIN
resources/usage.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.6 KiB |
Loading…
Reference in New Issue
Block a user