Last active
January 27, 2018 10:31
-
-
Save TheSeamau5/a8f8dc9524aa609ab775 to your computer and use it in GitHub Desktop.
Idealized vision of using Entity Component Systems
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
---------------------------- | |
----- TYPE DEFINITIONS ----- | |
---------------------------- | |
Entity : Object | |
Component : Object | |
DeltaTime : Component | |
deltaTime : Number | |
Directions : Component | |
directions : Point | |
Input : Entity, DeltaTime, Directions | |
State : Entity | |
input : Input | |
world : List Entity | |
actions : List Action | |
Action : State -> Entity -> Entity | |
Updater : State -> State | |
update : Updater | |
update state = | |
state.world <- applyAll state.actions state state.world | |
---------------------------- | |
-------- COMPONENTS -------- | |
---------------------------- | |
Position : Component | |
position : Point | |
Velocity : Component | |
velocity : Point | |
Mass : Component | |
mass : Number | |
Life : Component | |
Life | |
Flying : Component | |
Flying | |
Grounded : Component | |
Grounded | |
Controllable : Component | |
Controllable | |
Destructible: Component | |
Destructible | |
---------------------------- | |
--------- ENTITIES --------- | |
---------------------------- | |
mario : Entity, Position, Velocity, Mass, Life, Controllable, Groundedness | |
mario = | |
position = {0,0} | |
velocity = {0,0} | |
mass = 20 | |
Life | |
Controllable | |
Groundedness | |
goomba : Entity, Position, Velocity, Mass, Life, Grounded | |
goomba = | |
position = {20, 0} | |
velocity = {-1, 0} | |
mass = 10 | |
Life | |
Grounded | |
lakituCloud : Entity, Position, Velocity, Life, Flying | |
lakituCloud = | |
position = {0,100} | |
velocity = {-2,0} | |
Life | |
Flying | |
block : Entity, Position, Destructible | |
block = | |
position = {20,20} | |
Destructible | |
----------------------------- | |
---------- ACTIONS ---------- | |
----------------------------- | |
move : Action | |
move state entity = | |
if entity has Position, Velocity | |
then | |
entity.position <- entity.position + entity.velocity * state.input.deltaTime | |
else | |
entity | |
applyForce : Point -> Action | |
applyForce force state entity = | |
if entity has Velocity, Mass | |
then | |
entity.velocity <- entity.velocity + force / entity.mass * state.input.deltaTime | |
else | |
entity | |
applyInput : Action | |
applyInput state entity = | |
if entity has Position, Life and is Grounded | |
then | |
entity.position.x <- entity.position.x + input.directions.x | |
else | |
entity | |
---------------------------- | |
-------- GAME LOGIC -------- | |
---------------------------- | |
world : List Entity | |
world = [mario, goomba, lakituCloud, block] | |
actions : List Action | |
actions = [applyInput, applyGravity {0, -9.8}, move] | |
state : State | |
state = | |
actions = actions | |
world = world | |
input = | |
deltaTime = Time.deltaTime | |
directions = Keyboard.directions | |
updatedState : State | |
updatedState = update state |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment