Compare commits
2 commits
8bc4572ae3
...
7eaa303d6f
| Author | SHA1 | Date | |
|---|---|---|---|
| 7eaa303d6f | |||
| 799e5ad9eb |
5 changed files with 98 additions and 75 deletions
17
game.odin
17
game.odin
|
|
@ -54,8 +54,8 @@ setup_game :: proc(state: ^GameState) {
|
||||||
using state
|
using state
|
||||||
|
|
||||||
player_pos = glm.vec2 {
|
player_pos = glm.vec2 {
|
||||||
f32(screen_width) / (2 * camera.zoom),
|
f32(screen_width) / 2,
|
||||||
f32(screen_height) / camera.zoom - PLAYER_RECT.x,
|
f32(screen_height) - PLAYER_RECT.x,
|
||||||
}
|
}
|
||||||
player_score = 0
|
player_score = 0
|
||||||
player_health = 3
|
player_health = 3
|
||||||
|
|
@ -101,14 +101,8 @@ update_game :: proc(state: ^GameState) {
|
||||||
// update bullet frame idx
|
// update bullet frame idx
|
||||||
if frame_counter % 10 == 0 {BULLET_FRAME_ANIM = (BULLET_FRAME_ANIM + 1) % len(BULLET_TO)}
|
if frame_counter % 10 == 0 {BULLET_FRAME_ANIM = (BULLET_FRAME_ANIM + 1) % len(BULLET_TO)}
|
||||||
|
|
||||||
// Press enter to change to ENDING screen
|
|
||||||
if (rl.IsKeyPressed(rl.KeyboardKey.ENTER)) {
|
|
||||||
state.screen = .ENDING
|
|
||||||
log.info("Updated screen enum", state.screen)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Press space to change to fire
|
// Press space to change to fire
|
||||||
if (rl.IsKeyPressed(rl.KeyboardKey.SPACE)) {
|
if (rl.IsKeyPressed(rl.KeyboardKey.SPACE) || rl.IsKeyPressed(rl.KeyboardKey.ENTER)) {
|
||||||
fire_bullet(&bullets, &bullet_index)
|
fire_bullet(&bullets, &bullet_index)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -147,6 +141,7 @@ update_game :: proc(state: ^GameState) {
|
||||||
|
|
||||||
if game_over {
|
if game_over {
|
||||||
screen = .ENDING
|
screen = .ENDING
|
||||||
|
log.info("Game over!", game_end)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -177,7 +172,7 @@ update_game :: proc(state: ^GameState) {
|
||||||
corner_alien_pos :=
|
corner_alien_pos :=
|
||||||
shuffle_dir == .RIGHT ? aliens[ALIENS_PER_ROW - 1].position : aliens[0].position
|
shuffle_dir == .RIGHT ? aliens[ALIENS_PER_ROW - 1].position : aliens[0].position
|
||||||
if corner_alien_pos.x <= SPRITE_CELL ||
|
if corner_alien_pos.x <= SPRITE_CELL ||
|
||||||
corner_alien_pos.x >= f32(screen_width / GLOBAL_SPRITE_SCALE) - SPRITE_CELL ||
|
corner_alien_pos.x >= f32(screen_width) - SPRITE_CELL ||
|
||||||
shuffle_dir == .DOWN {
|
shuffle_dir == .DOWN {
|
||||||
switch shuffle_dir {
|
switch shuffle_dir {
|
||||||
case .RIGHT:
|
case .RIGHT:
|
||||||
|
|
@ -373,7 +368,7 @@ draw_game :: proc(state: ^GameState) {
|
||||||
texture_atlas,
|
texture_atlas,
|
||||||
{HEART_TO.x, HEART_TO.y, SPRITE_CELL, SPRITE_CELL},
|
{HEART_TO.x, HEART_TO.y, SPRITE_CELL, SPRITE_CELL},
|
||||||
{
|
{
|
||||||
f32(screen_width / GLOBAL_SPRITE_SCALE) - f32(hi * SPRITE_CELL * GLOBAL_SPRITE_SCALE),
|
f32(screen_width) - f32(hi * SPRITE),
|
||||||
0,
|
0,
|
||||||
f32(PLAYER_RECT.x),
|
f32(PLAYER_RECT.x),
|
||||||
f32(PLAYER_RECT.y),
|
f32(PLAYER_RECT.y),
|
||||||
|
|
|
||||||
94
main.odin
94
main.odin
|
|
@ -6,9 +6,6 @@ import glm "core:math/linalg/glsl"
|
||||||
|
|
||||||
import rl "vendor:raylib"
|
import rl "vendor:raylib"
|
||||||
|
|
||||||
// orignal resolution of space invaders
|
|
||||||
// 256 x 224 px
|
|
||||||
|
|
||||||
TEXTURE_ATLAS_PATH :: "./assets/texture_atlas.png"
|
TEXTURE_ATLAS_PATH :: "./assets/texture_atlas.png"
|
||||||
|
|
||||||
DEBUG_MODE :: false
|
DEBUG_MODE :: false
|
||||||
|
|
@ -19,55 +16,37 @@ GameEndType :: enum {
|
||||||
AliensReachedPlayer,
|
AliensReachedPlayer,
|
||||||
}
|
}
|
||||||
|
|
||||||
GameState :: struct {
|
|
||||||
// window
|
|
||||||
target_fps: c.int,
|
|
||||||
title: cstring,
|
|
||||||
screen_width: c.int,
|
|
||||||
screen_height: c.int,
|
|
||||||
// frame stats
|
|
||||||
frame_counter: int,
|
|
||||||
current_frame_time: f64,
|
|
||||||
last_frame_time: f64,
|
|
||||||
delta_time: f64,
|
|
||||||
// game vars
|
|
||||||
screen: GameScreen,
|
|
||||||
previous_screen: GameScreen,
|
|
||||||
last_frame_screen: GameScreen,
|
|
||||||
game_end: GameEndType,
|
|
||||||
reset_game: bool,
|
|
||||||
aliens: #soa[ALIENS]Alien,
|
|
||||||
bullets: #soa[MAX_BULLETS]Bullet,
|
|
||||||
bullet_index: int,
|
|
||||||
player_last_time_fired: f64,
|
|
||||||
player_pos: glm.vec2,
|
|
||||||
player_health: c.int,
|
|
||||||
player_score: c.int,
|
|
||||||
player_high_score: c.int,
|
|
||||||
shuffle_dir: ShuffleDirection,
|
|
||||||
last_shuffle_dir: ShuffleDirection,
|
|
||||||
}
|
|
||||||
state: GameState
|
state: GameState
|
||||||
|
ratio: i32
|
||||||
|
|
||||||
texture_atlas_image: rl.Image
|
texture_atlas_image: rl.Image
|
||||||
texture_atlas: rl.Texture2D
|
texture_atlas: rl.Texture2D
|
||||||
|
|
||||||
camera := rl.Camera2D {
|
camera := rl.Camera2D {
|
||||||
zoom = 2,
|
zoom = GLOBAL_SPRITE_SCALE,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
window_width : i32; window_height: i32
|
||||||
|
// orignal resolution of space invaders: 256 x 224 px
|
||||||
setup :: proc(state: ^GameState) {
|
setup :: proc(state: ^GameState) {
|
||||||
using state
|
using state
|
||||||
target_fps = 60
|
target_fps = 60
|
||||||
screen_width = 800 * 2
|
|
||||||
screen_height = 600 * 2
|
monitor := rl.GetCurrentMonitor()
|
||||||
title = "Space Invaders (raylib+odin-lang edition)"
|
window_width = rl.GetMonitorWidth(monitor)
|
||||||
|
window_height = rl.GetMonitorHeight(monitor)
|
||||||
|
|
||||||
current_frame_time = rl.GetTime()
|
current_frame_time = rl.GetTime()
|
||||||
previous_screen = .TITLE
|
previous_screen = .TITLE
|
||||||
screen = .TITLE
|
screen = .TITLE
|
||||||
|
|
||||||
rl.SetTargetFPS(target_fps)
|
rl.SetTargetFPS(target_fps)
|
||||||
|
|
||||||
|
if !ODIN_DEBUG {
|
||||||
|
rl.SetExitKey(nil)
|
||||||
|
} else {
|
||||||
|
log.info("Built with Odin compiler version: ", ODIN_VERSION)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
update :: proc(state: ^GameState) {
|
update :: proc(state: ^GameState) {
|
||||||
|
|
@ -81,13 +60,31 @@ update :: proc(state: ^GameState) {
|
||||||
|
|
||||||
update_screen(state)
|
update_screen(state)
|
||||||
}
|
}
|
||||||
|
target: rl.RenderTexture2D
|
||||||
draw :: proc(state: ^GameState) {
|
draw :: proc(state: ^GameState) {
|
||||||
|
rl.BeginTextureMode(target)
|
||||||
|
{
|
||||||
|
draw_screen(state)
|
||||||
|
}
|
||||||
|
rl.EndTextureMode()
|
||||||
|
|
||||||
rl.BeginDrawing()
|
rl.BeginDrawing()
|
||||||
rl.ClearBackground(rl.RAYWHITE)
|
{
|
||||||
rl.BeginMode2D(camera)
|
rl.ClearBackground(rl.RAYWHITE)
|
||||||
draw_screen(state)
|
rl.DrawTexturePro(
|
||||||
rl.EndMode2D()
|
target.texture,
|
||||||
|
{0, 0, f32(target.texture.width), f32(-target.texture.height)},
|
||||||
|
{
|
||||||
|
f32((window_width - (target.texture.width * ratio)) / 2),
|
||||||
|
0,
|
||||||
|
f32(target.texture.width * ratio),
|
||||||
|
f32(target.texture.height * ratio),
|
||||||
|
},
|
||||||
|
{0, 0},
|
||||||
|
0,
|
||||||
|
rl.WHITE,
|
||||||
|
)
|
||||||
|
}
|
||||||
rl.EndDrawing()
|
rl.EndDrawing()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -96,16 +93,19 @@ main :: proc() {
|
||||||
|
|
||||||
log.info(state.screen)
|
log.info(state.screen)
|
||||||
|
|
||||||
setup(&state)
|
|
||||||
|
|
||||||
rl.InitWindow(state.screen_width, state.screen_height, state.title)
|
rl.InitWindow(1200, 720, "title")
|
||||||
defer rl.CloseWindow()
|
defer rl.CloseWindow()
|
||||||
|
rl.RestoreWindow()
|
||||||
|
rl.SetWindowState({.WINDOW_RESIZABLE, .WINDOW_MAXIMIZED})
|
||||||
|
|
||||||
if !ODIN_DEBUG {
|
setup(&state)
|
||||||
rl.SetExitKey(nil)
|
state.screen_width = 720
|
||||||
} else {
|
state.screen_height = 520
|
||||||
log.info("Built with Odin compiler version: ", ODIN_VERSION)
|
|
||||||
}
|
target = rl.LoadRenderTexture(state.screen_width, state.screen_height)
|
||||||
|
defer rl.UnloadRenderTexture(target)
|
||||||
|
ratio = i32(window_height / target.texture.height)
|
||||||
|
|
||||||
texture_atlas_image = rl.LoadImage(TEXTURE_ATLAS_PATH)
|
texture_atlas_image = rl.LoadImage(TEXTURE_ATLAS_PATH)
|
||||||
texture_atlas = rl.LoadTextureFromImage(texture_atlas_image)
|
texture_atlas = rl.LoadTextureFromImage(texture_atlas_image)
|
||||||
|
|
|
||||||
22
screens.odin
22
screens.odin
|
|
@ -9,7 +9,6 @@ GameScreen :: enum {
|
||||||
ENDING,
|
ENDING,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
update_screen :: proc(state: ^GameState) {
|
update_screen :: proc(state: ^GameState) {
|
||||||
using state
|
using state
|
||||||
|
|
||||||
|
|
@ -51,27 +50,22 @@ draw_screen :: proc(state: ^GameState) {
|
||||||
switch state.screen {
|
switch state.screen {
|
||||||
case .TITLE:
|
case .TITLE:
|
||||||
{
|
{
|
||||||
rl.DrawRectangle(0, 0, state.screen_width, state.screen_height, rl.WHITE)
|
rl.DrawRectangle(0, 0, state.screen_width, state.screen_height, rl.BLACK)
|
||||||
|
|
||||||
rl.DrawTexturePro(
|
rl.DrawTexturePro(
|
||||||
texture_atlas,
|
texture_atlas,
|
||||||
{LOGO_TO[0].x, LOGO_TO[0].y, LOGO_TO[1].x, LOGO_TO[1].y},
|
{LOGO_TO[0].x, LOGO_TO[0].y, LOGO_TO[1].x, LOGO_TO[1].y},
|
||||||
{
|
{0, 0, LOGO_TO[1].x * 4, LOGO_TO[1].y * 4},
|
||||||
f32(screen_width / GLOBAL_SPRITE_SCALE) / 2,
|
{f32(-screen_width/4), f32(-screen_height/3)},
|
||||||
f32(screen_height / GLOBAL_SPRITE_SCALE) / 2,
|
|
||||||
LOGO_TO[1].x * 4,
|
|
||||||
LOGO_TO[1].y * 4,
|
|
||||||
},
|
|
||||||
{LOGO_TO[1].x * 2, LOGO_TO[1].y * 2},
|
|
||||||
0,
|
0,
|
||||||
rl.GREEN,
|
rl.GREEN,
|
||||||
)
|
)
|
||||||
text : cstring = "PRESS ENTER TO START GAME"
|
text: cstring = "PRESS ENTER TO START GAME"
|
||||||
size := rl.MeasureText(text, 20)
|
size := rl.MeasureText(text, 20)
|
||||||
rl.DrawText(
|
rl.DrawText(
|
||||||
text,
|
text,
|
||||||
(screen_width/4) - (size/2),
|
(screen_width / 2) - (size / 2),
|
||||||
(screen_height / 2)- 20,
|
screen_height - 20,
|
||||||
20,
|
20,
|
||||||
rl.DARKGREEN,
|
rl.DARKGREEN,
|
||||||
)
|
)
|
||||||
|
|
|
||||||
|
|
@ -9,22 +9,23 @@ LOGO_TO :: [2]glm.vec2{{0, 0}, {128-(SPRITE_CELL * 2), SPRITE_CELL * 2}}
|
||||||
|
|
||||||
GLOBAL_SPRITE_SCALE :: 2
|
GLOBAL_SPRITE_SCALE :: 2
|
||||||
SPRITE_CELL :: 16
|
SPRITE_CELL :: 16
|
||||||
|
SPRITE :: SPRITE_CELL * GLOBAL_SPRITE_SCALE
|
||||||
|
|
||||||
ALIEN_ROWS :: 5
|
ALIEN_ROWS :: 5
|
||||||
ALIENS_PER_ROW :: 11
|
ALIENS_PER_ROW :: 11
|
||||||
ALIENS :: ALIEN_ROWS * ALIENS_PER_ROW
|
ALIENS :: ALIEN_ROWS * ALIENS_PER_ROW
|
||||||
ALIEN_SIDE_STEP :: 20 * GLOBAL_SPRITE_SCALE
|
ALIEN_SIDE_STEP :: 20 * GLOBAL_SPRITE_SCALE
|
||||||
ALIEN_RECT :: glm.vec2{SPRITE_CELL * GLOBAL_SPRITE_SCALE, SPRITE_CELL * GLOBAL_SPRITE_SCALE}
|
ALIEN_RECT :: glm.vec2{SPRITE, SPRITE}
|
||||||
// note: this multiplication by GLOBAL_SPRITE_SCALE is a hack, but it's a weekend project
|
// note: this multiplication by GLOBAL_SPRITE_SCALE is a hack, but it's a weekend project
|
||||||
// so i won't bother refactoring it
|
// so i won't bother refactoring it
|
||||||
|
|
||||||
MAX_BULLETS :: 100
|
MAX_BULLETS :: 100
|
||||||
BULLET_SPEED :: 240
|
BULLET_SPEED :: 240
|
||||||
BULLET_RECT :: glm.vec2{SPRITE_CELL * GLOBAL_SPRITE_SCALE, SPRITE_CELL * GLOBAL_SPRITE_SCALE}
|
BULLET_RECT :: glm.vec2{SPRITE, SPRITE}
|
||||||
|
|
||||||
MAX_PLAYER_HEALTH :: 3
|
MAX_PLAYER_HEALTH :: 3
|
||||||
PLAYER_SPEED :: 120
|
PLAYER_SPEED :: 120
|
||||||
PLAYER_RECT :: glm.vec2{SPRITE_CELL * GLOBAL_SPRITE_SCALE, SPRITE_CELL * GLOBAL_SPRITE_SCALE}
|
PLAYER_RECT :: glm.vec2{SPRITE, SPRITE}
|
||||||
|
|
||||||
// texture offset for ship
|
// texture offset for ship
|
||||||
SHIP_TO :: glm.vec2{0, 112}
|
SHIP_TO :: glm.vec2{0, 112}
|
||||||
|
|
|
||||||
33
state.odin
Normal file
33
state.odin
Normal file
|
|
@ -0,0 +1,33 @@
|
||||||
|
package space_invaders
|
||||||
|
|
||||||
|
import "core:c"
|
||||||
|
import glm "core:math/linalg/glsl"
|
||||||
|
|
||||||
|
GameState :: struct {
|
||||||
|
// window
|
||||||
|
target_fps: c.int,
|
||||||
|
title: cstring,
|
||||||
|
screen_width: c.int,
|
||||||
|
screen_height: c.int,
|
||||||
|
// frame stats
|
||||||
|
frame_counter: int,
|
||||||
|
current_frame_time: f64,
|
||||||
|
last_frame_time: f64,
|
||||||
|
delta_time: f64,
|
||||||
|
// game vars
|
||||||
|
screen: GameScreen,
|
||||||
|
previous_screen: GameScreen,
|
||||||
|
last_frame_screen: GameScreen,
|
||||||
|
game_end: GameEndType,
|
||||||
|
reset_game: bool,
|
||||||
|
aliens: #soa[ALIENS]Alien,
|
||||||
|
bullets: #soa[MAX_BULLETS]Bullet,
|
||||||
|
bullet_index: int,
|
||||||
|
player_last_time_fired: f64,
|
||||||
|
player_pos: glm.vec2,
|
||||||
|
player_health: c.int,
|
||||||
|
player_score: c.int,
|
||||||
|
player_high_score: c.int,
|
||||||
|
shuffle_dir: ShuffleDirection,
|
||||||
|
last_shuffle_dir: ShuffleDirection,
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue