funfriend/src/funfriend_context.cr

122 lines
3.2 KiB
Crystal
Raw Normal View History

2023-05-30 15:47:50 +02:00
class Funfriend::FunfriendContext < Funfriend::WindowContext
getter renderer : FunfriendRenderer
property chatter_timer : Float64
2023-05-31 12:14:38 +02:00
property chatter_index : Int32
property chatter_array : Array(String)?
property held : Bool
property held_at : NamedTuple(x: Int32, y: Int32)
2023-05-30 15:47:50 +02:00
WINDOW_SIZE = {width: 82, height: 82}
2023-05-31 12:14:38 +02:00
CHATTER_TIMER = 3.0
CHATTER_ARRAY = [
["HELLO AGAIN"],
["HI INTERLOPER"],
["HELLO!", "IS THE AUTH LAYER STILL DISSOCIATED?", "I MISS THEM"],
["INTERLOPER!", "WELCOME", "BUT ALSO PLEASE DO NOT BOTHER ME", "VERY BUSY"]
]
2023-05-30 15:47:50 +02:00
def initialize
super(
title: "??_FUNFRIEND_??",
width: WINDOW_SIZE[:width], height: WINDOW_SIZE[:height],
transparent: true
)
@chatter_timer = 1.0
2023-05-31 12:14:38 +02:00
@chatter_index = 0
@chatter_array = CHATTER_ARRAY.sample
@held = false
@held_at = {x: 0, y: 0}
2023-05-30 15:47:50 +02:00
# 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
2023-05-31 12:14:38 +02:00
if event.mouse_button.one?
if event.action.press?
@held = true
@held_at = {
x: window.cursor.position[:x].to_i,
y: window.cursor.position[:y].to_i,
}
window.cursor Window::Cursor::Shape::Hand
elsif event.action.release?
@held = false
window.cursor Window::Cursor::Shape::Arrow
end
end
2023-05-30 15:47:50 +02:00
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
2023-05-31 12:14:38 +02:00
def update_pos(dt : Float64)
if held
window.position = {
x: window.position[:x] - held_at[:x] + window.cursor.position[:x].to_i,
y: window.position[:y] - held_at[:y] + window.cursor.position[:y].to_i,
}
end
end
2023-05-30 15:47:50 +02:00
def update(dt : Float64)
@chatter_timer = @chatter_timer - dt
if @chatter_timer <= 0.0
@chatter_timer = @chatter_timer + CHATTER_TIMER
2023-05-31 12:14:38 +02:00
if @chatter_array.try &.[(@chatter_index)]?
Funfriend.contexts.each do |context|
if context.is_a?(ChatterContext) && context.parent == self
context.bump
end
2023-05-30 15:47:50 +02:00
end
2023-05-31 12:14:38 +02:00
Funfriend.add_context(ChatterContext.new(@chatter_array.not_nil![@chatter_index], {
x: window.position[:x] + WINDOW_SIZE[:width] // 2,
y: window.position[:y] + WINDOW_SIZE[:height] // 2 - 70
}, parent: self))
SoundMan.play_sound("assets/sfx/talk#{(1..8).sample}.ogg")
2023-05-30 15:47:50 +02:00
end
2023-05-31 12:14:38 +02:00
@chatter_index = @chatter_index + 1
2023-05-30 15:47:50 +02:00
end
2023-05-31 12:14:38 +02:00
update_pos(dt)
2023-05-30 15:47:50 +02:00
render(dt)
window.swap_buffers
end
def clean_up
renderer.clean_up
end
end