Fixed null-pointer tweens, mouse controls in menu

This commit is contained in:
Nefrace 2024-09-09 19:05:38 +03:00
parent 4c6a476a49
commit 09b0f28735
5 changed files with 60 additions and 11 deletions

View File

@ -70,7 +70,7 @@ game_update :: proc(state: ^GameState, delta: f32) {
direction := Vec2{pad.velocity.x / PAD_MAX_SPEED, -1} direction := Vec2{pad.velocity.x / PAD_MAX_SPEED, -1}
ball.is_on_pad = false ball.is_on_pad = false
ball.velocity = rl.Vector2Normalize(direction) * 400 ball.velocity = rl.Vector2Normalize(direction) * 300
} }
} }
@ -81,9 +81,7 @@ game_update :: proc(state: ^GameState, delta: f32) {
if is_inside { all_balls_outside = false } if is_inside { all_balls_outside = false }
screen_pos := rl.GetWorldToScreen2D(ball.position, camera) screen_pos := rl.GetWorldToScreen2D(ball.position, camera)
fmt.println(screen_pos) if screen_pos.y > WINDOWF.y{
if !rl.CheckCollisionCircleRec(ball.position, ball.radius, rl.Rectangle{0, 0, WINDOWF.x, WINDOWF.y}
) {
unordered_remove(&balls, i) unordered_remove(&balls, i)
} }
} }
@ -137,6 +135,7 @@ game_draw :: proc(state: ^GameState) {
rl.DrawCircleV(Vec2{20 + 20 * f32(i), 20}, 10, rl.BLUE) rl.DrawCircleV(Vec2{20 + 20 * f32(i), 20}, 10, rl.BLUE)
} }
rl.DrawTextEx(FontUI, rl.TextFormat("%d", len(bricks)), {}, f32(48), 2, rl.BLACK) rl.DrawTextEx(FontUI, rl.TextFormat("%d", len(bricks)), {}, f32(48), 2, rl.BLACK)
rl.DrawTextEx(FontUI, rl.TextFormat("%d", len(balls)), {0, 100}, f32(48), 2, rl.BLACK)
} }

View File

@ -32,9 +32,10 @@ menu_init :: proc(prev: ^GameState = nil) -> ^GameState {
state = state, state = state,
position = {300, 300}, position = {300, 300},
line_size = 60, line_size = 60,
font_size = 42, font_size = 48,
elements = &menu_strings, elements = &menu_strings,
menu_pressed = menu_button_pressed menu_pressed = menu_button_pressed,
background = rl.Color{50, 10, 110, 255}
} }
state.update = menu_update state.update = menu_update
state.draw = menu_draw state.draw = menu_draw

View File

@ -2,6 +2,7 @@ package main
import rl "vendor:raylib" import rl "vendor:raylib"
import "core:math/ease" import "core:math/ease"
import "core:fmt"
MenuList :: struct($T: typeid) { MenuList :: struct($T: typeid) {
state: ^GameState, state: ^GameState,
@ -13,13 +14,39 @@ MenuList :: struct($T: typeid) {
tween: ^Tween, tween: ^Tween,
elements: ^[T]cstring, elements: ^[T]cstring,
menu_pressed: proc(state: ^GameState, element: T), menu_pressed: proc(state: ^GameState, element: T),
background: rl.Color,
mouse_pos: Vec2,
} }
menu_list_update :: proc(list: ^MenuList($T)) { menu_list_update :: proc(list: ^MenuList($T)) {
activate_element := false
prev_element := cast(i8)list.active_element prev_element := cast(i8)list.active_element
cur_element := prev_element cur_element := prev_element
last_mouse_pos := list.mouse_pos
list.mouse_pos = rl.GetMousePosition()
size := menu_list_get_size(list)
if rl.CheckCollisionPointRec(list.mouse_pos, rl.Rectangle{
x = list.position.x,
y = list.position.y,
width = size.x,
height = size.y,
}) {
if last_mouse_pos != list.mouse_pos {
mouse_relative := list.mouse_pos - list.position
cur_element = i8(mouse_relative.y / list.line_size)
fmt.println(cur_element)
}
if rl.IsMouseButtonPressed(rl.MouseButton.LEFT) {
list.menu_pressed(list.state, list.active_element)
}
}
last_mouse_pos = list.mouse_pos
if rl.IsKeyPressed(rl.KeyboardKey.DOWN) { if rl.IsKeyPressed(rl.KeyboardKey.DOWN) {
cur_element += 1 cur_element += 1
@ -37,8 +64,8 @@ menu_list_update :: proc(list: ^MenuList($T)) {
list.tween = tween_to( list.tween = tween_to(
&list.active_marker.y, &list.active_marker.y,
f32(list.active_element) * list.line_size, f32(list.active_element) * list.line_size,
0.5, 0.25,
ease.Ease.Back_Out, ease.Ease.Quadratic_Out,
) )
} }
if rl.IsKeyPressed(rl.KeyboardKey.ENTER) || rl.IsKeyPressed(rl.KeyboardKey.SPACE) { if rl.IsKeyPressed(rl.KeyboardKey.ENTER) || rl.IsKeyPressed(rl.KeyboardKey.SPACE) {
@ -47,9 +74,26 @@ menu_list_update :: proc(list: ^MenuList($T)) {
} }
menu_list_draw :: proc(list: ^MenuList($T)) { menu_list_draw :: proc(list: ^MenuList($T)) {
if list.background[3] != 0 {
size := menu_list_get_size(list)
rl.DrawRectangleV(list.position - {40, 40}, size + {80, 80}, list.background)
}
rl.DrawTextEx(FontUI, ">", list.position + list.active_marker + {-30, 0}, 48, 2, rl.WHITE) rl.DrawTextEx(FontUI, ">", list.position + list.active_marker + {-30, 0}, 48, 2, rl.WHITE)
for el, i in list.elements { for el, i in list.elements {
pos := list.position + {0, f32(i) * list.line_size} pos := list.position + {0, f32(i) * list.line_size}
rl.DrawTextEx(FontUI, el, pos, 48, 2, rl.WHITE) rl.DrawTextEx(FontUI, el, pos, list.font_size, 2, rl.WHITE)
} }
} }
menu_list_get_size :: proc(list: ^MenuList($T)) -> Vec2 {
size := Vec2{}
for el, i in list.elements {
line_size := rl.MeasureTextEx(FontUI, el, list.font_size, 2)
if line_size.x > size.x {
size.x = line_size.x
}
size.y += list.line_size
}
return size
}

View File

@ -30,7 +30,8 @@ pause_init :: proc(prev: ^GameState = nil) -> ^GameState {
line_size = 60, line_size = 60,
font_size = 42, font_size = 42,
elements = &pause_strings, elements = &pause_strings,
menu_pressed = pause_button_pressed menu_pressed = pause_button_pressed,
background = rl.Color{50, 10, 110, 180},
} }
state.update = pause_update state.update = pause_update
state.draw = pause_draw state.draw = pause_draw

View File

@ -65,7 +65,11 @@ tweens_process :: proc(delta: f32) {
p := clamp(tween.time / tween.duration, 0, 1) p := clamp(tween.time / tween.duration, 0, 1)
val := ease.ease(tween.ease_type, p) val := ease.ease(tween.ease_type, p)
if tween.ptr != nil {
tween.ptr^ = math.lerp(tween.from, tween.to, val) tween.ptr^ = math.lerp(tween.from, tween.to, val)
} else {
tween.active = false
}
if tween.time >= tween.duration { if tween.time >= tween.duration {
tween.active = false tween.active = false