fnm/game.odin

134 lines
3.1 KiB
Odin

package game
import rl "vendor:raylib"
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
Color :: rl.Color
SCREEN_WIDTH : i32 = 1000
SCREEN_HEIGHT : i32 = 800
BW := f32(SCREEN_WIDTH)
BH := f32(SCREEN_HEIGHT)
BULLET_SPEED : f32 = 7.5
BULLET_RADIUS : f32 = 5
ANGULAR_SPEED : f32 = 0.045
THRUST_SPEED : f32 = 2.3
Player :: struct {
pos: Vec2,
points: [4]Vec2,
angle: f32,
vel: Vec2
}
Bullet :: struct {
pos: Vec2,
vel: Vec2,
}
GameState :: struct {
dt: f32,
keymap: map[string]Key,
player: Player,
bullets: [dynamic]Bullet,
}
init_state :: proc() -> ^GameState {
keymap := make(map[string]Key)
keymap["Left"] = .A
keymap["Right"] = .D
keymap["Up"] = .W
keymap["Down"] = .S
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) {
theta := player.angle + math.PI / 2
player.vel.x += math.cos(theta) * speed
player.vel.y += math.sin(theta) * speed
}
if rl.IsKeyDown(.A) {
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) * 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
append(&s.bullets, Bullet{player.points[0], b_vel})
}
}
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, 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)
for !rl.WindowShouldClose() {
state.dt = rl.GetFrameTime()
player_input(state)
update(state)
rl.BeginDrawing()
rl.ClearBackground(rl.WHITE)
draw(state)
rl.EndDrawing()
}
rl.CloseWindow()
}