Fixed null-pointer tweens, mouse controls in menu
This commit is contained in:
		@ -70,7 +70,7 @@ game_update :: proc(state: ^GameState, delta: f32) {
 | 
			
		||||
 | 
			
		||||
			direction := Vec2{pad.velocity.x / PAD_MAX_SPEED, -1}
 | 
			
		||||
			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 }
 | 
			
		||||
 | 
			
		||||
		screen_pos := rl.GetWorldToScreen2D(ball.position, camera) 
 | 
			
		||||
		fmt.println(screen_pos)
 | 
			
		||||
		if !rl.CheckCollisionCircleRec(ball.position, ball.radius, rl.Rectangle{0, 0, WINDOWF.x, WINDOWF.y}
 | 
			
		||||
		) {
 | 
			
		||||
		if screen_pos.y > WINDOWF.y{
 | 
			
		||||
			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.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)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -32,9 +32,10 @@ menu_init :: proc(prev: ^GameState = nil) -> ^GameState {
 | 
			
		||||
		state = state,
 | 
			
		||||
		position = {300, 300},
 | 
			
		||||
		line_size = 60,
 | 
			
		||||
		font_size = 42,
 | 
			
		||||
		font_size = 48,
 | 
			
		||||
		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.draw = menu_draw 
 | 
			
		||||
 | 
			
		||||
@ -2,6 +2,7 @@ package main
 | 
			
		||||
 | 
			
		||||
import rl "vendor:raylib"
 | 
			
		||||
import "core:math/ease"
 | 
			
		||||
import "core:fmt"
 | 
			
		||||
 | 
			
		||||
MenuList :: struct($T: typeid) {
 | 
			
		||||
	state: ^GameState,
 | 
			
		||||
@ -13,13 +14,39 @@ MenuList :: struct($T: typeid) {
 | 
			
		||||
	tween: ^Tween,
 | 
			
		||||
	elements: ^[T]cstring,
 | 
			
		||||
	menu_pressed: proc(state: ^GameState, element: T),
 | 
			
		||||
	background: rl.Color,
 | 
			
		||||
	mouse_pos: Vec2,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
menu_list_update :: proc(list: ^MenuList($T)) {
 | 
			
		||||
 | 
			
		||||
	activate_element := false
 | 
			
		||||
	prev_element := cast(i8)list.active_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) {
 | 
			
		||||
		cur_element += 1
 | 
			
		||||
@ -37,8 +64,8 @@ menu_list_update :: proc(list: ^MenuList($T)) {
 | 
			
		||||
		list.tween = tween_to(
 | 
			
		||||
			&list.active_marker.y,
 | 
			
		||||
			f32(list.active_element) * list.line_size,
 | 
			
		||||
			0.5,
 | 
			
		||||
			ease.Ease.Back_Out,
 | 
			
		||||
			0.25,
 | 
			
		||||
			ease.Ease.Quadratic_Out,
 | 
			
		||||
		)
 | 
			
		||||
	}
 | 
			
		||||
	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)) {
 | 
			
		||||
	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)
 | 
			
		||||
	for el, i in list.elements {
 | 
			
		||||
		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
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -30,7 +30,8 @@ pause_init :: proc(prev: ^GameState = nil) -> ^GameState {
 | 
			
		||||
		line_size = 60,
 | 
			
		||||
		font_size = 42,
 | 
			
		||||
		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.draw = pause_draw
 | 
			
		||||
 | 
			
		||||
@ -65,7 +65,11 @@ tweens_process :: proc(delta: f32) {
 | 
			
		||||
		p := clamp(tween.time / tween.duration, 0, 1)
 | 
			
		||||
 | 
			
		||||
		val := ease.ease(tween.ease_type, p)
 | 
			
		||||
		tween.ptr^ = math.lerp(tween.from, tween.to, val)
 | 
			
		||||
		if tween.ptr != nil {
 | 
			
		||||
			tween.ptr^ = math.lerp(tween.from, tween.to, val)
 | 
			
		||||
		} else {
 | 
			
		||||
			tween.active = false
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		if tween.time >= tween.duration {
 | 
			
		||||
			tween.active = false
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user