diff options
Diffstat (limited to 'src/LivingVillage.Kernel/Sim.fs')
| -rw-r--r-- | src/LivingVillage.Kernel/Sim.fs | 40 |
1 files changed, 31 insertions, 9 deletions
diff --git a/src/LivingVillage.Kernel/Sim.fs b/src/LivingVillage.Kernel/Sim.fs index d1b2e57..0e3683b 100644 --- a/src/LivingVillage.Kernel/Sim.fs +++ b/src/LivingVillage.Kernel/Sim.fs @@ -2,10 +2,12 @@ namespace LivingVillage.Kernel module Sim = + [<Struct>] type Vec2 = { X: float32 Y: float32 } + [<Struct>] type Avatar = { Pos: Vec2 } @@ -14,33 +16,53 @@ module Sim = MoveY: float32 } type TimeStep = - { Dt: float32 - Input: Input } + { Input: Input } + [<Struct>] + type NoHost = + { Reserved: uint64 } + + [<Struct>] type World = { Tick: int64 - Avatar: Avatar } + Time: float + Rng: RngState + Avatar: Avatar + NoHost: NoHost } + + let ticksPerSecond = 60L + let secondsPerDay = 86400L + let ticksPerDay = secondsPerDay * ticksPerSecond + let dtSeconds = 1.0 / float ticksPerSecond + let dtSecondsF = 1.0f / float32 ticksPerSecond let mapWidthTiles = 64 let mapHeightTiles = 48 let tilePixels = 32 let avatarSpeed = 160.0f - let initialWorld () : World = + let initialWorld (seed: uint64) : World = let centerX = float32 (mapWidthTiles * tilePixels / 2 - tilePixels / 2) let centerY = float32 (mapHeightTiles * tilePixels / 2 - tilePixels / 2) { Tick = 0L - Avatar = { Pos = { X = centerX; Y = centerY } } } + Time = 0.0 + Rng = Rng.ofSeed seed + Avatar = { Pos = { X = centerX; Y = centerY } } + NoHost = { Reserved = 0UL } } let clamp v lo hi = if v < lo then lo elif v > hi then hi else v let step (ts: TimeStep) (world: World) : World = + let tick = world.Tick + 1L let maxX = float32 (mapWidthTiles * tilePixels - tilePixels) let maxY = float32 (mapHeightTiles * tilePixels - tilePixels) - let dx = ts.Input.MoveX * avatarSpeed * ts.Dt - let dy = ts.Input.MoveY * avatarSpeed * ts.Dt - { Tick = world.Tick + 1L + let dx = ts.Input.MoveX * avatarSpeed * dtSecondsF + let dy = ts.Input.MoveY * avatarSpeed * dtSecondsF + { Tick = tick + Time = float tick * dtSeconds + Rng = world.Rng Avatar = { Pos = { X = clamp (world.Avatar.Pos.X + dx) 0.0f maxX - Y = clamp (world.Avatar.Pos.Y + dy) 0.0f maxY } } } + Y = clamp (world.Avatar.Pos.Y + dy) 0.0f maxY } } + NoHost = world.NoHost } |
