Skip to content

Commit

Permalink
This is the dumbest wind sailing I've ever
Browse files Browse the repository at this point in the history
  • Loading branch information
Nhawdge committed Oct 26, 2023
1 parent b50e5d1 commit c380a26
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 10 deletions.
8 changes: 8 additions & 0 deletions Components/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,13 @@ internal class Player
internal Vector2 Position;

Check warning on line 7 in Components/Player.cs

View workflow job for this annotation

GitHub Actions / build

Field 'Player.Position' is never assigned to, and will always have its default value
internal float Speed = 500f;
internal float RotationSpeed = 100f;
internal SailStatus Sail = SailStatus.Closed;
}

public enum SailStatus
{
Closed,
Half,
Full,
}
}
12 changes: 12 additions & 0 deletions Components/Singleton.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NovemberPirates.Components
{
internal class Singleton
{
}
}
10 changes: 10 additions & 0 deletions Components/Wind.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Numerics;

namespace NovemberPirates.Components
{
internal class Wind
{
public float WindStrength = 10;
public Vector2 WindDirection = new Vector2(1, 0);
}
}
9 changes: 7 additions & 2 deletions Scenes/Levels/OceanScene.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using NovemberPirates.Entities.Archetypes;
using Arch.Core.Extensions;
using NovemberPirates.Components;
using NovemberPirates.Entities.Archetypes;
using NovemberPirates.Scenes.Levels.Systems;
using NovemberPirates.Utilities;
using System.Numerics;

namespace NovemberPirates.Scenes.Levels
{
Expand All @@ -11,11 +14,13 @@ internal OceanScene()
Systems.Add(new RenderSystem());
Systems.Add(new BoatMovementSystem());
Systems.Add(new CameraSystem());
Systems.Add(new WindSystem());

MapManager.Instance.LoadMap("Level_0", World);

PlayerBuilder.Create(World);

var singleton = World.Create<Singleton, Wind>();
singleton.Set(new Wind());
}
}
}
45 changes: 37 additions & 8 deletions Scenes/Levels/Systems/BoatMovementSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,32 @@ internal override void Update(World world)
var sprites = new QueryDescription().WithAll<Player, Sprite>();
var tiles = new QueryDescription().WithAll<MapTile, Render>();

var windQueryDescription = new QueryDescription().WithAll<Wind>();
Wind wind = null;

Check warning on line 23 in Scenes/Levels/Systems/BoatMovementSystem.cs

View workflow job for this annotation

GitHub Actions / build

Converting null literal or possible null value to non-nullable type.
var windQuery = world.Query(in windQueryDescription);
foreach (var chunk in windQuery.GetChunkIterator())
{
foreach (var entity in chunk)
{
wind = chunk.Get<Wind>(entity);
break;
}
}

world.Query(sprites, (entity) =>
{
var sprite = entity.Get<Sprite>();
var player = entity.Get<Player>();

var movement = new Vector2(0, 0);

if (Raylib.IsKeyDown(KeyboardKey.KEY_W))
if (Raylib.IsKeyPressed(KeyboardKey.KEY_W))
{
movement.Y -= 1;
player.Sail = (SailStatus)Math.Min(Enum.GetValues<SailStatus>().Length - 1, (int)player.Sail + 1);
}
if (Raylib.IsKeyDown(KeyboardKey.KEY_S))
if (Raylib.IsKeyPressed(KeyboardKey.KEY_S))
{
movement.Y += 1;
player.Sail = (SailStatus)Math.Max(0, (int)player.Sail - 1);
}
if (Raylib.IsKeyDown(KeyboardKey.KEY_A))
{
Expand All @@ -42,12 +54,29 @@ internal override void Update(World world)
{
sprite.Rotation += player.RotationSpeed * Raylib.GetFrameTime();
}

movement = RayMath.Vector2Rotate(movement, sprite.RotationAsRadians);

var boatAngle = (sprite.Rotation + 360) % 360;
var windAngle = (float)(((Math.Atan2(wind.WindDirection.Y, wind.WindDirection.X) * 180 / Math.PI) + 360 + 90) % 360);

Check warning on line 61 in Scenes/Levels/Systems/BoatMovementSystem.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.
var angleDiff = Math.Abs(boatAngle - windAngle);

//Console.WriteLine($"{boatAngle.ToString("0.0")} - Wind: {windAngle.ToString("0.0")} diff: {angleDiff.ToString("0.0")}");
switch (player.Sail)
{
case SailStatus.Closed:
movement = new Vector2(0, 0);
break;
case SailStatus.Half:
movement += wind.WindDirection * wind.WindStrength / angleDiff / 2 * Raylib.GetFrameTime();
break;
case SailStatus.Full:
movement += wind.WindDirection * wind.WindStrength / angleDiff * Raylib.GetFrameTime();
break;
}
var newPosition = sprite.Position + movement * player.Speed * Raylib.GetFrameTime();

var query = world.Query(in tiles);
var collides = false;

Render collidingTile = null;

Check warning on line 81 in Scenes/Levels/Systems/BoatMovementSystem.cs

View workflow job for this annotation

GitHub Actions / build

Converting null literal or possible null value to non-nullable type.
foreach (ref var chunk in query.GetChunkIterator())
Expand Down Expand Up @@ -76,16 +105,16 @@ internal override void Update(World world)
if (collidingTile.Collision == CollisionType.Slow)
{
sprite.Position += movement * (player.Speed / 2) * Raylib.GetFrameTime();
Console.WriteLine("I am on a slow tile");
//Console.WriteLine("I am on a slow tile");
}
else if (collidingTile.Collision == CollisionType.None)
{
sprite.Position += movement * player.Speed * Raylib.GetFrameTime();
Console.WriteLine("Smooth sailing");
//Console.WriteLine("Smooth sailing");
}
else if (collidingTile.Collision == CollisionType.Solid)
{
Console.WriteLine($"Collision {collidingTile.Collision}");
//Console.WriteLine($"Collision {collidingTile.Collision}");
}
}
});
Expand Down
31 changes: 31 additions & 0 deletions Scenes/Levels/Systems/WindSystem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using Arch.Core;
using Arch.Core.Extensions;
using NovemberPirates.Components;
using NovemberPirates.Systems;
using Raylib_CsLo;

namespace NovemberPirates.Scenes.Levels.Systems
{
internal class WindSystem : GameSystem
{
internal override void Update(World world)
{

}

internal override void UpdateNoCamera(World world)
{
var query = new QueryDescription().WithAll<Wind, Singleton>();

world.Query(in query, (entity) =>
{
var wind = entity.Get<Wind>();
var singleton = entity.Get<Singleton>();

wind.WindDirection = RayMath.Vector2Rotate(wind.WindDirection, 0.1f * Raylib.GetFrameTime());

Raylib.DrawLine(50, 50, (int)(50 + wind.WindDirection.X * 30), (int)(50 + wind.WindDirection.Y * 30), Raylib.ORANGE);
});
}
}
}

0 comments on commit c380a26

Please sign in to comment.