Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

An an implementation of an Entity Component System #18

Merged
merged 17 commits into from
Sep 5, 2024
Merged

An an implementation of an Entity Component System #18

merged 17 commits into from
Sep 5, 2024

Conversation

paulcadman
Copy link
Collaborator

@paulcadman paulcadman commented Aug 30, 2024

You can write something like, using the command makeWorldAndComponents to generate all of the boilerplate.

structure Position where
  position : Vector2

structure Velocity where
  velocity : Vector2

makeWorldAndComponents Position Velocity

def update (pv : Position × Velocity) : Position :=
  let (p, v) := pv
  {position := p.position.add v.velocity}

def dumpState : System World Unit := do
  IO.println "DUMP STATE"
  cmapM_ (fun ((p, v) : Position × Velocity) =>
    IO.println s!"position : ({p.position.x}, {p.position.y}) velocity : ({v.velocity.x}, {v.velocity.y})")

def game : System World Unit := do
  newEntity_ ({position := ⟨0,0⟩ : Position}, {velocity := ⟨10,11⟩ : Velocity})
  newEntity_ ({position := ⟨1,0⟩ : Position}, {velocity := ⟨-5,-2⟩ : Velocity})
  dumpState
  cmap update
  dumpState

def main : IO Unit := do
  runSystem game (← initWorld)

And run:

$ just run-ecs-example
DUMP STATE
position : (0.000000, 0.000000) velocity : (10.000000, 11.000000)
position : (1.000000, 0.000000) velocity : (-5.000000, -2.000000)
DUMP STATE
position : (10.000000, 11.000000) velocity : (10.000000, 11.000000)
position : (-4.000000, -2.000000) velocity : (-5.000000, -2.000000)

@paulcadman paulcadman force-pushed the ecs branch 2 times, most recently from 0274ada to 8ba4e9b Compare September 2, 2024 22:39
@paulcadman paulcadman marked this pull request as ready for review September 5, 2024 22:57
@paulcadman paulcadman merged commit 8ffd62f into main Sep 5, 2024
2 checks passed
@paulcadman paulcadman deleted the ecs branch September 5, 2024 23:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant