2021-11-29 08:26:51 +01:00
|
|
|
require "../src/game"
|
|
|
|
require "../src/controller"
|
2022-01-03 01:39:17 +01:00
|
|
|
require "../src/entity"
|
2021-11-30 04:04:31 +01:00
|
|
|
require "../src/pixel"
|
2022-01-03 01:39:17 +01:00
|
|
|
require "../src/shape"
|
|
|
|
require "../src/point"
|
|
|
|
|
|
|
|
class Triangle < PF::Entity
|
|
|
|
property frame : Array(PF::Point(Float64))
|
2021-11-29 08:26:51 +01:00
|
|
|
|
2022-01-03 01:39:17 +01:00
|
|
|
def initialize(*args, **kwargs)
|
|
|
|
@frame = [] of PF::Point(Float64)
|
|
|
|
end
|
2021-11-29 08:26:51 +01:00
|
|
|
|
|
|
|
def update(dt)
|
|
|
|
end
|
|
|
|
|
|
|
|
def draw(engine)
|
2022-01-03 01:39:17 +01:00
|
|
|
_frame = PF::Shape.rotate(@frame, @rotation)
|
|
|
|
_frame = PF::Shape.translate(_frame, @position)
|
|
|
|
engine.fill_triangle(_frame.map(&.to_i32), PF::Pixel.yellow)
|
2021-11-29 08:26:51 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class TriangleThing < PF::Game
|
|
|
|
@tri : Triangle
|
2022-01-03 01:39:17 +01:00
|
|
|
@paused = false
|
2021-12-23 04:18:14 +01:00
|
|
|
@controller : PF::Controller(LibSDL::Scancode)
|
2021-11-29 08:26:51 +01:00
|
|
|
|
|
|
|
def initialize(@width, @height, @scale)
|
|
|
|
super(@width, @height, @scale)
|
|
|
|
|
2022-01-03 01:39:17 +01:00
|
|
|
@tri = Triangle.new
|
|
|
|
@tri.position = PF::Point.new(@width / 2, @height / 2)
|
|
|
|
@tri.frame = PF::Shape.circle(3, size = @width / 3)
|
2021-11-29 08:26:51 +01:00
|
|
|
|
2021-12-23 04:18:14 +01:00
|
|
|
@controller = PF::Controller(LibSDL::Scancode).new({
|
|
|
|
LibSDL::Scancode::RIGHT => "Rotate Right",
|
|
|
|
LibSDL::Scancode::LEFT => "Rotate Left",
|
|
|
|
LibSDL::Scancode::SPACE => "Pause",
|
2021-11-29 08:26:51 +01:00
|
|
|
})
|
|
|
|
end
|
|
|
|
|
2022-01-03 01:39:17 +01:00
|
|
|
def update(dt, event)
|
|
|
|
case event
|
|
|
|
when SDL::Event::Keyboard
|
|
|
|
@controller.press(event.scancode) if event.keydown?
|
|
|
|
@controller.release(event.scancode) if event.keyup?
|
2021-12-06 04:02:11 +01:00
|
|
|
end
|
|
|
|
|
2022-01-03 01:39:17 +01:00
|
|
|
@paused = !@paused if @controller.pressed?("Pause")
|
2021-12-06 04:02:11 +01:00
|
|
|
|
2022-01-03 01:39:17 +01:00
|
|
|
@tri.rotation = @tri.rotation + 0.5 * dt if @controller.action?("Rotate Right")
|
|
|
|
@tri.rotation = @tri.rotation - 0.5 * dt if @controller.action?("Rotate Left")
|
2021-12-06 04:02:11 +01:00
|
|
|
|
2021-11-29 08:26:51 +01:00
|
|
|
unless @paused
|
2022-01-03 01:39:17 +01:00
|
|
|
@tri.rotation = @tri.rotation + 1.0 * dt
|
2021-11-29 08:26:51 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
@tri.update(dt)
|
|
|
|
end
|
|
|
|
|
|
|
|
def draw
|
2021-12-01 02:02:59 +01:00
|
|
|
clear(0, 0, 100)
|
2021-11-29 08:26:51 +01:00
|
|
|
@tri.draw(self)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-12-06 04:02:11 +01:00
|
|
|
engine = TriangleThing.new(50, 50, 10)
|
2021-11-29 08:26:51 +01:00
|
|
|
engine.run!
|