Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyjor committed Aug 23, 2024
1 parent 29b8b75 commit aebf4af
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 24 deletions.
8 changes: 4 additions & 4 deletions src/Component/Collider.jl
Original file line number Diff line number Diff line change
Expand Up @@ -198,23 +198,23 @@ module ColliderModule
if collision[1] == Left::CollisionDirection
push!(this.currentCollisions, collider)
for eventToCall in this.collisionEvents
eventToCall((collider=collider, direction=collision[1]))
Base.invokelatest(eventToCall,(collider=collider, direction=collision[1]))
end
#Begin to overlap, correct position
Component.get_parent(this).transform.position = Math.Vector2f(transform.position.x + collision[2], transform.position.y)
end
if collision[1] == Right::CollisionDirection
push!(this.currentCollisions, collider)
for eventToCall in this.collisionEvents
eventToCall((collider=collider, direction=collision[1]))
Base.invokelatest(eventToCall,(collider=collider, direction=collision[1]))
end
#Begin to overlap, correct position
Component.get_parent(this).transform.position = Math.Vector2f(transform.position.x - collision[2], transform.position.y)
end
if collision[1] == Bottom::CollisionDirection
push!(this.currentCollisions, collider)
for eventToCall in this.collisionEvents
eventToCall((collider=collider, direction=collision[1]))
Base.invokelatest(eventToCall,(collider=collider, direction=collision[1]))
end
#Begin to overlap, correct position
Component.get_parent(this).transform.position = Math.Vector2f(transform.position.x, transform.position.y - collision[2])
Expand All @@ -223,7 +223,7 @@ module ColliderModule
if collision[1] == Below::ColliderLocation
push!(this.currentCollisions, collider)
for eventToCall in this.collisionEvents
eventToCall((collider=collider, direction=collision[1]))
Base.invokelatest(eventToCall,(collider=collider, direction=collision[1]))
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion src/Entity.jl
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ module EntityModule
function JulGame.update(this::Entity, deltaTime)
for script in this.scripts
try
JulGame.update(script, deltaTime)
Base.invokelatest(JulGame.update, script, deltaTime)
catch e
@error string(e)
Base.show_backtrace(stdout, catch_backtrace())
Expand Down
26 changes: 9 additions & 17 deletions src/Main.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,9 @@ module MainLoop
input::Input
isGameModeRunningInEditor::Bool
isWindowFocused::Bool
lastMousePosition::Union{Math.Vector2, Math.Vector2f}
lastMousePositionWorld::Union{Math.Vector2, Math.Vector2f}
level::JulGame.SceneManagement.SceneBuilderModule.Scene
mousePositionWorld::Union{Math.Vector2, Math.Vector2f}
mousePositionWorldRaw::Union{Math.Vector2, Math.Vector2f}
optimizeSpriteRendering::Bool
panCounter::Union{Math.Vector2, Math.Vector2f}
panThreshold::Float64
scene::SceneModule.Scene
selectedEntity::Union{Entity, Nothing}
selectedUIElementIndex::Int64
Expand Down Expand Up @@ -57,8 +52,6 @@ module MainLoop
this.input.scene = this.scene
this.isWindowFocused = false
this.mousePositionWorld = Math.Vector2f()
this.mousePositionWorldRaw = Math.Vector2f()
this.lastMousePositionWorld = Math.Vector2f()
this.optimizeSpriteRendering = false
this.selectedEntity = nothing
this.selectedUIElementIndex = -1
Expand Down Expand Up @@ -234,10 +227,6 @@ function initialize_scripts_and_components(isUsingEditor::Bool = false)
end
end

this.lastMousePosition = Math.Vector2(0, 0)
this.panCounter = Math.Vector2f(0, 0)
this.panThreshold = .1

this.spriteLayers = build_sprite_layers()

if !isUsingEditor || this.isGameModeRunningInEditor
Expand All @@ -253,6 +242,7 @@ function initialize_scripts_and_components(isUsingEditor::Bool = false)
#end
end
end
build_sprite_layers()
end
end

Expand Down Expand Up @@ -487,7 +477,6 @@ function game_loop(this::Main, startTime::Ref{UInt64} = Ref(UInt64(0)), lastPhys

DEBUG = false
#region Input
this.lastMousePosition = this.input.mousePosition
if !isEditor
JulGame.InputModule.poll_input(this.input)
end
Expand All @@ -504,14 +493,16 @@ function game_loop(this::Main, startTime::Ref{UInt64} = Ref(UInt64(0)), lastPhys
end

#region Physics
if !isEditor
if !isEditor || this.isGameModeRunningInEditor
currentPhysicsTime = SDL2.SDL_GetTicks()
deltaTime = (currentPhysicsTime - lastPhysicsTime[]) / 1000.0

this.currentTestTime += deltaTime
if deltaTime > .25
lastPhysicsTime[] = SDL2.SDL_GetTicks()
return
println("Delta time: ", deltaTime)

#return
end
for rigidbody in this.scene.rigidbodies
try
Expand All @@ -525,6 +516,8 @@ function game_loop(this::Main, startTime::Ref{UInt64} = Ref(UInt64(0)), lastPhys
end
lastPhysicsTime[] = currentPhysicsTime
end
println("Game loop")


#region Rendering
currentRenderTime = SDL2.SDL_GetTicks()
Expand All @@ -536,7 +529,7 @@ function game_loop(this::Main, startTime::Ref{UInt64} = Ref(UInt64(0)), lastPhys
continue
end

if !isEditor
if !isEditor || this.isGameModeRunningInEditor
try
JulGame.update(entity, deltaTime)
if this.close
Expand Down Expand Up @@ -579,6 +572,7 @@ function game_loop(this::Main, startTime::Ref{UInt64} = Ref(UInt64(0)), lastPhys

sort!(renderOrder, by = x -> x[1])

println("Entities: ", length(this.scene.entities), " Render Order: ", length(renderOrder))
for i = eachindex(renderOrder)
try
rendercount += 1
Expand Down Expand Up @@ -642,9 +636,7 @@ function game_loop(this::Main, startTime::Ref{UInt64} = Ref(UInt64(0)), lastPhys
JulGame.render(uiElement, DEBUG)
end

this.lastMousePositionWorld = this.mousePositionWorld
pos1::Math.Vector2 = windowPos !== nothing ? windowPos : Math.Vector2(0, 0)
this.mousePositionWorldRaw = Math.Vector2f((this.input.mousePosition.x - pos1.x + (this.scene.camera.position.x * SCALE_UNITS * this.zoom)) / SCALE_UNITS / this.zoom, ( this.input.mousePosition.y - pos1.y + (this.scene.camera.position.y * SCALE_UNITS * this.zoom)) / SCALE_UNITS / this.zoom)
this.mousePositionWorld = Math.Vector2(floor(Int32,(this.input.mousePosition.x + (this.scene.camera.position.x * SCALE_UNITS * this.zoom)) / SCALE_UNITS / this.zoom), floor(Int32,( this.input.mousePosition.y + (this.scene.camera.position.y * SCALE_UNITS * this.zoom)) / SCALE_UNITS / this.zoom))
rawMousePos = Math.Vector2f(this.input.mousePosition.x - pos1.x , this.input.mousePosition.y - pos1.y )
#region Debug
Expand Down
1 change: 0 additions & 1 deletion src/SceneManagement/SceneBuilder.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ module SceneBuilderModule
using ...TextBoxModule
using ...ScreenButtonModule
using ..SceneReaderModule
using Revise

function init()
# if end of path is "test", then we are running tests
Expand Down
2 changes: 1 addition & 1 deletion src/editor/JulGameEditor/Utils/EditorUtils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ end

function reset_camera_event(main)
event = @event begin
main.camera.position = Vec2(0, 0)
main.scene.camera.position = JulGame.Math.Vector2f(0, 0)
end

return event
Expand Down

0 comments on commit aebf4af

Please sign in to comment.