mirror of
https://github.com/TimeCrafters/timecrafters_configuration_tool_desktop.git
synced 2025-12-15 21:32:35 +00:00
Used a fixed time step for Simulator
This commit is contained in:
@@ -10,7 +10,6 @@ module TAC
|
|||||||
save_source
|
save_source
|
||||||
|
|
||||||
begin
|
begin
|
||||||
@simulation_start_time = Gosu.milliseconds
|
|
||||||
@simulation = TAC::Simulator::Simulation.new(source_code: @source_code.value, field_container: @field_container)
|
@simulation = TAC::Simulator::Simulation.new(source_code: @source_code.value, field_container: @field_container)
|
||||||
@simulation.start
|
@simulation.start
|
||||||
rescue SyntaxError, NameError, NoMethodError, TypeError, ArgumentError => e
|
rescue SyntaxError, NameError, NoMethodError, TypeError, ArgumentError => e
|
||||||
@@ -78,7 +77,7 @@ robot.forward 100"
|
|||||||
@simulation.update
|
@simulation.update
|
||||||
|
|
||||||
unless @simulation.robots.all? { |robot| robot.queue.empty? } # Only update clock if simulation is running
|
unless @simulation.robots.all? { |robot| robot.queue.empty? } # Only update clock if simulation is running
|
||||||
@simulation_status.value = "Time: #{((Gosu.milliseconds - @simulation_start_time) / 1000.0).round(1)} seconds" if @simulation_start_time
|
@simulation_status.value = "Time: #{(@simulation.simulation_time).round(1)} seconds"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
module TAC
|
module TAC
|
||||||
class Simulator
|
class Simulator
|
||||||
class Simulation
|
class Simulation
|
||||||
attr_reader :robots, :show_paths
|
attr_reader :robots, :show_paths, :simulation_time
|
||||||
|
|
||||||
def initialize(source_code:, field_container:)
|
def initialize(source_code:, field_container:)
|
||||||
@source_code = source_code
|
@source_code = source_code
|
||||||
@field_container = field_container
|
@field_container = field_container
|
||||||
@@ -11,6 +12,9 @@ module TAC
|
|||||||
@show_paths = false
|
@show_paths = false
|
||||||
|
|
||||||
@last_milliseconds = Gosu.milliseconds
|
@last_milliseconds = Gosu.milliseconds
|
||||||
|
@simulation_step = 1.0 / 60.0
|
||||||
|
@accumulator = 0.0
|
||||||
|
@simulation_time = 0.0
|
||||||
end
|
end
|
||||||
|
|
||||||
def start
|
def start
|
||||||
@@ -23,8 +27,15 @@ module TAC
|
|||||||
end
|
end
|
||||||
|
|
||||||
def update
|
def update
|
||||||
@field.update
|
@accumulator += (Gosu.milliseconds - @last_milliseconds) / 1000.0
|
||||||
@robots.each { |robot| robot.update((Gosu.milliseconds - @last_milliseconds) / 1000.0) }
|
|
||||||
|
while(@accumulator > @simulation_step)
|
||||||
|
@field.update
|
||||||
|
@robots.each { |robot| robot.update(@simulation_step) }
|
||||||
|
|
||||||
|
@accumulator -= @simulation_step
|
||||||
|
@simulation_time += @simulation_step
|
||||||
|
end
|
||||||
|
|
||||||
@last_milliseconds = Gosu.milliseconds
|
@last_milliseconds = Gosu.milliseconds
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user