funfriend wanders around

This commit is contained in:
Jill 2023-05-31 13:38:50 +03:00
parent d1b5b4d301
commit 560c635694
Signed by: oat
GPG Key ID: 33489AA58A955108
3 changed files with 76 additions and 15 deletions

15
src/ease.cr Normal file
View File

@ -0,0 +1,15 @@
module Funfriend::Ease
extend self
include Math
def inSine(x)
1 - cos(x * (PI * 0.5))
end
def outSine(x)
sin(x * (PI * 0.5))
end
def inOutSine(x)
0.5 - 0.5 * cos(x * PI)
end
end

View File

@ -4,6 +4,7 @@ require "crystgl"
require "crystimage"
require "./log.cr"
require "./ease.cr"
require "./gl.cr"
require "./textureman.cr"
require "./fontman.cr"

View File

@ -1,12 +1,4 @@
class Funfriend::FunfriendContext < Funfriend::WindowContext
getter renderer : FunfriendRenderer
property chatter_timer : Float64
property chatter_index : Int32
property chatter_array : Array(String)?
property held : Bool
property held_at : NamedTuple(x: Int32, y: Int32)
WINDOW_SIZE = {width: 82, height: 82}
CHATTER_TIMER = 3.0
@ -17,6 +9,26 @@ class Funfriend::FunfriendContext < Funfriend::WindowContext
["INTERLOPER!", "WELCOME", "BUT ALSO PLEASE DO NOT BOTHER ME", "VERY BUSY"]
]
getter renderer : FunfriendRenderer
property chatter_timer : Float64 = 1.0
property chatter_index : Int32 = 0
property chatter_array : Array(String)? = CHATTER_ARRAY.sample
property held : Bool = false
property held_at : NamedTuple(x: Int32, y: Int32) = {x: 0, y: 0}
STAY_STILL_AFTER_HELD = 1.0
property held_timer : Float64 = 0.0
property static_pos : NamedTuple(x: Int32, y: Int32)
property easing_from : NamedTuple(x: Int32, y: Int32) = {x: 0, y: 0}
property easing_to : NamedTuple(x: Int32, y: Int32) = {x: 0, y: 0}
property easing_dur : Float64 = 0.0
property easing_t : Float64 = 0.0
WANDER_TIMER = 4.0
property wander_timer : Float64 = WANDER_TIMER
def initialize
super(
title: "??_FUNFRIEND_??",
@ -24,13 +36,6 @@ class Funfriend::FunfriendContext < Funfriend::WindowContext
transparent: true
)
@chatter_timer = 1.0
@chatter_index = 0
@chatter_array = CHATTER_ARRAY.sample
@held = false
@held_at = {x: 0, y: 0}
# just for initialization, let OpenGL know this is the current context
window.make_context_current
@renderer = FunfriendRenderer.new
@ -41,11 +46,13 @@ class Funfriend::FunfriendContext < Funfriend::WindowContext
end
if event.mouse_button.one?
if event.action.press?
@easing_dur = 0.0
@held = true
@held_at = {
x: window.cursor.position[:x].to_i,
y: window.cursor.position[:y].to_i,
}
@held_timer = STAY_STILL_AFTER_HELD
window.cursor Window::Cursor::Shape::Hand
elsif event.action.release?
@held = false
@ -66,6 +73,7 @@ class Funfriend::FunfriendContext < Funfriend::WindowContext
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
}
@static_pos = window.position
end
def render(dt : Float64)
@ -76,12 +84,49 @@ class Funfriend::FunfriendContext < Funfriend::WindowContext
renderer.render(dt, WINDOW_SIZE[:width], WINDOW_SIZE[:height])
end
def goto(pos : NamedTuple(x: Int32, y: Int32), dur : Float64)
@easing_t = 0.0
@easing_dur = dur
@easing_from = window.position
@easing_to = pos
LOG.info { "going from #{easing_from} to #{easing_to}" }
end
def update_wander(dt : Float64)
if @easing_dur != 0.0 && @easing_t <= @easing_dur
@easing_t = @easing_t + dt
a = Ease.inOutSine(@easing_t / @easing_dur)
window.position = {
x: (@easing_from[:x] * (1.0 - a) + @easing_to[:x] * a).to_i,
y: (@easing_from[:y] * (1.0 - a) + @easing_to[:y] * a).to_i,
}
@wander_timer = WANDER_TIMER
else
@wander_timer = @wander_timer - dt
if @wander_timer <= 0
goto({
x: @static_pos[:x] + (-40 .. 40).sample,
y: @static_pos[:y] + (-40 .. 40).sample,
}, 4.0)
end
end
end
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,
}
@static_pos = window.position
else
@held_timer = @held_timer - dt
if @held_timer <= 0
update_wander(dt)
end
end
end