Look at mouse position and move with WASD based on look direction

This commit is contained in:
Joseph Ferano 2024-09-07 14:45:48 +07:00
parent 195b54835c
commit 8b9cbda9b6
2 changed files with 35 additions and 10 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/fnm

View File

@ -5,11 +5,12 @@ import "core:math"
import "core:math/rand"
import "core:fmt"
Vec2 :: [2]f32
Rect :: rl.Rectangle
Img :: rl.Image
Tex :: rl.Texture
Key :: rl.KeyboardKey
Vec2 :: [2]f32
Rect :: rl.Rectangle
Img :: rl.Image
Tex :: rl.Texture
Key :: rl.KeyboardKey
Color :: rl.Color
SCREEN_WIDTH : i32 = 1000
SCREEN_HEIGHT : i32 = 800
@ -50,21 +51,33 @@ init_state :: proc() -> ^GameState {
state := GameState {
keymap = keymap,
player = Player {
pos = Vec2{BW / 2, BH / 2},
},
}
cloned := new_clone(state)
return cloned
}
player_input :: proc(using s: ^GameState) {
speed :f32 = 1.5
if rl.IsKeyDown(.D) {
player.angle += ANGULAR_SPEED
theta := player.angle + math.PI / 2
player.vel.x += math.cos(theta) * speed
player.vel.y += math.sin(theta) * speed
}
if rl.IsKeyDown(.A) {
player.angle -= ANGULAR_SPEED
theta := player.angle - math.PI / 2
player.vel.x += math.cos(theta) * speed
player.vel.y += math.sin(theta) * speed
}
if rl.IsKeyDown(.W) {
player.vel.x += math.cos(player.angle)
player.vel.y += math.sin(player.angle)
player.vel.x += math.cos(player.angle) * speed
player.vel.y += math.sin(player.angle) * speed
}
if rl.IsKeyDown(.S) {
player.vel.x += math.cos(-player.angle) * speed
player.vel.y += math.sin(-player.angle) * speed
}
if rl.IsKeyPressed(.SPACE) {
b_vel := Vec2{ math.cos(player.angle) , math.sin(player.angle) } * BULLET_SPEED
@ -73,19 +86,30 @@ player_input :: proc(using s: ^GameState) {
}
update :: proc(using s: ^GameState) {
player.vel *= 0.97
player.pos += player.vel * (THRUST_SPEED * s.dt)
m_pos, p_pos := rl.GetMousePosition(), player.pos
player.angle = math.atan2(m_pos.y - p_pos.y, m_pos.x - p_pos.x)
}
draw :: proc(using s: ^GameState) {
rl.DrawCircleV(player.pos, 10, rl.BLACK)
rect := Rect{player.pos.x, player.pos.y, 20, 5}
rect := Rect{player.pos.x, player.pos.y, 15, 5}
rl.DrawRectanglePro(rect, Vec2{0,2.5}, player.angle * rl.RAD2DEG, rl.BLACK)
m_pos := rl.GetMousePosition()
reticle_size :f32 = 50
reticle_rect := Rect{m_pos.x - reticle_size / 2, m_pos.y- reticle_size / 2, reticle_size, reticle_size}
color := Color{255, 0, 0, 172}
rl.DrawRectangleRoundedLines(reticle_rect, 0.2, 5, 5, color)
rl.DrawCircleV(m_pos, 3, color)
}
main :: proc() {
rl.SetTraceLogLevel(.ERROR)
rl.InitWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "F&M")
rl.SetTargetFPS(60)
rl.DisableCursor()
state := init_state()
defer free(state)