Skip to content

Commit

Permalink
feat: added linear falloff of .05f per second
Browse files Browse the repository at this point in the history
  • Loading branch information
FeroxFoxxo committed Aug 4, 2024
1 parent 55b3416 commit bc90c41
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 29 deletions.
30 changes: 8 additions & 22 deletions Server.Web/Models/ResourcesModel.cs
Original file line number Diff line number Diff line change
@@ -1,28 +1,14 @@
namespace Server.Web.Models;
public class ResourcesModel
public class SendResourceModel
{
public int Oxygen { get; set; } = UpperBounds;
public int Electricity { get; set; } = UpperBounds;
public int Fuel { get; set; } = UpperBounds;
public int Water { get; set; } = UpperBounds;
private readonly ResourcesModel _model;

public const int UpperBounds = 100;
public SendResourceModel(ResourcesModel model) => _model = model;

public bool Depleated() =>
Oxygen <= 0 || Electricity <= 0 || Fuel <= 0 || Water <= 0;
public SendResourceModel() { }

public void EnsureBounds()
{
if (Oxygen > UpperBounds)
Oxygen = UpperBounds;

if (Fuel > UpperBounds)
Fuel = UpperBounds;

if (Water > UpperBounds)
Water = UpperBounds;

if (Electricity > UpperBounds)
Electricity = UpperBounds;
}
public int Oxygen { get => (int)Math.Floor(_model.Oxygen); set => _model.Oxygen = value; }
public int Electricity { get => (int)Math.Floor(_model.Electricity); set => _model.Electricity = value; }
public int Fuel { get => (int)Math.Floor(_model.Fuel); set => _model.Fuel = value; }
public int Water { get => (int)Math.Floor(_model.Water); set => _model.Water = value; }
}
28 changes: 28 additions & 0 deletions Server.Web/Models/SendResourceModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
namespace Server.Web.Models;
public class ResourcesModel
{
public float Oxygen { get; set; } = UpperBounds;
public float Electricity { get; set; } = UpperBounds;
public float Fuel { get; set; } = UpperBounds;
public float Water { get; set; } = UpperBounds;

public const float UpperBounds = 100;

public bool Depleated() =>
Oxygen <= 0 || Electricity <= 0 || Fuel <= 0 || Water <= 0;

public void EnsureBounds()
{
if (Oxygen > UpperBounds)
Oxygen = UpperBounds;

if (Fuel > UpperBounds)
Fuel = UpperBounds;

if (Water > UpperBounds)
Water = UpperBounds;

if (Electricity > UpperBounds)
Electricity = UpperBounds;
}
}
2 changes: 1 addition & 1 deletion Server.Web/Models/StateModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@ public Dictionary<int, RoleModel> Roles
set => throw new ArgumentException("Cannot set roles via JSON deserialization!");
}

public ResourcesModel Resources { get; set; }
public SendResourceModel Resources { get; set; }
}
16 changes: 10 additions & 6 deletions Server.Web/World/Game.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@ public class Game

public GameState state = GameState.Lobby;

public float LinearDecrease = 0;

public StateModel StateModel => new ()
{
InternalRoles = IdToPlayer.Where(x => x.Key > 0).ToDictionary(x => x.Value.Role, x => x.Value),
InternalGameState = state,
Resources = Resources,
Resources = new SendResourceModel(Resources),
CurrentSequence = CurrentSequence
};

Expand Down Expand Up @@ -221,10 +223,12 @@ public static async Task DecreaseResources(object obj)
{
var game = (Game) obj;

game.Resources.Water--;
game.Resources.Fuel--;
game.Resources.Electricity--;
game.Resources.Oxygen--;
game.Resources.Water -= 1 + game.LinearDecrease;
game.Resources.Fuel -= 1 + game.LinearDecrease;
game.Resources.Electricity -= 1 + game.LinearDecrease;
game.Resources.Oxygen -= 1 + game.LinearDecrease;

game.LinearDecrease += .05f;

await game.CheckAndSendState();
}
Expand All @@ -248,7 +252,7 @@ public async Task CheckAndSendState()

public async Task EndGame()
{
await Group.WriteMessage(new MessageModel("Game has been completed!", Color.Gold));
await Group.WriteMessage(new MessageModel("Game has ended! It looks like the spaceship went <b>boom!</b>", Color.LightSalmon));

await Group.GameEnd();

Expand Down

0 comments on commit bc90c41

Please sign in to comment.