Fleshed out simulator

This commit is contained in:
2020-06-10 11:33:38 -05:00
parent 8d2a9fb1eb
commit ca750c3b19
4 changed files with 140 additions and 34 deletions

View File

@@ -1,30 +1,104 @@
module TAC
class Simulator
class Field
def initialize(container:, season:, robot:)
def initialize(container:, season:, simulation:, robot:)
@container = container
@season = season
@simulation = simulation
@robot = robot
@position = CyberarmEngine::Vector.new
@scale = CyberarmEngine::Vector.new(1, 1)
@scale = 1
@size = 0
@field_size = 144 # inches [1 pxel = 1 inch]
@blue = Gosu::Color.new(0xff_004080)
@red = Gosu::Color.new(0xff_800000)
end
def draw
Gosu.clip_to(@position.x, @position.y, @size, @size) do
Gosu.draw_rect(@position.x, @position.y, @size, @size, Gosu::Color::GRAY)
# Gosu.scale(@scale.x, @scale.y) do
Gosu.translate(@position.x, @position.y) do
Gosu.translate(@position.x, @position.y) do
Gosu.scale(@scale) do
draw_field
self.send(:"draw_field_#{@season}")
@robot.draw
@simulation.__queue.first.draw if @simulation.__queue.first
end
# end
end
end
end
def draw_field
Gosu.draw_rect(0, 0, @field_size, @field_size, Gosu::Color::GRAY)
6.times do |i| # Tile lines across
next if i == 0
Gosu.draw_rect(@field_size / 6 * i, 0, 1, @field_size, Gosu::Color::BLACK)
end
6.times do |i| # Tile lines down
next if i == 0
Gosu.draw_rect(0, @field_size / 6 * i, @field_size, 1, Gosu::Color::BLACK)
end
end
def draw_field_skystone
# blue bridge
Gosu.draw_rect(0, @field_size / 2 - 2, 49, 1, @blue)
Gosu.draw_rect(0, @field_size / 2 + 2, 49, 1, @blue)
# mid bridge
Gosu.draw_rect(@field_size / 2 - 24, @field_size / 2 - 9.25, 49, 18.5, Gosu::Color.new(0xff_222222))
Gosu.draw_rect(@field_size / 2 - 24, @field_size / 2 - 2, 49, 1, Gosu::Color::YELLOW)
Gosu.draw_rect(@field_size / 2 - 24, @field_size / 2 + 2, 49, 1, Gosu::Color::YELLOW)
# blue bridge
Gosu.draw_rect(@field_size - 47, @field_size / 2 - 2, 49, 1, @red)
Gosu.draw_rect(@field_size - 47, @field_size / 2 + 2, 49, 1, @red)
# blue build site
Gosu.draw_quad(
24 - 2, 0, @blue,
24, 0, @blue,
0, 24 - 2, @blue,
0, 24, @blue
)
# red build site
Gosu.draw_quad(
@field_size - (24 - 2), 0, @red,
@field_size - (24 - 0), 0, @red,
@field_size, 24 - 2, @red,
@field_size, 24, @red
)
# blue depot
Gosu.draw_rect(@field_size - 23, @field_size - 23, 24, 2, @blue)
Gosu.draw_rect(@field_size - 23, @field_size - 23, 2, 24, @blue)
# red depot
Gosu.draw_rect(-1, @field_size - 23, 24, 2, @red)
Gosu.draw_rect(22, @field_size - 23, 2, 24, @red)
# blue foundation
Gosu.draw_rect(48, 4, 18.5, 34.5, @blue)
# red foundation
Gosu.draw_rect(@field_size - (47 + 18.5), 4, 18.5, 34.5, @red)
# stones
6.times do |i|
Gosu.draw_rect(48, @field_size - 8 * i - 8, 4, 8, Gosu::Color::YELLOW)
end
6.times do |i|
Gosu.draw_rect(@field_size - (47 + 4), @field_size - 8 * i - 8, 4, 8, Gosu::Color::YELLOW)
end
end
def update
@position.x, @position.y = @container.x, @container.y
@size = @container.width
@scale = @size.to_f / @field_size
end
end
end

View File

@@ -10,8 +10,13 @@ module TAC
end
def draw
Gosu.rotate(@angle, @position.x, @position.y) do
Gosu.draw_rect(@position.x - @width / 2, @position.y - @depth / 2, @width, @depth, Gosu::Color::GREEN)
Gosu.translate(@width / 2, @depth / 2) do
Gosu.rotate(@angle, @position.x, @position.y) do
Gosu.draw_rect(@position.x - @width / 2, @position.y - @depth / 2, @width, @depth, Gosu::Color::BLACK)
Gosu.draw_rect(@position.x - @width / 2 + 1, @position.y - @depth / 2 + 1, @width - 2, @depth - 2, Gosu::Color.new(0xff_808022))
Gosu.draw_arc(@position.x, @position.y, 6, 1.0, 32, 2, TAC::Palette::TIMECRAFTERS_PRIMARY)
Gosu.draw_circle(@position.x, @position.y - @depth * 0.25, 2, 3, TAC::Palette::TIMECRAFTERS_TERTIARY)
end
end
end

View File

@@ -7,7 +7,7 @@ module TAC
@field_container = field_container
@robot = Simulator::Robot.new(width: 18, depth: 18)
@field = Field.new(robot: @robot, season: :skystone, container: @field_container)
@field = Field.new(simulation: self, robot: @robot, season: :skystone, container: @field_container)
@queue = []
@unit = :ticks
@@ -19,6 +19,11 @@ module TAC
def __start
self.instance_eval(@source_code)
@queue.first.start unless @queue.empty?
end
def __queue
@queue
end
def __draw
@@ -32,7 +37,10 @@ module TAC
if state = @queue.first
state.update((Gosu.milliseconds - @last_milliseconds) / 1000.0)
@queue.delete(state) if state.complete?
if state.complete?
@queue.delete(state)
@queue.first.start unless @queue.empty?
end
end
@last_milliseconds = Gosu.milliseconds
@@ -61,23 +69,29 @@ module TAC
@ticks_per_revolution = Integer(ticks)
end
def forward(distance, power)
def forward(distance, power = 0.5)
@queue << Move.new(simulation: self, distance: distance, power: power)
end
def backward(distance, power)
def backward(distance, power = 1.0)
@queue << Move.new(simulation: self, distance: -distance, power: power)
end
def turn(relative_angle, power)
def turn(relative_angle, power = 1.0)
@queue << Turn.new(simulation: self, relative_angle: relative_angle, power: power)
end
def __speed
@ticks_per_revolution * @gear_ratio
@ticks_per_revolution / @gear_ratio
end
class State
def start
end
def draw
end
def update(dt)
end
@@ -92,26 +106,35 @@ module TAC
@robot = @simulation.robot
@distance = distance
@power = power
end
def start
@starting_position = @robot.position.clone
@goal = @starting_position.clone
@goal.x += @distance * Math.cos(@robot.angle.gosu_to_radians)
@goal.y += @distance * Math.sin(@robot.angle.gosu_to_radians)
@goal.x += Math.cos(@robot.angle.gosu_to_radians) * @distance
@goal.y += Math.sin(@robot.angle.gosu_to_radians) * @distance
@complete = false
@allowable_error = 3.0
end
def draw
Gosu.draw_line(@robot.position.x, @robot.position.y, Gosu::Color::GREEN, @goal.x, @goal.y, Gosu::Color::GREEN)
Gosu.draw_rect(@goal.x, @goal.y, 16, 16, Gosu::Color::RED)
end
def update(dt)
target = @starting_position + @goal
speed = (@distance > 0 ? @power * dt : -@power * dt) * @simulation.__speed
# TODO: Fix not stopping if travelling at an angle
if @robot.position.distance(target) <= @allowable_error
if @robot.position.distance(@goal) <= @allowable_error
@complete = true
@robot.position = @goal
else
@robot.position.x += Math.cos(@robot.angle.gosu_to_radians) * speed
@robot.position.y += Math.sin(@robot.angle.gosu_to_radians) * speed
if speed > 0
@robot.position -= (@robot.position - @goal).normalized * speed
else
@robot.position += (@robot.position - @goal).normalized * speed
end
end
end
end
@@ -122,7 +145,9 @@ module TAC
@robot = @simulation.robot
@relative_angle = relative_angle
@power = power
end
def start
@starting_angle = @robot.angle
@last_angle = @starting_angle
@complete = false
@@ -134,8 +159,10 @@ module TAC
if @robot.angle.between?(target_angle - @allowable_error, target_angle + @allowable_error)
@complete = true
@robot.angle = target_angle
elsif (@robot.angle - @last_angle).between?(target_angle - @allowable_error, target_angle + @allowable_error)
@complete = true
@robot.angle = target_angle
elsif target_angle > @starting_angle
@robot.angle += @power * dt * @simulation.__speed
elsif target_angle < @starting_angle

View File

@@ -13,28 +13,28 @@ module TAC
end
flow width: 1.0, height: 0.9 do
background Gosu::Color::GRAY
@field_container = stack width: 0.4, height: 1.0 do
background Gosu::Color::WHITE
background Gosu::Color.new(0xff_333333)..Gosu::Color::BLACK
end
stack width: 0.6, height: 1.0 do
background Gosu::Color::GREEN
stack width: 1.0, height: 0.95 do
# background Gosu::Color::YELLOW
@source_code = edit_line "", width: 1.0, height: 1.0, text_size: 18
end
background Gosu::Color.new(0x88_ff8800)
flow width: 1.0, height: 0.05 do
background Gosu::Color::BLUE
button "Run", text_size: 18, width: 0.49 do
button get_image("#{TAC::ROOT_PATH}/media/icons/right.png"), image_width: 18, width: 0.49 do
@simulation = TAC::Simulator::Simulation.new(source_code: @source_code.value, field_container: @field_container)
@simulation.__start
end
button "Save", text_size: 18, width: 0.49
button get_image("#{TAC::ROOT_PATH}/media/icons/stop.png"), image_width: 18, width: 0.49 do
@simulation.__queue.clear if @simulation
end
button get_image("#{TAC::ROOT_PATH}/media/icons/save.png"), image_width: 18, width: 0.49
@simulation_status = label "", text_size:18
end
stack width: 1.0, height: 0.95 do
@source_code = edit_line "backward 100; turn 90; forward 100; turn -90; forward 100; turn -90; forward 100", width: 1.0, height: 1.0, text_size: 18
end
end
end