First commit with asteroid moving and rotating
This commit is contained in:
commit
11af775673
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
/asteroids-odin
|
86
game.odin
Normal file
86
game.odin
Normal file
@ -0,0 +1,86 @@
|
||||
package asteroids
|
||||
|
||||
import rl "vendor:raylib"
|
||||
import "core:math"
|
||||
import "core:fmt"
|
||||
|
||||
SCREEN_WIDTH : i32 = 1280
|
||||
SCREEN_HEIGHT : i32 = 720
|
||||
|
||||
SCALE: f32 = 0.5
|
||||
SHIP_W: f32 = 70 * SCALE
|
||||
SHIP_H: f32 = 100 * SCALE
|
||||
|
||||
THRUST_SPEED : f32 = 0.03
|
||||
ANGULAR_SPEED : f32 = 2.5
|
||||
|
||||
Vec2 :: [2]f32
|
||||
|
||||
GameState :: struct {
|
||||
ship_pos: Vec2,
|
||||
ship_angle: f32,
|
||||
ship_vel: Vec2,
|
||||
dt: f32,
|
||||
}
|
||||
|
||||
rotate_vec2 :: proc(v : Vec2, angle: f32) -> Vec2 {
|
||||
return {
|
||||
v.x * math.cos(angle) - v.y * math.sin(angle),
|
||||
v.x * math.sin(angle) + v.y * math.cos(angle),
|
||||
}
|
||||
}
|
||||
|
||||
get_ship_shape :: proc(s : GameState) -> (Vec2, Vec2, Vec2, Vec2) {
|
||||
v1 := rotate_vec2({ SHIP_H*0.5, 0}, s.ship_angle) + s.ship_pos
|
||||
v2 := rotate_vec2({-SHIP_H*0.5, -SHIP_W*0.5}, s.ship_angle) + s.ship_pos
|
||||
v3 := rotate_vec2({-SHIP_H*0.5*0.5, 0}, s.ship_angle) + s.ship_pos
|
||||
v4 := rotate_vec2({-SHIP_H*0.5, SHIP_W*0.5}, s.ship_angle) + s.ship_pos
|
||||
return v1, v2, v3, v4
|
||||
}
|
||||
|
||||
main :: proc() {
|
||||
rl.SetTraceLogLevel(.ERROR)
|
||||
rl.InitWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "Asteroids")
|
||||
|
||||
s := GameState{{0,0}, -math.PI / 2, {0,0}, rl.GetFrameTime()}
|
||||
s.ship_pos = {600, 350}
|
||||
|
||||
bw := f32(SCREEN_WIDTH)
|
||||
bh := f32(SCREEN_HEIGHT)
|
||||
|
||||
for !rl.WindowShouldClose() {
|
||||
s.dt = rl.GetFrameTime()
|
||||
if rl.IsKeyDown(.D) {
|
||||
s.ship_angle += ANGULAR_SPEED * s.dt
|
||||
}
|
||||
if rl.IsKeyDown(.A) {
|
||||
s.ship_angle -= ANGULAR_SPEED * s.dt
|
||||
}
|
||||
if rl.IsKeyDown(.W) {
|
||||
s.ship_vel.x += math.cos(s.ship_angle) * THRUST_SPEED * s.dt
|
||||
s.ship_vel.y += math.sin(s.ship_angle) * THRUST_SPEED * s.dt
|
||||
}
|
||||
|
||||
s.ship_pos += s.ship_vel * (5000 * s.dt)
|
||||
|
||||
|
||||
if s.ship_pos.x + SHIP_W < 0 { s.ship_pos.x = bw + SHIP_W}
|
||||
if s.ship_pos.x - SHIP_W > bw { s.ship_pos.x = SHIP_W}
|
||||
if s.ship_pos.y + < 0 { s.ship_pos.y = bh}
|
||||
if s.ship_pos.y > bh { s.ship_pos.y = 0}
|
||||
|
||||
rl.BeginDrawing()
|
||||
rl.ClearBackground(rl.BLACK)
|
||||
|
||||
// Draw ship
|
||||
v1, v2, v3, v4 := get_ship_shape(s)
|
||||
rl.DrawLineEx(v1, v2, 1, rl.WHITE)
|
||||
rl.DrawLineEx(v2, v3, 1, rl.WHITE)
|
||||
rl.DrawLineEx(v3, v4, 1, rl.WHITE)
|
||||
rl.DrawLineEx(v4, v1, 1, rl.WHITE)
|
||||
|
||||
rl.EndDrawing()
|
||||
}
|
||||
|
||||
rl.CloseWindow()
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user