Skip to content

Commit

Permalink
rounding sample components
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasteles committed Mar 18, 2024
1 parent 9ce9f6c commit aa9dc7b
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 18 deletions.
3 changes: 2 additions & 1 deletion samples/SpaceWar.Lobby/publish.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ publish() {
dotnet publish . \
--configuration Release --self-contained \
--output "$output_path" \
-r $1 -p:DebugType=None -p:DebugSymbols=false
-r $1 -p:DebugType=None -p:DebugSymbols=false \
-p:EnableCompressionInSingleFile=false
}

publish "win-x64"
Expand Down
7 changes: 7 additions & 0 deletions samples/SpaceWar.Shared/Extensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace SpaceWar;

public static class Extensions
{
public static Vector2 RoundTo(this Vector2 vector, int digits = 2) =>
new(MathF.Round(vector.X, digits), MathF.Round(vector.Y, digits));
}
38 changes: 21 additions & 17 deletions samples/SpaceWar.Shared/Logic/GameState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ public void Init(int numberOfPlayers)
var heading = (i + 1) * 360f / numberOfPlayers;
var theta = MathHelper.ToRadians(heading);
var (cosT, sinT) = (Math.Cos(theta), Math.Sin(theta));
var x = width / 2.0 + r * cosT;
var y = height / 2.0 + r * sinT;
var x = Math.Round(width / 2.0 + r * cosT, 2);
var y = Math.Round(height / 2.0 + r * sinT, 2);
Ships[i].Id = (byte)(i + 1);
Ships[i].Position = new((float)x, (float)y);
Ships[i].Active = true;
Expand All @@ -48,6 +48,7 @@ public GameInput ParseShipInputs(PlayerInputs inputs, in Ship ship)
{
if (!ship.Active)
return new();

float heading;
if (inputs.HasFlag(PlayerInputs.RotateRight))
heading = (ship.Heading + Config.RotateIncrement) % 360f;
Expand All @@ -71,7 +72,7 @@ public GameInput ParseShipInputs(PlayerInputs inputs, in Ship ship)
public void UpdateShip(in Ship ship, in GameInput inputs)
{
ship.Heading = (int)inputs.Heading;
Vector2 rotation = new(
Vector2 dir = new(
MathF.Cos(MathHelper.ToRadians(ship.Heading)),
MathF.Sin(MathHelper.ToRadians(ship.Heading))
);
Expand All @@ -82,8 +83,8 @@ public void UpdateShip(in Ship ship, in GameInput inputs)
if (bullet.Active)
continue;
bullet.Active = true;
bullet.Position = ship.Position + rotation * ship.Radius;
bullet.Velocity = ship.Velocity + rotation * Config.BulletSpeed;
bullet.Position = (ship.Position + dir * ship.Radius).RoundTo();
bullet.Velocity = (ship.Velocity + dir * Config.BulletSpeed).RoundTo();
ship.FireCooldown = Config.BulletCooldown;
break;
}
Expand All @@ -97,19 +98,22 @@ public void UpdateShip(in Ship ship, in GameInput inputs)
ship.Missile.ExplosionRadius = Config.MissileExplosionRadius;
ship.Missile.ExplodeTimeout = Config.MissileExplosionTimeout;
ship.Missile.HitBoxTime = Config.MissileHitBoxTimeout;
ship.Missile.Velocity = rotation * Config.MissileSpeed;
ship.Missile.Position = ship.Position + ship.Velocity +
rotation * (ship.Radius + ship.Missile.ProjectileRadius);
ship.Velocity += ship.Missile.Velocity * -2;
ship.Missile.Velocity = dir * Config.MissileSpeed;
ship.Missile.Position = (
ship.Position + ship.Velocity + dir *
(ship.Radius + ship.Missile.ProjectileRadius)
).RoundTo();

ship.Velocity += (ship.Missile.Velocity * -2).RoundTo();
}

ship.Thrust = Math.Sign(inputs.Thrust);
if (inputs.Thrust != 0)
{
ship.Velocity += rotation * inputs.Thrust;
ship.Velocity += (dir * inputs.Thrust).RoundTo();
var magnitude = ship.Velocity.Length();
if (magnitude > Config.ShipMaxThrust)
ship.Velocity = ship.Velocity * Config.ShipMaxThrust / magnitude;
ship.Velocity = (ship.Velocity * Config.ShipMaxThrust / magnitude).RoundTo();
}

ship.Position += ship.Velocity;
Expand All @@ -129,13 +133,11 @@ public void UpdateShip(in Ship ship, in GameInput inputs)

UpdateBullets(ship);
UpdateMissile(ship);

if (ship.FireCooldown > 0) ship.FireCooldown--;
if (ship.MissileCooldown > 0) ship.MissileCooldown--;
if (ship.Invincible > 0) ship.Invincible--;
if (ship.Health <= 0) ship.Active = false;
// LATER: validate multiplatform float precision
// ship.Velocity = Vector2.Round(ship.Velocity);
// ship.Position = Vector2.Round(ship.Position);
}

void UpdateBullets(Ship ship)
Expand Down Expand Up @@ -187,8 +189,10 @@ void UpdateMissile(Ship ship)
ref var missile = ref ship.Missile;
missile.Position += missile.Velocity;
if (missile.Velocity.Length() < Config.MissileMaxSpeed)
missile.Velocity +=
Vector2.Normalize(missile.Velocity) * Config.MissileAcceleration;
missile.Velocity += (
Vector2.Normalize(missile.Velocity) * Config.MissileAcceleration
).RoundTo();

if (missile.HitBoxTime <= 0)
missile.Active = false;
else
Expand Down Expand Up @@ -237,7 +241,7 @@ void UpdateMissile(Ship ship)
other.Health -= Config.MissileDamage;
other.Invincible = Config.MissileInvincibleTime;
var pushDirection = Vector2.Normalize(other.Position - missile.Position);
other.Velocity = pushDirection * Config.ShipMaxThrust;
other.Velocity = (pushDirection * Config.ShipMaxThrust).RoundTo();
other.Position += other.Velocity * 2;
}

Expand Down

0 comments on commit aa9dc7b

Please sign in to comment.