Skip to content

Instantly share code, notes, and snippets.

@kim366
Created August 25, 2018 11:13
Show Gist options
  • Select an option

  • Save kim366/65ac05a7132e7db1b5648820dd47289d to your computer and use it in GitHub Desktop.

Select an option

Save kim366/65ac05a7132e7db1b5648820dd47289d to your computer and use it in GitHub Desktop.
using StaticArrays
mutable struct CAcceleration
a::SVector{2, Float32}
end
mutable struct CPosition
p::SVector{2, Float32}
end
mutable struct CVelocity
v::SVector{2, Float32}
end
function SVelocity(entity)
if CVelocity ∈ components(entity) && CAcceleration ∈ components(entity)
entity[CVelocity].v += entity[CAcceleration].a
end
end
function SPosition(entity)
if CPosition ∈ components(entity) && CVelocity ∈ components(entity)
entity[CPosition].p += entity[CVelocity].v
end
end
entities = []
components = keys
systems = [SPosition, SVelocity]
const Entity = Dict{DataType, Any}
function pushentity!(components::Pair{DataType, <:Any}...)
entity = Entity()
for component ∈ components
entity[first(component)] = first(component)(last(component))
end
push!(entities, entity)
end
pushentity!(CPosition => [0, 0], CVelocity => [1, 3], CAcceleration => [1, 0])
pushentity!(CPosition => [0, 0], CVelocity => [2, 1])
for _ ∈ 0:100, system ∈ systems, entity ∈ entities
system(entity)
end
for entity ∈ entities
println(entity)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment