// Sharp Game Engine Example
// Features: Structs, impl blocks, methods, struct initialization, arrays
struct Vector2 {
x: float
y: float
}
struct Transform {
position: Vector2
rotation: float
scale: float
}
struct Health {
current: int
max: int
}
struct GameObject {
name: int // Store as simple int for now
transform: Transform
health: Health
active: bool
}
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 Transform {
def new(x: float, y: float) -> Transform {
return Transform {
position: Vector2 { x: x, y: y },
rotation: 0.0,
scale: 1.0
}
}
def move(mut self, dx: float, dy: float) {
self.position.x += dx
self.position.y += dy
}
}
impl Health {
def new(max: int) -> Health {
return Health { current: max, max: max }
}
def take_damage(mut self, amount: int) {
self.current -= amount
if self.current < 0 {
self.current = 0
}
}
def heal(mut self, amount: int) {
self.current += amount
if self.current > self.max {
self.current = self.max
}
}
def is_alive(self) -> bool {
if self.current > 0 {
return true
} else {
return false
}
}
}
impl GameObject {
def new(name: int, x: float, y: float, max_health: int) -> GameObject {
return GameObject {
name: name,
transform: Transform {
position: Vector2 { x: x, y: y },
rotation: 0.0,
scale: 1.0
},
health: Health { current: max_health, max: max_health },
active: true
}
}
def update(mut self, dt: float) {
if self.health.current <= 0 {
self.active = false
}
}
def is_alive(self) -> bool {
return self.active
}
}
def main() -> int {
let mut player = GameObject::new(1, 0.0, 0.0, 100)
let mut enemy = GameObject::new(2, 50.0, 0.0, 50)
// Simulate combat
let round = 0
while round < 3 {
if enemy.health.current > 0 {
player.health.take_damage(10)
}
if player.health.current > 0 {
enemy.health.take_damage(15)
}
}
// Return player health status
return player.health.current
}