Files use .rex, the command is rex, and the crate, runtime prefixes, docs, editor files and examples all use the new name. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
81 lines
1.5 KiB
Rexx
81 lines
1.5 KiB
Rexx
struct Vec2 {
|
|
x, y: float
|
|
|
|
func add(b: Vec2): Vec2 {
|
|
return Vec2{x + b.x, y + b.y}
|
|
}
|
|
|
|
func scale(k: float): Vec2 {
|
|
return Vec2{x * k, y * k}
|
|
}
|
|
|
|
func length_squared(): float {
|
|
return x * x + y * y
|
|
}
|
|
}
|
|
|
|
struct Player {
|
|
name: string
|
|
pos: Vec2
|
|
vel: Vec2
|
|
hp: int
|
|
|
|
// changes pos and vel, so the compiler treats it as a mutating method
|
|
func update(dt: float) {
|
|
pos = pos.add(vel.scale(dt))
|
|
if pos.x < 0 || pos.x > 100 {
|
|
vel.x = -vel.x
|
|
}
|
|
if pos.y < 0 || pos.y > 100 {
|
|
vel.y = -vel.y
|
|
}
|
|
}
|
|
|
|
// calls a mutating method, so it mutates too
|
|
func step_twice(dt: float) {
|
|
update(dt)
|
|
update(dt)
|
|
}
|
|
|
|
func hurt(amount: int) {
|
|
hp -= amount
|
|
if hp < 0 {
|
|
hp = 0
|
|
}
|
|
}
|
|
|
|
func is_alive(): bool {
|
|
return hp > 0
|
|
}
|
|
|
|
func describe(): string {
|
|
return "{name} at ({pos.x}, {pos.y}) hp={hp}"
|
|
}
|
|
}
|
|
|
|
func main() {
|
|
var p = Player{name: "Ana", pos: Vec2{90, 50}, vel: Vec2{10, 5}, hp: 20}
|
|
print(p.describe())
|
|
|
|
p.update(1)
|
|
print(p.describe())
|
|
p.update(1)
|
|
print(p.describe())
|
|
|
|
p.step_twice(0.5)
|
|
print(p.pos)
|
|
|
|
// omitted fields start at zero
|
|
var q = Player{name: "Zed"}
|
|
print(q)
|
|
print(q.is_alive())
|
|
|
|
p.hurt(15)
|
|
p.hurt(15)
|
|
print(p.hp, p.is_alive())
|
|
|
|
var origin: Vec2
|
|
print(origin, origin.length_squared())
|
|
print(Vec2{3, 4}.length_squared())
|
|
}
|