Look at mouse position and move with WASD based on look direction
This commit is contained in:
parent
195b54835c
commit
8b9cbda9b6
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
/fnm
|
44
game.odin
44
game.odin
@ -5,11 +5,12 @@ import "core:math"
|
|||||||
import "core:math/rand"
|
import "core:math/rand"
|
||||||
import "core:fmt"
|
import "core:fmt"
|
||||||
|
|
||||||
Vec2 :: [2]f32
|
Vec2 :: [2]f32
|
||||||
Rect :: rl.Rectangle
|
Rect :: rl.Rectangle
|
||||||
Img :: rl.Image
|
Img :: rl.Image
|
||||||
Tex :: rl.Texture
|
Tex :: rl.Texture
|
||||||
Key :: rl.KeyboardKey
|
Key :: rl.KeyboardKey
|
||||||
|
Color :: rl.Color
|
||||||
|
|
||||||
SCREEN_WIDTH : i32 = 1000
|
SCREEN_WIDTH : i32 = 1000
|
||||||
SCREEN_HEIGHT : i32 = 800
|
SCREEN_HEIGHT : i32 = 800
|
||||||
@ -50,21 +51,33 @@ init_state :: proc() -> ^GameState {
|
|||||||
|
|
||||||
state := GameState {
|
state := GameState {
|
||||||
keymap = keymap,
|
keymap = keymap,
|
||||||
|
player = Player {
|
||||||
|
pos = Vec2{BW / 2, BH / 2},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
cloned := new_clone(state)
|
cloned := new_clone(state)
|
||||||
return cloned
|
return cloned
|
||||||
}
|
}
|
||||||
|
|
||||||
player_input :: proc(using s: ^GameState) {
|
player_input :: proc(using s: ^GameState) {
|
||||||
|
speed :f32 = 1.5
|
||||||
if rl.IsKeyDown(.D) {
|
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) {
|
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) {
|
if rl.IsKeyDown(.W) {
|
||||||
player.vel.x += math.cos(player.angle)
|
player.vel.x += math.cos(player.angle) * speed
|
||||||
player.vel.y += math.sin(player.angle)
|
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) {
|
if rl.IsKeyPressed(.SPACE) {
|
||||||
b_vel := Vec2{ math.cos(player.angle) , math.sin(player.angle) } * BULLET_SPEED
|
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) {
|
update :: proc(using s: ^GameState) {
|
||||||
|
player.vel *= 0.97
|
||||||
player.pos += player.vel * (THRUST_SPEED * s.dt)
|
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) {
|
draw :: proc(using s: ^GameState) {
|
||||||
rl.DrawCircleV(player.pos, 10, rl.BLACK)
|
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)
|
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() {
|
main :: proc() {
|
||||||
rl.SetTraceLogLevel(.ERROR)
|
rl.SetTraceLogLevel(.ERROR)
|
||||||
rl.InitWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "F&M")
|
rl.InitWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "F&M")
|
||||||
rl.SetTargetFPS(60)
|
rl.SetTargetFPS(60)
|
||||||
|
rl.DisableCursor()
|
||||||
|
|
||||||
state := init_state()
|
state := init_state()
|
||||||
defer free(state)
|
defer free(state)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user