diff --git a/.gitignore b/.gitignore index 64335931..1c954a2a 100644 --- a/.gitignore +++ b/.gitignore @@ -34,3 +34,5 @@ src/editor/EditorBuild/ test/projects/ProfilingTest/Platformer/.vscode/settings.json .vscode/settings.json + +playground.jl diff --git a/src/Component/Animation.jl b/src/Component/Animation.jl index bcb54666..ace07b59 100644 --- a/src/Component/Animation.jl +++ b/src/Component/Animation.jl @@ -1,5 +1,6 @@ module AnimationModule using ..Component.JulGame + import ..Component import .JulGame: deprecated_get_property export Animation mutable struct Animation @@ -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 diff --git a/src/Component/Animator.jl b/src/Component/Animator.jl index 0cf4eb7a..fafb9f7d 100644 --- a/src/Component/Animator.jl +++ b/src/Component/Animator.jl @@ -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} @@ -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 diff --git a/src/Component/CircleCollider.jl b/src/Component/CircleCollider.jl index 2c5134cf..b930a2f2 100644 --- a/src/Component/CircleCollider.jl +++ b/src/Component/CircleCollider.jl @@ -3,6 +3,7 @@ module CircleColliderModule using ..Component.JulGame using ..Component.ColliderModule import ..Component.JulGame: deprecated_get_property + import ..Component export CircleCollider struct CircleCollider @@ -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 @@ -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 diff --git a/src/Component/Collider.jl b/src/Component/Collider.jl index be27f42f..90e6dd5b 100644 --- a/src/Component/Collider.jl +++ b/src/Component/Collider.jl @@ -2,6 +2,7 @@ module ColliderModule include("../Enums.jl") using ..Component.JulGame import ..Component.JulGame: deprecated_get_property + import ..Component export Collider struct Collider @@ -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 @@ -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 diff --git a/src/Component/Component.jl b/src/Component/Component.jl index 2f8832f7..0442e6d8 100644 --- a/src/Component/Component.jl +++ b/src/Component/Component.jl @@ -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 @@ -19,4 +20,4 @@ module Component export SoundSourceModule export SpriteModule export TransformModule -end \ No newline at end of file +end diff --git a/src/Component/ComponentFunctions.jl b/src/Component/ComponentFunctions.jl new file mode 100644 index 00000000..3b775cc8 --- /dev/null +++ b/src/Component/ComponentFunctions.jl @@ -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 diff --git a/src/Component/Rigidbody.jl b/src/Component/Rigidbody.jl index c6a8ed88..2993c859 100644 --- a/src/Component/Rigidbody.jl +++ b/src/Component/Rigidbody.jl @@ -1,6 +1,7 @@ module RigidbodyModule using ..Component.JulGame import ..Component.JulGame: deprecated_get_property + import ..Component export Rigidbody struct Rigidbody mass::Float64 @@ -41,16 +42,16 @@ function Base.getproperty(this::InternalRigidbody, s::Symbol) # Todo: update this based on offset and scale method_props = ( - update = update, - applyForces = apply_forces, - getVelocity = get_velocity, - getParent = get_parent, - setVector2fValue = set_vector2f_value + update = Component.update, + applyForces = Component.apply_forces, + getVelocity = Component.get_velocity, + getParent = Component.get_parent, + setVector2fValue = Component.set_vector2f_value ) deprecated_get_property(method_props, this, s) end - function update(this::InternalRigidbody, dt) + function Component.update(this::InternalRigidbody, dt) velocityMultiplier = Math.Vector2f(1.0, 1.0) transform = this.parent.transform currentPosition = transform.getPosition() @@ -72,22 +73,22 @@ end end - function apply_forces(this::InternalRigidbody) + function Component.apply_forces(this::InternalRigidbody) gravityAcceleration = Math.Vector2f(0.0, this.useGravity ? GRAVITY : 0.0) dragForce = 0.5 * this.drag * (this.velocity * this.velocity) dragAcceleration = dragForce / this.mass return gravityAcceleration - dragAcceleration end - function get_velocity(this::InternalRigidbody) + function Component.get_velocity(this::InternalRigidbody) return this.velocity end - function get_parent(this::InternalRigidbody) + function Component.get_parent(this::InternalRigidbody) return this.parent end - function set_vector2f_value(this::InternalRigidbody, field, x, y) + function Component.set_vector2f_value(this::InternalRigidbody, field, x, y) setfield!(this, field, Math.Vector2f(x,y)) end diff --git a/src/Component/Shape.jl b/src/Component/Shape.jl index d1480064..9d3a76e0 100644 --- a/src/Component/Shape.jl +++ b/src/Component/Shape.jl @@ -1,7 +1,7 @@ module ShapeModule using ..Component.JulGame import ..Component.JulGame: deprecated_get_property - + import ..Component export Shape struct Shape color::Math.Vector3 @@ -39,13 +39,13 @@ module ShapeModule function Base.getproperty(this::InternalShape, s::Symbol) method_props = ( - draw = draw, - setParent = set_parent + draw = Component.draw, + setParent = Component.set_parent ) deprecated_get_property(method_props, this, s) end - function draw(this::InternalShape) + function Component.draw(this::InternalShape) if JulGame.Renderer == C_NULL return end @@ -69,7 +69,7 @@ module ShapeModule end - function set_parent(this::InternalShape, parent::Any) + function Component.set_parent(this::InternalShape, parent::Any) this.parent = parent end end diff --git a/src/Component/SoundSource.jl b/src/Component/SoundSource.jl index 2efde7d2..dfedfcd4 100644 --- a/src/Component/SoundSource.jl +++ b/src/Component/SoundSource.jl @@ -1,6 +1,7 @@ module SoundSourceModule using ..JulGame import ..JulGame: deprecated_get_property + import ..Component export SoundSource struct SoundSource @@ -53,16 +54,16 @@ module SoundSourceModule function Base.getproperty(this::InternalSoundSource, s::Symbol) method_props = ( - toggleSound = toggle_sound, - stopMusic = stop_music, - loadSound = load_sound, - unloadSound = unload_sound, - setParent = set_parent + toggleSound = Component.toggle_sound, + stopMusic = Component.stop_music, + loadSound = Component.load_sound, + unloadSound = Component.unload_sound, + setParent = Component.set_parent ) deprecated_get_property(method_props, this, s) end - function toggle_sound(this::InternalSoundSource, loops = 0) + function Component.toggle_sound(this::InternalSoundSource, loops = 0) if this.isMusic if SDL2.Mix_PlayingMusic() == 0 SDL2.Mix_PlayMusic( this.sound, Int32(-1) ) @@ -78,11 +79,11 @@ module SoundSourceModule end end - function stop_music(this::InternalSoundSource) + function Component.stop_music(this::InternalSoundSource) SDL2.Mix_HaltMusic() end - function load_sound(this::InternalSoundSource, soundPath::String, isMusic::Bool) + function Component.load_sound(this::InternalSoundSource, soundPath::String, isMusic::Bool) this.isMusic = isMusic this.sound = this.isMusic ? SDL2.Mix_LoadMUS(joinpath(BasePath, "assets", "sounds", soundPath)) : SDL2.Mix_LoadWAV(joinpath(BasePath, "assets", "sounds", soundPath)) error = unsafe_string(SDL2.SDL_GetError()) @@ -95,7 +96,7 @@ module SoundSourceModule this.path = soundPath end - function unload_sound(this::InternalSoundSource) + function Component.unload_sound(this::InternalSoundSource) if this.isMusic SDL2.Mix_FreeMusic(this.sound) else @@ -104,7 +105,7 @@ module SoundSourceModule this.sound = C_NULL end - function set_parent(this::InternalSoundSource, parent::Any) + function Component.set_parent(this::InternalSoundSource, parent::Any) this.parent = parent end diff --git a/src/Component/Sprite.jl b/src/Component/Sprite.jl index 65399ed7..0f3f53a0 100644 --- a/src/Component/Sprite.jl +++ b/src/Component/Sprite.jl @@ -1,6 +1,7 @@ module SpriteModule using ..Component.JulGame import ..Component.JulGame: deprecated_get_property + import ..Component export Sprite struct Sprite @@ -76,17 +77,17 @@ module SpriteModule function Base.getproperty(this::InternalSprite, s::Symbol) method_props = ( - draw = draw, - initialize = initialize, - flip = flip, - setParent = set_parent, - loadImage = load_image, - destroy = destroy, - setColor = set_color + draw = Component.draw, + initialize = Component.initialize, + flip = Component.flip, + setParent = Component.set_parent, + loadImage = Component.load_image, + destroy = Component.destroy, + setColor = Component.set_color ) deprecated_get_property(method_props, this, s) end - function draw(this::InternalSprite, zoom::Float64 = 1.0) + function Component.draw(this::InternalSprite, zoom::Float64 = 1.0) if this.image == C_NULL || JulGame.Renderer == C_NULL return end @@ -166,7 +167,7 @@ module SpriteModule end end - function initialize(this::InternalSprite) + function Component.initialize(this::InternalSprite) if this.image == C_NULL return end @@ -174,15 +175,15 @@ module SpriteModule this.texture = SDL2.SDL_CreateTextureFromSurface(JulGame.Renderer, this.image) end - function flip(this::InternalSprite) + function Component.flip(this::InternalSprite) this.isFlipped = !this.isFlipped end - function set_parent(this::InternalSprite, parent::Any) + function Component.set_parent(this::InternalSprite, parent::Any) this.parent = parent end - function load_image(this::InternalSprite, imagePath::String) + function Component.load_image(this::InternalSprite, imagePath::String) SDL2.SDL_ClearError() this.image = SDL2.IMG_Load(joinpath(BasePath, "assets", "images", imagePath)) error = unsafe_string(SDL2.SDL_GetError()) @@ -201,7 +202,7 @@ module SpriteModule this.setColor() end - function destroy(this::InternalSprite) + function Component.destroy(this::InternalSprite) if this.image == C_NULL return end @@ -212,7 +213,7 @@ module SpriteModule this.texture = C_NULL end - function set_color(this::InternalSprite) + function Component.set_color(this::InternalSprite) SDL2.SDL_SetTextureColorMod(this.texture, UInt8(this.color.x%256), UInt8(this.color.y%256), (this.color.z%256)); end end diff --git a/src/Component/Transform.jl b/src/Component/Transform.jl index 531ed8af..11da4454 100644 --- a/src/Component/Transform.jl +++ b/src/Component/Transform.jl @@ -1,6 +1,8 @@ module TransformModule using ..Component.JulGame import ..Component.JulGame: deprecated_get_property + import ..Component + export Transform mutable struct Transform rotation::Float64 @@ -20,48 +22,48 @@ module TransformModule function Base.getproperty(this::Transform, s::Symbol) method_props = ( - getPosition = get_position, - setPosition = set_position, - getScale = get_scale, - setScale = set_scale, - getRotation = get_rotation, - setRotation = set_rotation, - update = update, - setVector2fValue = set_vector2f_value + getPosition = Component.get_position, + setPosition = Component.set_position, + getScale = Component.get_scale, + setScale = Component.set_scale, + getRotation = Component.get_rotation, + setRotation = Component.set_rotation, + update = Component.update, + setVector2fValue = Component.set_vector2f_value ) deprecated_get_property(method_props, this, s) end - function get_position(this::Transform) + function Component.get_position(this::Transform) return this.position end - function set_position(this::Transform, position::Math.Vector2f) + function Component.set_position(this::Transform, position::Math.Vector2f) this.position = position end - function get_scale(this::Transform) + function Component.get_scale(this::Transform) return this.scale end - function set_scale(this::Transform, scale::Math.Vector2f) + function Component.set_scale(this::Transform, scale::Math.Vector2f) this.scale = scale end - function get_rotation(this::Transform) + function Component.get_rotation(this::Transform) return this.rotation end - function set_rotation(this::Transform, rotation::Float64) + function Component.set_rotation(this::Transform, rotation::Float64) this.rotation = rotation end - function update(this::Transform) + function Component.update(this::Transform) #println(this.position) end - function set_vector2f_value(this::Transform, field, x, y) + function Component.set_vector2f_value(this::Transform, field, x, y) setfield!(this, field, Math.Vector2f(x,y)) end end