Wired first pass of enemies, testing game controller.
This commit is contained in:
+46
-9
@@ -3,6 +3,7 @@ package game
|
||||
import "core:fmt"
|
||||
import rl "vendor:raylib"
|
||||
|
||||
|
||||
Player_State :: enum { IDLE, WALK, ATTACK }
|
||||
|
||||
Player :: struct {
|
||||
@@ -24,7 +25,7 @@ Player :: struct {
|
||||
|
||||
player_init :: proc(assets: ^Player_Assets) -> Player {
|
||||
player := Player{}
|
||||
player.position = {39, 39}
|
||||
player.position = {260, 260}
|
||||
player.speed = 180
|
||||
player.accel = 720
|
||||
player.friction = 640
|
||||
@@ -53,15 +54,45 @@ get_player_collider :: proc(player: ^Player, pos: rl.Vector2) -> rl.Rectangle {
|
||||
}
|
||||
}
|
||||
|
||||
update_player :: proc(player: ^Player, assets: ^Player_Assets, camera: rl.Camera2D, game_map: Game_Map) {
|
||||
update_player :: proc(
|
||||
player: ^Player, assets: ^Player_Assets, camera: rl.Camera2D,
|
||||
game_map: Game_Map, enemies: ^[MAX_ENEMIES]Enemy, active_enemies: int
|
||||
) {
|
||||
moving := false
|
||||
|
||||
dt := rl.GetFrameTime()
|
||||
input_dir := rl.Vector2{0,0}
|
||||
|
||||
if rl.IsKeyDown(.W) { moving = true; input_dir.y -=1; }
|
||||
if rl.IsKeyDown(.S) { moving = true; input_dir.y +=1; }
|
||||
if rl.IsKeyDown(.A) { moving = true; input_dir.x -=1; player.flipped = true }
|
||||
if rl.IsKeyDown(.D) { moving = true; input_dir.x +=1; player.flipped = false }
|
||||
if rl.IsKeyDown(.W) || rl.IsKeyDown(.UP) { moving = true; input_dir.y -= 1 }
|
||||
if rl.IsKeyDown(.S) || rl.IsKeyDown(.DOWN) { moving = true; input_dir.y += 1 }
|
||||
if rl.IsKeyDown(.A) || rl.IsKeyDown(.LEFT) { moving = true; input_dir.x -= 1; player.flipped = true }
|
||||
if rl.IsKeyDown(.D) || rl.IsKeyDown(.RIGHT) { moving = true; input_dir.x += 1; player.flipped = false }
|
||||
|
||||
GAMEPAD :: 1
|
||||
if rl.IsGamepadAvailable(GAMEPAD) {
|
||||
rl.HideCursor()
|
||||
|
||||
// D-pad (buttons) — same digital feel as WASD
|
||||
if rl.IsGamepadButtonDown(GAMEPAD, .LEFT_FACE_UP) { moving = true; input_dir.y -= 1 }
|
||||
if rl.IsGamepadButtonDown(GAMEPAD, .LEFT_FACE_DOWN) { moving = true; input_dir.y += 1 }
|
||||
if rl.IsGamepadButtonDown(GAMEPAD, .LEFT_FACE_LEFT) { moving = true; input_dir.x -= 1; player.flipped = true }
|
||||
if rl.IsGamepadButtonDown(GAMEPAD, .LEFT_FACE_RIGHT) { moving = true; input_dir.x += 1; player.flipped = false }
|
||||
|
||||
// Left analog stick, with a deadzone
|
||||
stick_x := rl.GetGamepadAxisMovement(GAMEPAD, .LEFT_X)
|
||||
stick_y := rl.GetGamepadAxisMovement(GAMEPAD, .LEFT_Y)
|
||||
DEADZONE :: 0.25
|
||||
|
||||
if abs(stick_x) > DEADZONE {
|
||||
moving = true
|
||||
input_dir.x += stick_x
|
||||
player.flipped = stick_x < 0
|
||||
}
|
||||
if abs(stick_y) > DEADZONE {
|
||||
moving = true
|
||||
input_dir.y += stick_y
|
||||
}
|
||||
}
|
||||
|
||||
has_input := input_dir.x != 0 || input_dir.y != 0
|
||||
if has_input {
|
||||
@@ -79,20 +110,26 @@ update_player :: proc(player: ^Player, assets: ^Player_Assets, camera: rl.Camera
|
||||
// Check for collisions
|
||||
new_position := player.velocity * dt
|
||||
new_x_pos := rl.Vector2{player.position.x + new_position.x, player.position.y}
|
||||
if !check_map_collision(get_player_collider(player, new_x_pos), game_map) {
|
||||
if
|
||||
!check_map_collision(get_player_collider(player, new_x_pos), game_map) &&
|
||||
!check_enemy_collision(get_player_collider(player, new_x_pos), active_enemies, enemies)
|
||||
{
|
||||
player.position.x = new_x_pos.x
|
||||
} else {
|
||||
player.velocity.x = 0
|
||||
}
|
||||
|
||||
new_y_pos := rl.Vector2{player.position.x, player.position.y + new_position.y}
|
||||
if !check_map_collision(get_player_collider(player, new_y_pos), game_map) {
|
||||
if
|
||||
!check_map_collision(get_player_collider(player, new_y_pos), game_map) &&
|
||||
!check_enemy_collision(get_player_collider(player, new_y_pos), active_enemies, enemies)
|
||||
{
|
||||
player.position.y = new_y_pos.y
|
||||
} else {
|
||||
player.velocity.y = 0
|
||||
}
|
||||
|
||||
// TESTING
|
||||
// @TESTING
|
||||
if rl.IsMouseButtonPressed(.LEFT) {
|
||||
mouse_pos := rl.GetMousePosition()
|
||||
screen_space := window_to_screen(mouse_pos)
|
||||
|
||||
Reference in New Issue
Block a user