Skip to content

Commit

Permalink
WIP. Spliting and pointing
Browse files Browse the repository at this point in the history
  • Loading branch information
ctrl-alt-d committed Aug 9, 2019
1 parent 78da151 commit f9a1de0
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/blagario/AgarElements/Cell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ internal void Split()
newPart.X = this.X.Value + 100;
newPart.Y = this.Y.Value + 100;
newPart._Mass = currentPart._Mass;
newPart.PointTo(currentPart.PointingXto, currentPart.PointingYto);
this.Add(newPart);
}
}
Expand Down
6 changes: 6 additions & 0 deletions src/blagario/AgarElements/MoveableAgarElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ public class MoveableAgarElement: AgarElement
protected double Vx {get; private set;} = 0;
protected double Vy {get; private set;} = 0;

public double PointingXto {get; private set; };
public double PointingYto {get; private set; };

public virtual double Vel {get; protected set;} = 1;

public virtual void PointTo( double x, double y )
Expand Down Expand Up @@ -35,6 +38,9 @@ public virtual void PointTo( double x, double y )
this.Vy = this.Vel * sinAlf;
this.Vx = this.Vel * cosAlf;

PointingXto = x;
PointingYto = y;

}

public override async Task Tic(int fpsTicNum) {
Expand Down
15 changes: 13 additions & 2 deletions src/blagario/AgarElements/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public void Play()

}

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

public Cell Cell {get; private set;}

Expand Down Expand Up @@ -81,7 +81,7 @@ internal void PointTo(double bx, double by)
Cell.PointTo(bx, by);
}

public void Tic()
public void Tic(int fpsTicNum)
{
this.Cell.Purge();

Expand All @@ -101,6 +101,11 @@ public void Tic()
DeltaZoom -= d;
Zoom += d;
}

if ( (fpsTicNum+8) % TimedHostedService.fps == 0)
{
Task.Run( async () => await SetFocusToUniverse() );
}
}

/* --- */
Expand Down Expand Up @@ -158,6 +163,12 @@ public async Task CheckVisibleArea(IJSRuntime jsRuntime = null)
await JsRuntime.InvokeAsync<object>("AreaResized", CreateDotNetObjectRef(this) );
}

public async Task SetFocusToUniverse(IJSRuntime jsRuntime = null)
{
JsRuntime = jsRuntime ?? JsRuntime;
await JsRuntime.InvokeAsync<object>("SetFocusToUniverse");
}

#region Hack to fix https://github.com/aspnet/AspNetCore/issues/11159

public static object CreateDotNetObjectRefSyncObj = new object();
Expand Down
13 changes: 9 additions & 4 deletions src/blagario/AgarElements/World.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
namespace blagario.elements
{

public class WorldTicEventArgs: EventArgs
{
public int FpsTicNum {set;get;}
}

public class World : AgarElement
{
public List<AgarElement> Elements;
Expand Down Expand Up @@ -36,11 +41,11 @@ public World(Universe universe)
public IEnumerable<CellPart> Leaderboard => Elements.Where(x => x.ElementType == ElementType.CellPart ).Select(x=> x as CellPart).OrderByDescending(x => x._Mass ).Take(10);


public event EventHandler OnTicReached;
public event EventHandler<WorldTicEventArgs> OnTicReached;

protected virtual void OnTic(EventArgs e)
protected virtual void OnTic(WorldTicEventArgs e)
{
EventHandler handler = OnTicReached;
var handler = OnTicReached;
handler?.Invoke(this, e);
}
public override async Task Tic(int fpsTicNum)
Expand All @@ -67,7 +72,7 @@ public override async Task Tic(int fpsTicNum)

await base.Tic(fpsTicNum);

OnTic( EventArgs.Empty );
OnTic( new WorldTicEventArgs {FpsTicNum=fpsTicNum} );
}

private List<AgarElement> FillWorld(ElementType elementType)
Expand Down
4 changes: 3 additions & 1 deletion src/blagario/Pages/Index.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ protected async override Task OnAfterRenderAsync()
}
protected async Task OnAfterFirstRenderAsync()
{
await Player.CheckVisibleArea(JsRuntime);
await Player.CheckVisibleArea(JsRuntime);
await Player.SetFocusToUniverse(JsRuntime);
}

}
}
3 changes: 2 additions & 1 deletion src/blagario/Pages/Index.razor
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
@onmousemove="@TrackMouse"
@onmousewheel="@MouseWheel"
@onkeydown="@KeyDown"
@onclick="@OnClick"
tabindex="0"
id="universe"
>
<!-- This is the world. Where the virus, cells and other elements live. -->
<div
Expand Down
4 changes: 4 additions & 0 deletions src/blagario/Pages/_Host.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@
return [ x , y ];
};
window.SetFocusToUniverse = () => {
console.log("setting focus");
document.getElementById("universe").focus();
};
</script>

</body>
Expand Down

0 comments on commit f9a1de0

Please sign in to comment.