Skip to content

Commit

Permalink
Added game life cycle
Browse files Browse the repository at this point in the history
  • Loading branch information
ctrl-alt-d committed Aug 7, 2019
1 parent 84ce65e commit 4f5dde7
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 11 deletions.
1 change: 1 addition & 0 deletions src/blagario/AgarElements/AgarElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class AgarElement
protected static readonly Random getrandom = new Random();

public ElementType ElementType {get; protected set; }
public string Name {get; set;} = "";
public double X {set; get; }
public double Y {set; get; }
public double _Mass {set; get; }
Expand Down
4 changes: 3 additions & 1 deletion src/blagario/AgarElements/Cell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@ public Cell(Universe universe)

public override async Task Tic(int fpsTicNum) {
_Mass = _Mass * 0.999995;
if (_Mass<10) _Mass = 10;
if (_Mass>0 && _Mass<10) _Mass = 10;
await base.Tic(fpsTicNum);
}

static string[] availableColors = new string [] {"2ecc71", "3498db", "9b59b6", "f1c40f", "e67e22", "e74c3c" };

public override string CssStyle(Player c) =>
c.Cell==null
?"visibility:none":
this == c.Cell
?$@"
top: {((long)(-this.Radius*c.Zoom + c.VisibleAreaY/2)).ToString()}px ;
Expand Down
37 changes: 27 additions & 10 deletions src/blagario/AgarElements/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,28 @@ public class Player: IDisposable
public Player(Universe universe)
{
Universe = universe;
Cell = new Cell(universe);
Cell = null ; //new Cell(universe);
Universe.World.OnTicReached += OnTicEvent;
}
public string FormCss => $@"
position: absolute;
top: {XGame2World(CurrentX)}px;
right: {XGame2World(CurrentY)}px;
";

public string Name {set; get;} = "Unnamed cell";
public void Play()
{
Cell = new Cell(Universe);
Cell.Name = Name;
}

private void OnTicEvent(object o, EventArgs e) => this.Tic();

public Cell Cell {get; private set;}

public double CurrentX => Cell!=null?Cell.X:(Universe.X/2);
public double CurrentY => Cell!=null?Cell.Y:(Universe.Y/2);
public double CurrentX => Cell?.X ?? (Universe.World.X/2);
public double CurrentY => Cell?.Y ?? (Universe.World.Y/2);

/* --- */
public IJSRuntime JsRuntime {get; private set;}
Expand Down Expand Up @@ -53,7 +65,7 @@ public long XGame2World( double x )
}
public long YGame2World( double y )
{
return (long) (long) ( y * this.Zoom);
return (long) ( y * this.Zoom);
}

internal void IncreaseZoom(float v)
Expand All @@ -63,11 +75,16 @@ internal void IncreaseZoom(float v)

internal void PointTo(double bx, double by)
{
Cell.PointTo(bx, by);
Cell?.PointTo(bx, by);
}

public void Tic()
{
if ( (this.Cell?.Mass??0) == 0 )
{
this.Cell = null;
}

if (DeltaZoom<0 && Zoom<0.5)
{
DeltaZoom=0;
Expand Down Expand Up @@ -108,11 +125,11 @@ public bool OnArea(AgarElement e)
if (e==null) return false;
if (e.ElementType == ElementType.Universe ) return true;
if (e.ElementType == ElementType.World ) return true;
var nTimesTheDiameter = Math.Max( Cell.Diameter * 5, 30);
if (Cell.X - e.X + e.Radius > nTimesTheDiameter ) return false;
if (e.X - e.Radius - Cell.X > nTimesTheDiameter ) return false;
if (Cell.Y - e.Y + e.Radius > nTimesTheDiameter ) return false;
if (e.Y - e.Radius - Cell.Y > nTimesTheDiameter ) return false;
var nTimesTheDiameter = Math.Max( ( Cell?.Diameter ?? 50 ) * 5, 30);
if (CurrentX - e.X + e.Radius > nTimesTheDiameter ) return false;
if (e.X - e.Radius - CurrentX > nTimesTheDiameter ) return false;
if (CurrentY - e.Y + e.Radius > nTimesTheDiameter ) return false;
if (e.Y - e.Radius - CurrentY > nTimesTheDiameter ) return false;
return true;
}

Expand Down
9 changes: 9 additions & 0 deletions src/blagario/Pages/Index.razor
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@
style="@element.CssStyle(Player)">
</div>
}

@if ( Player.Cell == null )
{
<div style="@Player.FormCss">
<input type="text" style="position:absolute; top:0; left:-100px; width: 350px;" bind-value="Player.Name">
<input type="button" style="position:absolute; top:0; left:-150px; width:50px;" value="Play!" onclick="@Player.Play">
</div>
}

</div>

<div class="scoreboard">
Expand Down

0 comments on commit 4f5dde7

Please sign in to comment.