Skip to content

Commit

Permalink
added more manager tests and ci
Browse files Browse the repository at this point in the history
  • Loading branch information
louisponet committed Oct 19, 2019
1 parent b8b2b79 commit be9021b
Show file tree
Hide file tree
Showing 7 changed files with 177 additions and 41 deletions.
1 change: 1 addition & 0 deletions .codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
comment: false
60 changes: 60 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
## Documentation: http://docs.travis-ci.com/user/languages/julia/
language: julia
os:
- linux
- osx
julia:
- 1.0
- 1.3
- nightly

notifications:
email: false
git:
depth: 99999999

## uncomment the following lines to allow failures on nightly julia
## (tests will run but not make your overall status red)
matrix:
allow_failures:
- julia: nightly

env:
matrix:
- JULIA_NUM_THREADS=1
- JULIA_NUM_THREADS=4
- JULIA_NUM_THREADS=8
## uncomment and modify the following lines to manually install system packages
#addons:
# apt: # apt-get for linux
# packages:
# - gfortran
#before_script: # homebrew for mac
# - if [ $TRAVIS_OS_NAME = osx ]; then brew install gcc; fi

## uncomment the following lines to override the default test script
#script:
# - julia -e 'Pkg.clone(pwd()); Pkg.build("PStdLib"); Pkg.test("PStdLib"; coverage=true)'
after_success:
# push coverage results to Coveralls
- julia -e 'using ECS; using Pkg; cd(dirname(dirname(pathof(ECS)))); Pkg.add("Coverage"); using Coverage; Coveralls.submit(Coveralls.process_folder())'
# push coverage results to Codecov
- julia -e 'using ECS; using Pkg; cd(dirname(dirname(pathof(ECS)))); Pkg.add("Coverage"); using Coverage; Codecov.submit(Codecov.process_folder())'

- julia -e 'Pkg.add("Documenter")'
- julia -e 'using ECS; cd(dirname(dirname(pathof(ECS)))); include(joinpath("docs", "make.jl"))'

# jobs:
# include:
# - name: "Benchmark"
# julia: 1.3
# os: linux
# before_script:
# - git fetch origin '+refs/heads/master:refs/remotes/origin/master'
# - git branch master origin/master
# # Run benchmark outside `script` so that it's hidden by default:
# - julia -e 'using Run; Run.script("benchmark/runjudge.jl")'
# script:
# - julia -e 'using Run; Run.script("benchmark/pprintjudge.jl")'
# after_success: skip
# if: NOT (branch = master)
36 changes: 36 additions & 0 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
environment:
matrix:
- julia_version: 1.0 # latest 1.0.x
- julia_version: 1.3 # latest 1.0.x
- julia_version: nightly
## uncomment the following lines to allow failures on nightly julia
## (tests will run but not make your overall status red)
platform:
- x86
- x64

matrix:
allow_failures:
- julia_version: nightly

branches:
only:
- master
- /release-.*/

notifications:
- provider: Email
on_build_success: false
on_build_failure: false
on_build_status_changed: false

install:
- ps: iex ((new-object net.webclient).DownloadString("https://raw.githubusercontent.com/JuliaCI/Appveyor.jl/version-1/bin/install.ps1"))

build_script:
- echo "%JL_BUILD_SCRIPT%"
- C:\julia\bin\julia -e "%JL_BUILD_SCRIPT%"

test_script:
- echo "%JL_TEST_SCRIPT%"
- C:\julia\bin\julia -e "%JL_TEST_SCRIPT%"
2 changes: 1 addition & 1 deletion src/ECS.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ module ECS
export @component, @shared_component, @component_with_kw, @shared_component_with_kw
export @entities_in

export update_systems, schedule_delete!, delete_scheduled!
export update_systems, schedule_delete!, delete_scheduled!, empty_entities!, system_stage

end # module
93 changes: 53 additions & 40 deletions src/manager.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#To implement AbstractManager interface, the subtype should just define the function manager
#where all the fields are like in this one
mutable struct Manager <: AbstractManager
entities ::Vector{Entity}
free_entities::Vector{Entity}
Expand Down Expand Up @@ -47,43 +49,35 @@ function Manager(system_stages::SystemStage...)
return m
end

Base.map(f, s::Union{System, Manager}, T...) = f(map(x -> Base.getindex(s, x), T)...)

manager(m::Manager) = m

components(m::AbstractManager) = manager(m).components
entities(m::AbstractManager) = manager(m).entities
free_entities(m::AbstractManager) = manager(m).free_entities
to_delete(m::AbstractManager) = manager(m).to_delete
manager(m::Manager) = m

valid_entities(m::AbstractManager) = filter(x -> x.id != 0, entities(m))
system_stages(m::AbstractManager) = manager(m).system_stages
components(m::AbstractManager) = manager(m).components
entities(m::AbstractManager) = manager(m).entities
free_entities(m::AbstractManager) = manager(m).free_entities
to_delete(m::AbstractManager) = manager(m).to_delete
valid_entities(m::AbstractManager) = filter(x -> x.id != 0, entities(m))
system_stages(m::AbstractManager) = manager(m).system_stages
system_stage(m::AbstractManager, s::Symbol) = manager(m).system_stages[s]
singleton(m::AbstractManager, ::Type{T}) where {T<:ComponentData} = m[T][1]

system_stage(m::AbstractManager, s::Symbol) = manager(m).system_stages[s]

singleton(m::AbstractManager, ::Type{T}) where {T<:ComponentData} = m[T][1]
##### BASE Extensions ####
Base.map(f, s::Union{System, Manager}, T...) = f(map(x -> Base.getindex(s, x), T)...)

function components(manager::AbstractManager, ::Type{T}) where {T<:ComponentData}
comps = AbstractComponent[]
for c in components(manager)
if eltype(c) <: T
push!(comps, c)
end
end
return comps
end
Base.in(::Type{R}, m::AbstractManager) where {R<:ComponentData} =
components(m)[component_id(R)] !== EMPTY_COMPONENT

function entity_assert(m::AbstractManager, e::Entity)
es = entities(m)
@assert length(es) >= e.id "$e was never initiated."
@assert es[e.id] != EMPTY_ENTITY "$e was removed previously."
function Base.empty!(m::AbstractManager)
empty!(entities(m))
empty!(free_entities(m))
empty!(components(m))
empty!(system_stages(m))
end

function Base.getindex(m::AbstractManager, ::Type{T}) where {T<:ComponentData}
id = component_id(T)
return components(m)[id]::component_type(T){T}
end

function Base.getindex(m::AbstractManager, e::Entity)
entity_assert(m, e)
data = ComponentData[]
Expand All @@ -94,24 +88,24 @@ function Base.getindex(m::AbstractManager, e::Entity)
end
return data
end

function Base.getindex(v::Vector{SystemStage}, s::Symbol)
id = findfirst(x->first(x) == s, v)
if id === nothing
error("Stage $s not found.")
end
return v[id]
end

Base.getindex(m::AbstractManager, args...) = Base.getindex(manager(m), args...)
Base.setindex!(m::AbstractManager, args...) = Base.setindex!(manager(m), args...)


#TODO: Performance
function Base.getindex(m::Manager, ::Type{T}, e::Entity) where {T<:ComponentData}
entity_assert(m, e)
return m[T][e]
end

Base.setindex!(m::AbstractManager, args...) = Base.setindex!(manager(m), args...)

function Base.setindex!(m::Manager, v::T, e::Entity) where {T<:ComponentData}
entity_assert(m, e)
if !in(T, m)
Expand Down Expand Up @@ -152,6 +146,12 @@ function Base.push!(m::AbstractManager, stage::SystemStage)
push!(system_stages(m), stage)
end

function Base.insert!(m::AbstractManager, i::Integer, stage::SystemStage)
comps = requested_components(stage)
append!(m, comps)
insert!(system_stages(m), i, stage)
end

function Base.push!(m::AbstractManager, stage::Symbol, sys::System)
stage = system_stage(m, stage)
comps = requested_components(sys)
Expand All @@ -176,6 +176,30 @@ function Base.delete!(m::AbstractManager, e::Entity)
end
end

function empty_entities!(m::AbstractManager)
empty!(entities(m))
empty!(free_entities(m))
for c in components(m)
empty!(c)
end
end

function components(manager::AbstractManager, ::Type{T}) where {T<:ComponentData}
comps = AbstractComponent[]
for c in components(manager)
if eltype(c) <: T
push!(comps, c)
end
end
return comps
end

function entity_assert(m::AbstractManager, e::Entity)
es = entities(m)
@assert length(es) >= e.id "$e was never initiated."
@assert es[e.id] != EMPTY_ENTITY "$e was removed previously."
end

function schedule_delete!(m::AbstractManager, e::Entity)
entity_assert(m, e)
push!(to_delete(m), e)
Expand All @@ -191,14 +215,6 @@ function delete_scheduled!(m::AbstractManager)
end
end

function Base.empty!(m::AbstractManager)
empty!(entities(m))
empty!(free_entities(m))
for c in components(m)
empty!(c)
end
end

function update_systems(s::SystemStage, m::AbstractManager)
for s in last(s)
update(s, m)
Expand All @@ -214,9 +230,6 @@ end
update_stage(m::AbstractManager, s::Symbol) =
update_systems(system_stage(m, s), m)

Base.in(::Type{R}, m::AbstractManager) where {R<:ComponentData} =
components(m)[component_id(R)] !== EMPTY_COMPONENT

function prepare(m::AbstractManager)
for s in system_stages(m)
prepare(s, m)
Expand Down
2 changes: 2 additions & 0 deletions src/system.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ const SystemStage = Pair{Symbol, Vector{System}}

Base.push!(s::SystemStage, sys) = push!(last(s), sys)

Base.insert!(s::SystemStage, i::Integer, sys) = insert!(last(s), i, sys)

function requested_components(stage::SystemStage)
comps = Type{<:ComponentData}[]
for s in last(stage)
Expand Down
24 changes: 24 additions & 0 deletions test/test_manager.jl
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,27 @@ delete_scheduled!(m)

@test length(m[Test4]) == 3


empty!(m)
@test isempty(m.entities)
@test isempty(m.system_stages)
@test isempty(m.components)

push!(m, SystemStage(:default, [TestSys()]))

@test length(m.components) == 4

struct TestSys2 <: System end

push!(m, :default, TestSys())

@test length(last(system_stage(m, :default))) == 2

insert!(m, :default, 1, TestSys2())

@test last(system_stage(m, :default))[1] == TestSys2()

insert!(m, 1, SystemStage(:test, [TestSys(), TestSys2()]))

@test first(m.system_stages[1]) == :test

0 comments on commit be9021b

Please sign in to comment.