# Sharp Game Engine README
This README explains how to build game engines with Sharp, using structs, impl blocks, methods, enums, and composition. It includes patterns, examples, and guidance.
## Concepts
- **Entity as Struct**: Model game objects as structs composed of smaller components.
- **Behavior via Methods**: Use `impl` blocks to attach behavior.
- **Composition over Inheritance**: Prefer nested structs to build complex entities.
- **State with Enums**: Manage engine and entity state via `enum` types.
## Core Components
```sharp
struct Vector2 { x: float, y: float }
struct Transform {
position: Vector2
rotation: float
}
struct Health { current: int, max: int }
struct GameObject {
transform: Transform
health: Health
active: bool
}
```
## Methods and Behavior
```sharp
impl Vector2 {
def new(x: float, y: float) -> Vector2 {
return Vector2 { x: x, y: y }
}
def add(self, other: Vector2) -> Vector2 {
return Vector2 { x: self.x + other.x, y: self.y + other.y }
}
}
impl Health {
def new(max: int) -> Health {
return Health { current: max, max: max }
}
def take_damage(mut self, amount: int) { self.current -= amount }
def heal(mut self, amount: int) {
self.current += amount
if self.current > self.max { self.current = self.max }
}
def is_alive(self) -> bool { return self.current > 0 }
}
impl GameObject {
def new(x: float, y: float, max_hp: int) -> GameObject {
return GameObject {
transform: Transform { position: Vector2 { x: x, y: y }, rotation: 0.0 },
health: Health { current: max_hp, max: max_hp },
active: true
}
}
def update(mut self) {
if self.health.current <= 0 { self.active = false }
}
}
```
## Struct Initialization
```sharp
let player = GameObject::new(0.0, 0.0, 100)
let enemy = GameObject::new(50.0, 0.0, 50)
```
## State Machines with Enums
```sharp
enum GameState { Loading, MainMenu, Playing, Paused, GameOver }
enum EntityType { Player, Enemy, NPC, Projectile }
```
## Example: Mini Engine Loop
```sharp
def main() -> int {
let mut player = GameObject::new(0.0, 0.0, 100)
let mut enemy = GameObject::new(50.0, 0.0, 50)
# simulate a few frames
let frame: int = 0
while frame < 3 {
player.health.take_damage(10)
enemy.health.take_damage(15)
player.update()
enemy.update()
}
return player.health.current
}
```
## Patterns
- **Physics**: `RigidBody { velocity: Vector2, acceleration: Vector2, mass: float }`
- **Collision**: `Collider { radius: float, layer: int }`
- **Rendering**: `Sprite { texture_id: int, frame: int }`
- **ECS-style Composition**: Build entities by composing components as fields.
## Tips
- Keep data in structs; behavior in `impl` methods.
- Use `mut self` when the method modifies the instance.
- Prefer enums for engine modes, entity kinds, or outcomes (`Result<T>`).
- Use arrays `[T; N]` for fixed-size collections; dynamic methods like `.len()` are planned.
## Examples
See:
- `examples/gameobjects.shrp`
- `examples/gameobject_simple.shrp`
- `examples/structs.shrp`
- `examples/enums.shrp`
## Roadmap (Engine)
- Arrays with methods (len/push)
- Generics and traits for component interfaces
- Standard library: math/collections/time
- Pattern matching on enums
## License
MIT License