Created
January 6, 2015 17:21
-
-
Save TheSeamau5/a24b24624c462401cf22 to your computer and use it in GitHub Desktop.
Using Heterogeneous lists in Proposal in Elm
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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