funfriend/src/funfriend_context.cr

79 lines
2.0 KiB
Crystal

class Funfriend::FunfriendContext < Funfriend::WindowContext
getter renderer : FunfriendRenderer
property chatter_timer : Float64
WINDOW_SIZE = {width: 82, height: 82}
CHATTER_TIMER = 5.0
def initialize
super(
title: "??_FUNFRIEND_??",
width: WINDOW_SIZE[:width], height: WINDOW_SIZE[:height],
transparent: true
)
@chatter_timer = 1.0
# just for initialization, let OpenGL know this is the current context
window.make_context_current
@renderer = FunfriendRenderer.new
window.on_mouse_button do |event|
if event.action.press? && event.mouse_button.two?
renderer.catmoding = !renderer.catmoding
end
end
window.on_key do |event|
if event.action.press? && event.key.escape?
event.window.should_close
end
end
# pick a random pos
monitor = Monitor.primary
window.position = {
x: monitor.position[:x] + (monitor.video_mode.size[:width] * rand((0.0..1.0))).to_i,
y: monitor.position[:y] + (monitor.video_mode.size[:height] * rand((0.0..1.0))).to_i
}
end
def render(dt : Float64)
# let OpenGL draw to it
window.make_context_current
# draw funfriend
renderer.render(dt, WINDOW_SIZE[:width], WINDOW_SIZE[:height])
end
def update(dt : Float64)
@chatter_timer = @chatter_timer - dt
if @chatter_timer <= 0.0
@chatter_timer = @chatter_timer + CHATTER_TIMER
Funfriend.contexts.each do |context|
if context.is_a?(ChatterContext)
window = context.window
window.position = {
x: window.position[:x],
y: window.position[:y] - context.window_size[:height] - 10
}
end
end
Funfriend.add_context(ChatterContext.new(renderer.catmoding ? "HEWWO INTEWWOPEW" : "HELLO INTERLOPER", {
x: window.position[:x] + WINDOW_SIZE[:width] // 2,
y: window.position[:y] + WINDOW_SIZE[:height] // 2 - 70
}))
end
render(dt)
window.swap_buffers
end
def clean_up
renderer.clean_up
end
end