Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor/move call sites to functional #60

Merged
merged 8 commits into from
Feb 7, 2024
Merged
5 changes: 3 additions & 2 deletions src/Camera.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using JulGame
using .Math
import JulGame: deprecated_get_property
import JulGame.Component: get_position, get_scale

mutable struct Camera
dimensions::Vector2
Expand Down Expand Up @@ -47,9 +48,9 @@ function update(this::Camera, newPosition = C_NULL)
SDL2.SDL_RenderFillRectF(Renderer, Ref(SDL2.SDL_FRect(this.windowPos.x, this.windowPos.y, this.dimensions.x, this.dimensions.y)))

if this.target != C_NULL && newPosition == C_NULL
targetPos = this.target.getPosition()
targetPos = get_position(this.target)
center = Vector2f(this.dimensions.x/SCALE_UNITS/2, this.dimensions.y/SCALE_UNITS/2)
targetScale = this.target.getScale()
targetScale = get_scale(this.target)
this.position = targetPos - center + 0.5 * targetScale + this.offset
return
end
Expand Down
51 changes: 51 additions & 0 deletions src/CommonFunctions.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
function add_animator end
function add_circle_collider end
function add_click_event end
function add_collider end
function add_collision_event end
function add_rigidbody end
function add_script end
function add_shape end
function add_sound_source end
function add_sprite end
function append_array end
function apply_forces end
function center_text end
function check_collisions end
function create_sound_source end
function destroy 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 handle_event end
function initialize end
function load_image end
function load_sound end
function render 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_vector2_value 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
function update_text end
76 changes: 38 additions & 38 deletions src/Component/ComponentFunctions.jl
Original file line number Diff line number Diff line change
@@ -1,39 +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
import ..JulGame: add_collision_event,
append_array,
append_array,
apply_forces,
check_collisions,
destroy,
draw,
draw,
flip,
get_last_update,
get_offset,
get_parent,
get_position,
get_rotation,
get_scale,
get_size,
get_tag,
get_type,
get_velocity,
initialize,
load_image,
load_sound,
set_color,
set_last_update,
set_offset,
set_parent,
set_position,
set_rotation,
set_scale,
set_size,
set_sprite,
set_tag,
set_vector2f_value,
stop_music,
toggle_sound,
unload_sound,
update,
update_array_value
46 changes: 24 additions & 22 deletions src/Entity.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
using ..JulGame.SpriteModule
using ..JulGame.TransformModule
import ..JulGame: deprecated_get_property
import ..JulGame: Component
import ..JulGame

export Entity
mutable struct Entity
Expand Down Expand Up @@ -53,22 +55,22 @@

function Base.getproperty(this::Entity, s::Symbol)
method_props = (
addScript = add_script,
update = update,
addAnimator = add_animator,
addCollider = add_collider,
addCircleCollider = add_circle_collider,
addRigidbody = add_rigidbody,
addSoundSource = add_sound_source,
createSoundSource = create_sound_source,
addSprite = add_sprite,
addShape = add_shape
addScript = JulGame.add_script,
update = JulGame.update,
addAnimator = JulGame.add_animator,
addCollider = JulGame.add_collider,
addCircleCollider = JulGame.add_circle_collider,
addRigidbody = JulGame.add_rigidbody,
addSoundSource = JulGame.add_sound_source,
createSoundSource = JulGame.create_sound_source,
addSprite = JulGame.add_sprite,
addShape = JulGame.add_shape
)
deprecated_get_property(method_props, this, s)
end


function add_script(this::Entity, script)
function JulGame.add_script(this::Entity, script)

Check warning on line 73 in src/Entity.jl

View check run for this annotation

Codecov / codecov/patch

src/Entity.jl#L73

Added line #L73 was not covered by tests
#println(string("Adding script of type: ", typeof(script), " to entity named " , this.name))
push!(this.scripts, script)
script.setParent(this)
Expand All @@ -80,7 +82,7 @@
end
end

function update(this::Entity, deltaTime)
function JulGame.update(this::Entity, deltaTime)
for script in this.scripts
try
script.update(deltaTime)
Expand All @@ -91,21 +93,21 @@
end
end

function add_animator(this::Entity, animator::Animator = Animator(Animation[Animation(Vector4[Vector4(0,0,0,0)], Int32(60))]))
function JulGame.add_animator(this::Entity, animator::Animator = Animator(Animation[Animation(Vector4[Vector4(0,0,0,0)], Int32(60))]))
if this.animator != C_NULL
println("Animator already exists on entity named ", this.name)
return
end

this.animator = InternalAnimator(this::Entity, animator.animations)
if this.sprite != C_NULL
this.animator.setSprite(this.sprite)
Component.set_sprite(this.animator, this.sprite)
end

return this.animator
end

function add_collider(this::Entity, collider::Collider = Collider(true, false, false, Vector2f(0,0), Vector2f(1,1), "Default"))
function JulGame.add_collider(this::Entity, collider::Collider = Collider(true, false, false, Vector2f(0,0), Vector2f(1,1), "Default"))
if this.collider != C_NULL || this.circleCollider != C_NULL
println("Collider already exists on entity named ", this.name)
return
Expand All @@ -116,7 +118,7 @@
return this.collider
end

function add_circle_collider(this::Entity, collider::CircleCollider = CircleCollider(1.0, true, false, Vector2f(0,0), "Default"))
function JulGame.add_circle_collider(this::Entity, collider::CircleCollider = CircleCollider(1.0, true, false, Vector2f(0,0), "Default"))
if this.collider != C_NULL || this.circleCollider != C_NULL
println("Collider already exists on entity named ", this.name)
return
Expand All @@ -127,7 +129,7 @@
return this.circleCollider
end

function add_rigidbody(this::Entity, rigidbody::Rigidbody = Rigidbody())
function JulGame.add_rigidbody(this::Entity, rigidbody::Rigidbody = Rigidbody())
if this.rigidbody != C_NULL
println("Rigidbody already exists on entity named ", this.name)
return
Expand All @@ -138,7 +140,7 @@
return this.rigidbody
end

function add_sound_source(this::Entity, soundSource::SoundSource = SoundSource(Int32(-1), false, "", Int32(50)))
function JulGame.add_sound_source(this::Entity, soundSource::SoundSource = SoundSource(Int32(-1), false, "", Int32(50)))
if this.soundSource != C_NULL
println("SoundSource already exists on entity named ", this.name)
return
Expand All @@ -149,27 +151,27 @@
return this.soundSource
end

function create_sound_source(this::Entity, soundSource::SoundSource = SoundSource(Int32(-1), false, "", Int32(50)))
function JulGame.create_sound_source(this::Entity, soundSource::SoundSource = SoundSource(Int32(-1), false, "", Int32(50)))
newSoundSource::InternalSoundSource = InternalSoundSource(this::Entity, soundSource.path, soundSource.channel, soundSource.volume, soundSource.isMusic)
return newSoundSource
end

function add_sprite(this::Entity, isCreatedInEditor::Bool = false, sprite::Sprite = Sprite(Math.Vector3(255, 255, 255), C_NULL, false, "", true, 0, Math.Vector2f(0,0), Math.Vector2f(0,0), 0, -1))
function JulGame.add_sprite(this::Entity, isCreatedInEditor::Bool = false, sprite::Sprite = Sprite(Math.Vector3(255, 255, 255), C_NULL, false, "", true, 0, Math.Vector2f(0,0), Math.Vector2f(0,0), 0, -1))
if this.sprite != C_NULL
println("Sprite already exists on entity named ", this.name)
return
end

this.sprite = InternalSprite(this::Entity, sprite.imagePath, sprite.crop, sprite.isFlipped, sprite.color, isCreatedInEditor; pixelsPerUnit=sprite.pixelsPerUnit, isWorldEntity=sprite.isWorldEntity, position=sprite.position, rotation=sprite.rotation, layer=sprite.layer)
if this.animator != C_NULL
this.animator.setSprite(this.sprite)
Component.set_sprite(this.animator, this.sprite)

Check warning on line 167 in src/Entity.jl

View check run for this annotation

Codecov / codecov/patch

src/Entity.jl#L167

Added line #L167 was not covered by tests
end
this.sprite.initialize()

return this.sprite
end

function add_shape(this::Entity, shape::Shape = Shape(Math.Vector3(255,0,0), Math.Vector2f(1,1), true, false, Math.Vector2f(0,0), Math.Vector2f(0,0)))
function JulGame.add_shape(this::Entity, shape::Shape = Shape(Math.Vector3(255,0,0), Math.Vector2f(1,1), true, false, Math.Vector2f(0,0), Math.Vector2f(0,0)))
if this.shape != C_NULL
println("Shape already exists on entity named ", this.name)
return
Expand Down
1 change: 1 addition & 0 deletions src/JulGame.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module JulGame
include("CommonFunctions.jl")
using SimpleDirectMediaLayer
const SDL2 = SimpleDirectMediaLayer

Expand Down
Loading
Loading