chatter, sfx & draggability (janky!)

This commit is contained in:
Jill 2023-05-31 13:14:38 +03:00
parent 65b0a40006
commit d1b5b4d301
Signed by: oat
GPG Key ID: 33489AA58A955108
14 changed files with 125 additions and 20 deletions

BIN
assets/sfx/talk1.ogg Normal file

Binary file not shown.

BIN
assets/sfx/talk2.ogg Normal file

Binary file not shown.

BIN
assets/sfx/talk3.ogg Normal file

Binary file not shown.

BIN
assets/sfx/talk4.ogg Normal file

Binary file not shown.

BIN
assets/sfx/talk5.ogg Normal file

Binary file not shown.

BIN
assets/sfx/talk6.ogg Normal file

Binary file not shown.

BIN
assets/sfx/talk7.ogg Normal file

Binary file not shown.

BIN
assets/sfx/talk8.ogg Normal file

Binary file not shown.

View File

@ -24,3 +24,7 @@ shards:
git: https://github.com/calebuharrison/libstbimage.git
version: 0.1.0+git.commit.32bc23af165dbe004a7468426f09e6701463bc72
sdl:
git: https://github.com/ysbaddaden/sdl.cr.git
version: 0.1.0+git.commit.a86f867f8c7afbceb8d166ec09185ee14bf0c6d5

View File

@ -16,6 +16,8 @@ dependencies:
branch: master
crystimage:
github: calebuharrison/CrystImage
sdl:
github: ysbaddaden/sdl.cr
targets:
funfriend:

View File

@ -1,14 +1,16 @@
class Funfriend::ChatterContext < Funfriend::WindowContext
getter renderer : TextRenderer
getter parent : WindowContext?
property parent_relative_pos : NamedTuple(x: Int32, y: Int32)
getter window_size : NamedTuple(width: Int32, height: Int32)
property timer : Float64
WINDOW_SIZE = {width: 256, height: 32}
DEFAULT_DURATION = 10.0
DEFAULT_DURATION = 6.0
PADDING = 10
def initialize(text : String, position : NamedTuple(x: Int32, y: Int32), duration : Float64 = DEFAULT_DURATION)
def initialize(text : String, position : NamedTuple(x: Int32, y: Int32), duration : Float64 = DEFAULT_DURATION, parent : WindowContext? = nil)
sheet = FontMan.parse_bm(File.read "assets/fonts/SpaceMono.fnt")
position_data = FontMan.position_text(text, sheet)
@ -34,6 +36,26 @@ class Funfriend::ChatterContext < Funfriend::WindowContext
x: position[:x] - window_size[:width]//2,
y: position[:y] - window_size[:height]//2,
}
@parent = parent
if parent
@parent_relative_pos = {
x: position[:x] - (parent.window.position[:x] + parent.window.size[:width] // 2),
y: position[:y] - (parent.window.position[:y] + parent.window.size[:height] // 2),
}
else
@parent_relative_pos = {x: 0, y: 0}
end
end
def update_position
if parent
p = parent.not_nil!
window.position = {
x: p.window.position[:x] + p.window.size[:width] // 2 + parent_relative_pos[:x] - window_size[:width] // 2,
y: p.window.position[:y] + p.window.size[:height] // 2 + parent_relative_pos[:y] - window_size[:height] // 2,
}
end
end
def render(dt : Float64)
@ -52,11 +74,21 @@ class Funfriend::ChatterContext < Funfriend::WindowContext
window.should_close
end
update_position
render(dt)
window.swap_buffers
end
def bump
@parent_relative_pos = {
x: parent_relative_pos[:x],
y: parent_relative_pos[:y] - window_size[:height] - 10
}
update_position
end
def clean_up
renderer.clean_up
end

View File

@ -7,6 +7,7 @@ require "./log.cr"
require "./gl.cr"
require "./textureman.cr"
require "./fontman.cr"
require "./soundman.cr"
require "./window_context.cr"
require "./funfriend_context.cr"
require "./funfriend_renderer.cr"
@ -24,8 +25,6 @@ module Funfriend
@@contexts = [] of WindowContext
@@main_context : WindowContext?
@@clean_up_queue = [] of WindowContext
def self.should_close?
@@main_context && @@main_context.not_nil!.window.should_close?
end
@ -33,6 +32,7 @@ module Funfriend
def self.init_contexts
add_context(FunfriendContext.new)
@@main_context = @@contexts[0]
at_exit { contexts.each &.destroy }
end
def self.add_context(context : WindowContext)
@ -46,6 +46,8 @@ module Funfriend
def self.run
Logging.init
SoundMan.init
CrystGLFW.run do
init_contexts
@ -57,7 +59,7 @@ module Funfriend
@@contexts = @@contexts.select do |context|
if context.window.should_close?
context.close
@@clean_up_queue << context
at_exit { context.clean_up }
false
else
context.update(dt)
@ -67,9 +69,6 @@ module Funfriend
CrystGLFW.wait_events(1/120)
end
@@contexts.each &.destroy
@@clean_up_queue.each &.clean_up
end
end
end

View File

@ -1,9 +1,21 @@
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 = 5.0
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"]
]
def initialize
super(
@ -13,6 +25,11 @@ class Funfriend::FunfriendContext < Funfriend::WindowContext
)
@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
@ -22,6 +39,19 @@ class Funfriend::FunfriendContext < Funfriend::WindowContext
if event.action.press? && event.mouse_button.two?
renderer.catmoding = !renderer.catmoding
end
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
end
window.on_key do |event|
@ -46,27 +76,40 @@ class Funfriend::FunfriendContext < Funfriend::WindowContext
renderer.render(dt, WINDOW_SIZE[:width], WINDOW_SIZE[:height])
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,
}
end
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
}
if @chatter_array.try &.[(@chatter_index)]?
Funfriend.contexts.each do |context|
if context.is_a?(ChatterContext) && context.parent == self
context.bump
end
end
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")
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
}))
@chatter_index = @chatter_index + 1
end
update_pos(dt)
render(dt)
window.swap_buffers

25
src/soundman.cr Normal file
View File

@ -0,0 +1,25 @@
require "sdl"
require "sdl/mix"
module Funfriend::SoundMan
extend self
@@sounds = Hash(String, SDL::Mix::Sample).new
def play_sound(filepath : String)
if !@@sounds.has_key?(filepath)
sound = SDL::Mix::Sample.new(filepath)
@@sounds[filepath] = sound
end
SDL::Mix::Channel.play(@@sounds[filepath])
end
def init
SDL.init(SDL::Init::AUDIO); at_exit { SDL.quit }
SDL::Mix.init(SDL::Mix::Init::OGG); at_exit { SDL::Mix.quit }
SDL::Mix.open
SDL::Mix::Channel.volume = SDL::Mix::MAX_VOLUME // 5
end
end