Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
132 changes: 132 additions & 0 deletions samples/pong/Car.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
using rasdaq;
using rasdaq.Core.ECS;
using rasdaq.Graphics;
using rasdaq.Inputs;
using rasdaq.Logging;
using rasdaq.Transformations;

public class Car : Component
{
public float speed;
PhysicsBody? body;

public override void Start()
{
base.Start();

body = Entity?.GetComponent<PhysicsBody>();
}

Vector2 currentMovement = Vector2.Zero;

public override void Update(double deltaTime)
{
base.Update(deltaTime);
Accelerate(deltaTime);
Turn((float)deltaTime);
CameraFollow(deltaTime);
}

private void CameraFollow(double deltaTime)
{
Vector2 lerpedCamPosition = Lerp.Linear(
Renderer.Instance.Camera.Position,
(Vector2)Entity.Transform.position,
10 * (float)deltaTime
);

Renderer.Instance.Camera.Position = lerpedCamPosition;
}

private float _currentSpeed;
public float maxSpeed = 2000;

private void Accelerate(double deltaTime)
{
if (body == null)
{
return;
}

if (currentMovement.Y > 0)
{
_currentSpeed += speed * (float)deltaTime;
}
else if (currentMovement.Y < 0)
{
_currentSpeed -= speed * (float)deltaTime;
}
else
{
_currentSpeed = Lerp.Linear(_currentSpeed, 0, 1 * (float)deltaTime);
}

_currentSpeed = Math.Clamp(_currentSpeed, -maxSpeed, maxSpeed);

Vector2 velocity = Entity.Transform.Forward * _currentSpeed;
body.MoveOnce(velocity);
}

public override void FrameUpdate(double deltaTime)
{
base.FrameUpdate(deltaTime);

Vertical();
Horizontal();
}

public float rotationSpeed = 100;

private void Turn(float deltaTime)
{
// Prevent turning when stopped
float speedRatio = Math.Abs(_currentSpeed) / maxSpeed;
if (speedRatio < 0.001f)
{
return;
}

float turnFactor = 0.5f - speedRatio;

Entity.Transform.rotation += (float)(
currentMovement.X * rotationSpeed * deltaTime * turnFactor
);
}

private void Horizontal()
{
if (Input.IsKeyDown(Keys.D))
{
currentMovement.X = -1;
}
else if (Input.IsKeyDown(Keys.A))
{
currentMovement.X = 1;
}
else
{
currentMovement.X = 0;
}

if (_currentSpeed < 0)
{
currentMovement.X *= -1;
}
}

private void Vertical()
{
if (Input.IsKeyDown(Keys.W))
{
currentMovement.Y = 1;
}
else if (Input.IsKeyDown(Keys.S))
{
currentMovement.Y = -1;
}
else
{
currentMovement.Y = 0;
}
}
}
60 changes: 44 additions & 16 deletions samples/pong/Game.cs
Original file line number Diff line number Diff line change
@@ -1,42 +1,70 @@
using rasdaq;
using rasdaq.Core.ECS;
using rasdaq.Graphics;
using rasdaq.Logging;
using rasdaq.Resources;
using rasdaq.Transformations;
using System.Drawing;

namespace pong;

public class Game : Application
{
public Texture andrei;
public Texture evilDiv;
public Texture carTexture;
public Texture background;

public override void Init()
{
andrei = ResourceManager.Load<Texture>("assets/andrei.png");
evilDiv = ResourceManager.Load<Texture>("assets/evil_enemy.jpg");
carTexture = ResourceManager.Load<Texture>("assets/sport_red.png");
background = ResourceManager.Load<Texture>("assets/rug.png");
}

public override void Start()
{
World world = new();
Entity player = new(new Vector3(456, 228, 1));
Entity enemy = new();
Entity player = new(new Vector3(500, 500, 1));

Sprite spr = new(456, 456, andrei);
world.AddEntity(player);

Sprite spr = new(50, 25, carTexture);
player.AddComponent(spr);
player.AddComponent(new PhysicsBody());
PhysicsBody body = new() { ApplyGravity = false };
player.AddComponent(body);
BoxCollider carCollider = new(50, 25);
player.AddComponent(carCollider);
Car car = new();
player.AddComponent(car);
car.speed = 1000;
car.rotationSpeed = 250;
car.maxSpeed = 5000;

Sprite enemySprite = new(456, 456, evilDiv);
enemy.AddComponent(enemySprite);
Entity collisionBox = new(new Vector3(400, 400, 0));
world.AddEntity(collisionBox);

Soldier sold = new();
player.AddComponent(sold);
Sprite collisionBoxSprite = new(200, 200, background);
collisionBox.AddComponent(collisionBoxSprite);

Enemy enem = new();
enemy.AddComponent(enem);
PhysicsBody physicsBody = new() { ApplyGravity = false };
collisionBox.AddComponent(physicsBody);

world.AddEntity(player);
world.AddEntity(enemy);
BoxCollider boxCollider = new(200, 200);
collisionBox.AddComponent(boxCollider);

boxCollider.OnCollisionEnter += (other) =>
{
Log.Info($"Collision detected with {other.Entity}");
collisionBoxSprite.Color = Color.Red;
};

boxCollider.OnCollisionExit += (other) =>
{
Log.Info($"Collision ended with {other.Entity}");
collisionBoxSprite.Color = Color.White;
};

boxCollider.OnCollisionStay += (other) =>
{
Log.Info($"Collision ongoing with {other.Entity}");
};
}
}
54 changes: 21 additions & 33 deletions samples/pong/Soldier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,71 +5,59 @@
using rasdaq.Logging;
using rasdaq.Resources;
using rasdaq.Transformations;
using System.Drawing;

namespace pong;

internal class Soldier : Component
{
public BoxCollider? collider;
public Sprite? sprite;

public override void Start()
{
base.Start();
sprite = Entity?.GetComponent<Sprite>();
}

public override void Update(double deltaTime)
{
base.Update(deltaTime);

if (collider == null)
{
collider = Entity?.GetComponent<BoxCollider>();
}

if (collider != null)
{
sprite?.Color = collider.Collisions.Count > 0 ? Color.Red : Color.WhiteSmoke;
}
}

public float moveSpeed = 100;

public override void FrameUpdate(double deltaTime)
{
base.Update(deltaTime);

if (Input.IsKeyPressed(Keys.V))
{
Log.Info("V pressed");
}

PhysicsBody? body = Entity?.GetComponent<PhysicsBody>();

if (Input.IsKeyDown(Keys.W))
{
body?.MoveOnce(new Vector2(0, 100));
body?.MoveOnce(new Vector2(0, moveSpeed));
}

if (Input.IsKeyDown(Keys.S))
{
body?.MoveOnce(new Vector2(0, -100));
body?.MoveOnce(new Vector2(0, -moveSpeed));
}
if (Input.IsKeyDown(Keys.A))
{
body?.MoveOnce(new Vector2(-100, 0));
body?.MoveOnce(new Vector2(-moveSpeed, 0));
}
if (Input.IsKeyDown(Keys.D))
{
body?.MoveOnce(new Vector2(100, 0));
}
if (Input.IsKeyDown(Keys.H))
{
// track camera to entity
var x = Entity?.Transform.position.X - (Application.WindowSize?.X / 2) ?? 0;
var y = Entity?.Transform.position.Y - (Application.WindowSize?.Y / 2) ?? 0;
Renderer.Instance.Camera.SetPosition(x, y);
}
}
}

internal class Enemy : Component
{
public override void FrameUpdate(double deltaTime)
{
base.Update(deltaTime);

if (Input.IsKeyDown(Keys.I))
{
// track camera to entity
var x = Entity?.Transform.position.X - (Application.WindowSize?.X / 2) ?? 0;
var y = Entity?.Transform.position.Y - (Application.WindowSize?.Y / 2) ?? 0;
Renderer.Instance.Camera.SetPosition(x, y);
body?.MoveOnce(new Vector2(moveSpeed, 0));
}
}
}
Binary file added samples/pong/assets/rug.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added samples/pong/assets/sport_red.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 9 additions & 7 deletions src/rasdaq/Application.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,14 @@ public class Application : IDisposable
/// </summary>
public static Application Instance
{
get => _instance ?? throw new InvalidOperationException("Application not started. Call Run() first.");
get =>
_instance
?? throw new InvalidOperationException("Application not started. Call Run() first.");
private set => _instance = value;
}
private static Application? _instance;
public static Vector2? WindowSize => Instance._gameWindow?.Size;

/// <summary>
/// Return list of instantiated <c>Worlds</c>.
/// </summary>
Expand All @@ -44,7 +47,9 @@ internal InputManager InputManager
private GameWindow? _gameWindow;
internal GameWindow GameWindow
{
get => _gameWindow ?? throw new InvalidOperationException("Game window not found. Call Run() first.");
get =>
_gameWindow
?? throw new InvalidOperationException("Game window not found. Call Run() first.");
private set => _gameWindow = value;
}

Expand All @@ -71,14 +76,11 @@ public void Run(int width, int height, string title)
Instance = this;

GameWindow = new(
new GameWindowSettings()
{
UpdateFrequency = 0
},
new GameWindowSettings() { UpdateFrequency = 0 },
new NativeWindowSettings() { ClientSize = (width, height), Title = title }
)
{
VSync = VSyncMode.Off
VSync = VSyncMode.Off,
};
GameWindow.UpdateFrame += OnUpdateFrame;
GameWindow.Load += OnLoad;
Expand Down
Loading