Skip to content

Commit

Permalink
Refactor/dispatch-component-functions (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrufsvold authored Feb 7, 2024
1 parent f9af0d6 commit b1badc8
Show file tree
Hide file tree
Showing 12 changed files with 172 additions and 121 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,5 @@ src/editor/EditorBuild/
test/projects/ProfilingTest/Platformer/.vscode/settings.json

.vscode/settings.json

playground.jl
13 changes: 7 additions & 6 deletions src/Component/Animation.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module AnimationModule
using ..Component.JulGame
import ..Component
import .JulGame: deprecated_get_property
export Animation
mutable struct Animation
Expand All @@ -18,25 +19,25 @@ module AnimationModule

function Base.getproperty(this::Animation, s::Symbol)
method_props = (
updateArrayValue = update_array_value,
appendArray = append_array,
getType = get_type
updateArrayValue = Component.update_array_value,
appendArray = Component.append_array,
getType = Component.get_type
)
deprecated_get_property(method_props, this, s)
end

function update_array_value(this::Animation, value, field, index::Int32)
function Component.update_array_value(this::Animation, value, field, index::Int32)
fieldToUpdate = getfield(this, field)
if this.getType(value) == "_Vector4"
fieldToUpdate[index] = Math.Vector4(value.x, value.y, value.z, value.t)
end
end

function append_array(this::Animation)
function Component.append_array(this::Animation)
push!(this.frames, Math.Vector4(0,0,0,0))
end

function get_type(this::Animation, item)
function Component.get_type(this::Animation, item)
componentFieldType = "$(typeof(item).name.wrapper)"
return String(split(componentFieldType, '.')[length(split(componentFieldType, '.'))])
end
Expand Down
25 changes: 13 additions & 12 deletions src/Component/Animator.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using ..Component.JulGame.Math
using ..Component.SpriteModule
import ..Component.JulGame: deprecated_get_property
import ..Component
export Animator
struct Animator
animations::Vector{Animation}
Expand Down Expand Up @@ -34,39 +35,39 @@

function Base.getproperty(this::InternalAnimator, s::Symbol)
method_props = (
getLastUpdate = get_last_update,
setLastUpdate = set_last_update,
update = update,
setSprite = set_sprite,
setParent = set_parent,
appendArray = append_array
getLastUpdate = Component.get_last_update,
setLastUpdate = Component.set_last_update,
update = Component.update,
setSprite = Component.set_sprite,
setParent = Component.set_parent,
appendArray = Component.append_array
)

deprecated_get_property(method_props, this, s)
end


function get_last_update(this::InternalAnimator)
function Component.get_last_update(this::InternalAnimator)
return this.lastUpdate
end

function set_last_update(this::InternalAnimator, value)
function Component.set_last_update(this::InternalAnimator, value)
this.lastUpdate = value
end

function update(this::InternalAnimator, currentRenderTime, deltaTime)
function Component.update(this::InternalAnimator, currentRenderTime, deltaTime)
Update(this, currentRenderTime, deltaTime)
end

function set_sprite(this::InternalAnimator, sprite)
function Component.set_sprite(this::InternalAnimator, sprite)
this.sprite = sprite
end

function set_parent(this::InternalAnimator, parent)
function Component.set_parent(this::InternalAnimator, parent)
this.parent = parent
end

function append_array(this::InternalAnimator)
function Component.append_array(this::InternalAnimator)
push!(this.animations, Animation([Math.Vector4(0,0,0,0)], Int32(60)))
end

Expand Down
25 changes: 13 additions & 12 deletions src/Component/CircleCollider.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module CircleColliderModule
using ..Component.JulGame
using ..Component.ColliderModule
import ..Component.JulGame: deprecated_get_property
import ..Component

export CircleCollider
struct CircleCollider
Expand Down Expand Up @@ -47,21 +48,21 @@ module CircleColliderModule

function Base.getproperty(this::CircleCollider, s::Symbol)
method_props = (
getSize = get_size,
checkCollisions = check_collisions,
setParent = set_parent,
getParent = get_parent,
addCollisionEvent = add_collision_event,
getType = get_type
getSize = Component.get_size,
checkCollisions = Component.check_collisions,
setParent = Component.set_parent,
getParent = Component.get_parent,
addCollisionEvent = Component.add_collision_event,
getType = Component.get_type
)
deprecated_get_property(method_props, this, s)
end

function get_size(this::CircleCollider)
function Component.get_size(this::CircleCollider)
return this.size
end

function check_collisions(this::CircleCollider)
function Component.check_collisions(this::CircleCollider)
colliders = MAIN.scene.colliders
#Only check the player against other colliders
counter = 0
Expand Down Expand Up @@ -145,19 +146,19 @@ module CircleColliderModule
this.currentCollisions = []
end

function set_parent(this::CircleCollider, parent::Any)
function Component.set_parent(this::CircleCollider, parent::Any)
this.parent = parent
end

function get_parent(this::CircleCollider)
function Component.get_parent(this::CircleCollider)
return this.parent
end

function add_collision_event(this::CircleCollider, event)
function Component.add_collision_event(this::CircleCollider, event)
push!(this.collisionEvents, event)
end

function get_type(this::CircleCollider)
function Component.get_type(this::CircleCollider)
return "CircleCollider"
end

Expand Down
53 changes: 27 additions & 26 deletions src/Component/Collider.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module ColliderModule
include("../Enums.jl")
using ..Component.JulGame
import ..Component.JulGame: deprecated_get_property
import ..Component

export Collider
struct Collider
Expand Down Expand Up @@ -52,56 +53,56 @@ module ColliderModule

function Base.getproperty(this::InternalCollider, s::Symbol)
method_props = (
getSize = get_size,
setSize = set_size,
getOffset = get_offset,
setOffset = set_offset,
getTag = get_tag,
setTag = set_tag,
getParent = get_parent,
setParent = set_parent,
checkCollisions = check_collisions,
update = update,
addCollisionEvent = add_collision_event,
setVector2fValue = set_vector2f_value,
getType = get_type
getSize = Component.get_size,
setSize = Component.set_size,
getOffset = Component.get_offset,
setOffset = Component.set_offset,
getTag = Component.get_tag,
setTag = Component.set_tag,
getParent = Component.get_parent,
setParent = Component.set_parent,
checkCollisions = Component.check_collisions,
update = Component.update,
addCollisionEvent = Component.add_collision_event,
setVector2fValue = Component.set_vector2f_value,
getType = Component.get_type
)
deprecated_get_property(method_props, this, s)
end

function get_size(this::InternalCollider)
function Component.get_size(this::InternalCollider)
return this.size
end

function set_size(this::InternalCollider, size::Math.Vector2f)
function Component.set_size(this::InternalCollider, size::Math.Vector2f)
this.size = size
end

function get_offset(this::InternalCollider)
function Component.get_offset(this::InternalCollider)
return this.offset
end

function set_offset(this::InternalCollider, offset::Math.Vector2f)
function Component.set_offset(this::InternalCollider, offset::Math.Vector2f)
this.offset = offset
end

function get_tag(this::InternalCollider)
function Component.get_tag(this::InternalCollider)
return this.tag
end

function set_tag(this::InternalCollider, tag::String)
function Component.set_tag(this::InternalCollider, tag::String)
this.tag = tag
end

function get_parent(this::InternalCollider)
function Component.get_parent(this::InternalCollider)
return this.parent
end

function set_parent(this::InternalCollider, parent::Any)
function Component.set_parent(this::InternalCollider, parent::Any)
this.parent = parent
end

function check_collisions(this::InternalCollider)
function Component.check_collisions(this::InternalCollider)
colliders = MAIN.scene.colliders
#Only check the player against other colliders
counter = 0
Expand Down Expand Up @@ -203,19 +204,19 @@ module ColliderModule
this.currentCollisions = InternalCollider[]
end

function update(this::InternalCollider)
function Component.update(this::InternalCollider)

end

function add_collision_event(this::InternalCollider, event)
function Component.add_collision_event(this::InternalCollider, event)
push!(this.collisionEvents, event)
end

function set_vector2f_value(this::InternalCollider, field, x, y)
function Component.set_vector2f_value(this::InternalCollider, field, x, y)
setfield!(this, field, Math.Vector2f(x,y))
end

function get_type(this::InternalCollider)
function Component.get_type(this::InternalCollider)
return "Collider"
end

Expand Down
21 changes: 11 additions & 10 deletions src/Component/Component.jl
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
module Component
using ..JulGame
include("Transform.jl")
include("Sprite.jl")
include("Animation.jl")
include("Animator.jl")
include("Collider.jl")
include("CircleCollider.jl")
include("Rigidbody.jl")
include("Shape.jl")
include("SoundSource.jl")
include("ComponentFunctions.jl")
include("Transform.jl")
include("Sprite.jl")
include("Animation.jl")
include("Animator.jl")
include("Collider.jl")
include("CircleCollider.jl")
include("Rigidbody.jl")
include("Shape.jl")
include("SoundSource.jl")

export AnimationModule
export AnimatorModule
Expand All @@ -19,4 +20,4 @@ module Component
export SoundSourceModule
export SpriteModule
export TransformModule
end
end
39 changes: 39 additions & 0 deletions src/Component/ComponentFunctions.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Declare Common Functions so that they can be dispatched from ModuleExtensions
function add_collision_event end
function append_array end
function append_array end
function apply_forces end
function check_collisions end
function destroy end
function draw end
function draw end
function flip end
function get_last_update end
function get_offset end
function get_parent end
function get_position end
function get_rotation end
function get_scale end
function get_size end
function get_tag end
function get_type end
function get_velocity end
function initialize end
function load_image end
function load_sound end
function set_color end
function set_last_update end
function set_offset end
function set_parent end
function set_position end
function set_rotation end
function set_scale end
function set_size end
function set_sprite end
function set_tag end
function set_vector2f_value end
function stop_music end
function toggle_sound end
function unload_sound end
function update end
function update_array_value end
Loading

0 comments on commit b1badc8

Please sign in to comment.