Skip to content

Instantly share code, notes, and snippets.

@TheSeamau5
Created January 6, 2015 17:21
Show Gist options
  • Save TheSeamau5/a24b24624c462401cf22 to your computer and use it in GitHub Desktop.
Save TheSeamau5/a24b24624c462401cf22 to your computer and use it in GitHub Desktop.
Using Heterogeneous lists in Proposal in Elm
mario = {
position = {x = 0, y = 0},
velocity = {x = 0, y = 0},
mass = 10,
Life,
Groundedness,
Controllability
}
goomba = {
position = {x = 20, y = 0},
velocity = {x = -1, y = 0},
mass = 10,
Life,
Groundedness
}
lakituCloud = {
position = {x = 0, y = 0},
velocity = {x = -2, y = 0},
Life
}
optional {mass : Float, velocity : {x : Float, y : Float}} from entity
applyGravity : {x : Float, y : Float } -> entity -> entity
applyGravity force entity =
if has {mass : Float, velocity : {x : Float, y : Float}} entity
then
{entity |
velocity.x <- entity.velocity.x + force.x / entity.mass,
velocity.y <- entity.velocity.y + force.y / entity.mass
}
else
entity
optional {position : {x : Float, y : Float}, velocity : {x : Float, y : Float}} from entity
move : entity -> entity
move entity =
if has {position : {x : Float, y : Float}, velocity : {x : Float, y : Float}} entity
then
{entity |
position.x <- entity.position.x + entity.velocity.x,
position.y <- entity.position.y + entity.velocity.y
}
else
entity
update = move << applyGravity {x = 0, y = -9.81}
-- The list of entities. Note how all entities do not have the same fields
entities = [mario, goomba, lakituCloud]
-- The entities after calling update on every entity
-- In this case, mario, goomba, and lakituCloud have moved
-- But only mario and goomba were affected by gravity
updatedEntities = map update entities
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment