basic sprites working

This commit is contained in:
Stefan Stefanov 2024-02-09 22:06:25 +02:00
parent 178cf92f44
commit 348b4fd181
6 changed files with 216 additions and 63 deletions

8
Makefile Normal file
View file

@ -0,0 +1,8 @@
build:
odin run . -out:space_invaders.exe -debug
clean:
rm -rf ./space_invaders.exe
run:
space_invaders.exe

BIN
assets/texture_atlas.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 709 B

200
game.odin
View file

@ -7,25 +7,45 @@ import glm "core:math/linalg/glsl"
import rl "vendor:raylib" import rl "vendor:raylib"
GLOBAL_SPRITE_SCALE :: 2 GLOBAL_SPRITE_SCALE :: 2
SPRITE_CELL :: 16
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.ivec2{16 * GLOBAL_SPRITE_SCALE, 16 * GLOBAL_SPRITE_SCALE} ALIEN_RECT :: glm.vec2{SPRITE_CELL * GLOBAL_SPRITE_SCALE, SPRITE_CELL * GLOBAL_SPRITE_SCALE}
MAX_BULLETS :: 100 MAX_BULLETS :: 100
BULLET_SPEED :: 240 BULLET_SPEED :: 240
BULLET_RECT :: glm.ivec2{16 * GLOBAL_SPRITE_SCALE, 16 * GLOBAL_SPRITE_SCALE} BULLET_RECT :: glm.vec2{SPRITE_CELL * GLOBAL_SPRITE_SCALE, SPRITE_CELL * GLOBAL_SPRITE_SCALE}
MAX_PLAYER_HEALTH :: 3 MAX_PLAYER_HEALTH :: 3
PLAYER_SPEED :: 40 PLAYER_SPEED :: 120
PLAYER_RECT :: glm.ivec2{16 * GLOBAL_SPRITE_SCALE, 16 * GLOBAL_SPRITE_SCALE} PLAYER_RECT :: glm.vec2{SPRITE_CELL * GLOBAL_SPRITE_SCALE, SPRITE_CELL * GLOBAL_SPRITE_SCALE}
// texture offset for ship
SHIP_TO :: glm.vec2{0, 112}
BULLET_TO :: glm.vec2{32, 112}
AlienKind :: enum {
ORANGE,
GREEN,
YELLOW,
RED,
}
// texture atlas offset for aliens
ALIENS_TO := [AlienKind]glm.vec2 {
.ORANGE = {0, 128 - (SPRITE_CELL * 2)},
.GREEN = {SPRITE_CELL * 1, 128 - (SPRITE_CELL * 2)},
.YELLOW = {SPRITE_CELL * 2, 128 - (SPRITE_CELL * 2)},
.RED = {SPRITE_CELL * 3, 128 - (SPRITE_CELL * 2)},
}
Alien :: struct { Alien :: struct {
alive: bool, alive: bool,
position: glm.vec2, position: glm.vec2,
id: int, id: AlienKind,
} }
Bullet :: struct { Bullet :: struct {
@ -33,26 +53,50 @@ Bullet :: struct {
position: glm.vec2, position: glm.vec2,
} }
collideAABB :: proc(
a_rect: glm.vec2,
a_pos: ^glm.vec2,
b_rect: glm.vec2,
b_pos: ^glm.vec2,
) -> bool {
return(
a_pos.x < b_pos.x + f32(b_rect.x / 2) &&
a_pos.x + f32(a_rect.x / 2) > b_pos.x &&
a_pos.y < b_pos.y + f32(b_rect.y / 2) &&
a_pos.y + f32(a_rect.y / 2) > b_pos.y \
)
}
setup_game :: proc(state: ^GameState) { setup_game :: proc(state: ^GameState) {
using state using state
player_pos = glm.vec2{f32(screen_width / 2), f32(screen_height - PLAYER_RECT.x)} player_pos = glm.vec2{f32(screen_width / 2), f32(screen_height) - PLAYER_RECT.x}
// setup the initial positions of the aliens // setup the initial positions of the aliens
for row in 0 ..< ALIEN_ROWS { for row in 0 ..< ALIEN_ROWS {
for alien in 0 ..< ALIENS_PER_ROW { for alien in 0 ..< ALIENS_PER_ROW {
aliens[(row * ALIENS_PER_ROW) + alien].position = glm.vec2 { alien_ptr := &aliens[(row * ALIENS_PER_ROW) + alien]
alien_ptr.position = glm.vec2 {
f32((alien + 1) * int(ALIEN_RECT.x + 10)), f32((alien + 1) * int(ALIEN_RECT.x + 10)),
f32((row + 1) * int(ALIEN_RECT.x + 10)), f32((row + 1) * int(ALIEN_RECT.x + 10)),
} }
alien_ptr.alive = true
} }
} }
// aliens[ALIENS - 1].alive = true
if player_score > player_high_score {
player_high_score = player_score
}
player_score = 0
reset_game = false
} }
update_game :: proc(state: ^GameState) { update_game :: proc(state: ^GameState) {
using state using state
// If we're entering the game screen then we need to setup initial state of the game variables // If we're entering the game screen then we need to setup initial state of the game variables
if last_frame_screen != .GAMEPLAY { if last_frame_screen != .GAMEPLAY || reset_game {
setup_game(state) setup_game(state)
log.info("Done setting up game") log.info("Done setting up game")
} }
@ -66,10 +110,10 @@ update_game :: proc(state: ^GameState) {
// Press space to change to fire // Press space to change to fire
if (rl.IsKeyPressed(rl.KeyboardKey.SPACE)) { if (rl.IsKeyPressed(rl.KeyboardKey.SPACE)) {
log.info("FIRE!") log.info("FIRE!")
bullet := &bullets[bullet_index] bullet := &bullets[bullet_index];bullet_index = (bullet_index + 1) % MAX_BULLETS
bullet.alive = true bullet.alive = true
bullet.position = player_pos bullet.position = player_pos
bullet.position.y = bullet.position.y - f32((PLAYER_RECT.y / 2) + BULLET_RECT.y / 2) bullet.position.y = bullet.position.y - ((PLAYER_RECT.y / 2) + BULLET_RECT.y / 2.0)
} }
if (rl.IsKeyDown(rl.KeyboardKey.RIGHT)) { if (rl.IsKeyDown(rl.KeyboardKey.RIGHT)) {
@ -79,39 +123,115 @@ update_game :: proc(state: ^GameState) {
player_pos.x = player_pos.x - f32(PLAYER_SPEED * delta_time) player_pos.x = player_pos.x - f32(PLAYER_SPEED * delta_time)
} }
// movement update all_aliens_dead := true
{ for &alien in aliens {
for &bullet in bullets { if !alien.alive {continue}
if bullet.alive { all_aliens_dead = false
bullet.position.y = bullet.position.y - f32(BULLET_SPEED * delta_time) }
}
}
}
}
draw_game :: proc(state: ^GameState) { if all_aliens_dead {
using state screen = .ENDING
rl.DrawRectangle(0, 0, state.screen_width, state.screen_height, rl.BLACK) }
// rl.DrawText("GAMEPLAY SCREEN", 20, 20, 40, rl.MAROON)
// rl.DrawText("PRESS ENTER or TAP to JUMP to ENDING SCREEN", 130, 220, 20, rl.MAROON)
rl.DrawCircle(c.int(player_pos.x), c.int(player_pos.y), f32(PLAYER_RECT.x / 2), rl.RED)
for row in 0 ..< ALIEN_ROWS { // movement update
for alien in 0 ..< ALIENS_PER_ROW { for &bullet in bullets {
alien := &aliens[(row * ALIENS_PER_ROW) + alien] if !bullet.alive {continue}
rl.DrawCircle(
c.int(alien.position.x), bullet.position.y = bullet.position.y - f32(BULLET_SPEED * delta_time)
c.int(alien.position.y), }
f32(ALIEN_RECT.x / 2),
rl.YELLOW, for &bullet in bullets {
) if !bullet.alive {continue}
for &alien in aliens {
if !alien.alive {continue}
if collideAABB(ALIEN_RECT, &alien.position, BULLET_RECT, &bullet.position) {
bullet.alive = false
alien.alive = false
player_score += 10
log.info("HIT 'EM HARD")
}
} }
} }
for &bullet in bullets {
if !bullet.alive { continue }
rl.DrawCircle(c.int(bullet.position.x), c.int(bullet.position.y), f32(BULLET_RECT.x / 2), rl.WHITE)
}
}
ship_sprite_cell_offset := rl.Rectangle{SHIP_TO.x, SHIP_TO.y, SPRITE_CELL, SPRITE_CELL}
bullet_sprite_cell_offset := rl.Rectangle{BULLET_TO.x, BULLET_TO.y, SPRITE_CELL, SPRITE_CELL}
draw_game :: proc(state: ^GameState) {
using state
rl.DrawRectangle(0, 0, state.screen_width, state.screen_height, rl.BLACK)
rl.DrawTexturePro(
texture_atlas,
ship_sprite_cell_offset,
{player_pos.x, player_pos.y, f32(PLAYER_RECT.x), f32(PLAYER_RECT.y)},
{SPRITE_CELL, SPRITE_CELL},
0,
rl.WHITE,
)
for alien in aliens {
if !alien.alive {continue}
rl.DrawTexturePro(
texture_atlas,
{ALIENS_TO[alien.id].x, ALIENS_TO[alien.id].y, SPRITE_CELL, SPRITE_CELL},
{alien.position.x, alien.position.y, SPRITE_CELL, SPRITE_CELL},
{SPRITE_CELL, SPRITE_CELL},
0,
rl.WHITE,
)
// rl.DrawCircle(
// c.int(alien.position.x),
// c.int(alien.position.y),
// f32(ALIEN_RECT.x / 2),
// rl.YELLOW,
// )
}
for &bullet in bullets {
if !bullet.alive {continue}
rl.DrawCircle(
c.int(bullet.position.x),
c.int(bullet.position.y),
f32(BULLET_RECT.x / 2),
rl.WHITE,
)
}
if DEBUG_MODE {
for &bullet in bullets {
if !bullet.alive {continue}
rl.DrawRectangleLines(
c.int(bullet.position.x - (BULLET_RECT.x / 2)),
c.int(bullet.position.y - (BULLET_RECT.y / 2)),
c.int(BULLET_RECT.x),
c.int(BULLET_RECT.y),
rl.YELLOW,
)
}
for &alien in aliens {
if !alien.alive {continue}
rl.DrawRectangleLines(
c.int(alien.position.x - (ALIEN_RECT.x / 2)),
c.int(alien.position.y - (ALIEN_RECT.y / 2)),
c.int(ALIEN_RECT.x),
c.int(ALIEN_RECT.y),
rl.YELLOW,
)
}
rl.DrawRectangleLines(
c.int(player_pos.x - (PLAYER_RECT.x / 2)),
c.int(player_pos.y - (PLAYER_RECT.y / 2)),
c.int(PLAYER_RECT.x),
c.int(PLAYER_RECT.y),
rl.YELLOW,
)
}
rl.DrawText(rl.TextFormat("Score: %d", player_score), 130, 220, 20, rl.MAROON)
// rl.DrawText("GAMEPLAY SCREEN", 20, 20, 40, rl.MAROON) // rl.DrawText("GAMEPLAY SCREEN", 20, 20, 40, rl.MAROON)
} }

View file

@ -9,6 +9,11 @@ import rl "vendor:raylib"
// orignal resolution of space invaders // orignal resolution of space invaders
// 256 x 224 px // 256 x 224 px
TEXTURE_ATLAS_PATH :: "./assets/texture_atlas.png"
DEBUG_MODE :: true
GameState :: struct { GameState :: struct {
// window // window
target_fps: c.int, target_fps: c.int,
@ -24,15 +29,20 @@ GameState :: struct {
screen: GameScreen, screen: GameScreen,
previous_screen: GameScreen, previous_screen: GameScreen,
last_frame_screen: GameScreen, last_frame_screen: GameScreen,
reset_game: bool,
aliens: #soa[ALIENS]Alien, aliens: #soa[ALIENS]Alien,
bullets: #soa[MAX_BULLETS]Bullet, bullets: #soa[MAX_BULLETS]Bullet,
bullet_index: int, bullet_index: int,
player_pos: glm.vec2, player_pos: glm.vec2,
player_health: c.int, player_health: c.int,
player_score: c.int, player_score: c.int,
player_high_score: c.int,
} }
state: GameState state: GameState
texture_atlas_image : rl.Image
texture_atlas : rl.Texture2D
setup :: proc(state: ^GameState) { setup :: proc(state: ^GameState) {
using state using state
target_fps = 60 target_fps = 60
@ -76,6 +86,12 @@ main :: proc() {
rl.InitWindow(state.screen_width, state.screen_height, state.title) rl.InitWindow(state.screen_width, state.screen_height, state.title)
defer rl.CloseWindow() defer rl.CloseWindow()
texture_atlas_image = rl.LoadImage(TEXTURE_ATLAS_PATH)
texture_atlas = rl.LoadTextureFromImage(texture_atlas_image)
rl.UnloadImage(texture_atlas_image)
log.info("Loaded images")
for !rl.WindowShouldClose() { for !rl.WindowShouldClose() {
update(&state) update(&state)
draw(&state) draw(&state)

10
ols.json Normal file
View file

@ -0,0 +1,10 @@
{
"$schema": "https://raw.githubusercontent.com/DanielGavin/ols/master/misc/ols.schema.json",
"collections": [
{ "name": "dependencies", "path": "../dependencies/" }
],
"enable_semantic_tokens": true,
"enable_document_symbols": true,
"enable_hover": true,
"enable_snippets": true
}

View file

@ -12,26 +12,27 @@ GameScreen :: enum {
update_screen :: proc(state: ^GameState) { update_screen :: proc(state: ^GameState) {
using state using state
switch screen { switch screen {
case .LOGO: case .LOGO:
{ {
// Wait for 2 seconds (120 frames) before jumping to TITLE screen // Wait for 2 seconds (120 frames) before jumping to TITLE screen
if (frame_counter > int(target_fps * 2)) { if (frame_counter > int(target_fps * 2)) {
previous_screen = screen previous_screen = screen
screen = .TITLE screen = .TITLE
log.info("Updated screen enum", screen) log.info("Updated screen enum", screen)
} }
} }
case .TITLE: case .TITLE:
{ {
// Press enter to change to GAMEPLAY screen // Press enter to change to GAMEPLAY screen
if (rl.IsKeyPressed(rl.KeyboardKey.ENTER)) { if (rl.IsKeyPressed(rl.KeyboardKey.ENTER)) {
previous_screen = screen previous_screen = screen
screen = .GAMEPLAY screen = .GAMEPLAY
log.info("Updated screen enum", screen) log.info("Updated screen enum", screen)
} }
reset_game = true
} }
case .GAMEPLAY: case .GAMEPLAY:
{ {
@ -39,19 +40,19 @@ update_screen :: proc(state: ^GameState) {
} }
case .ENDING: case .ENDING:
{ {
// Press enter to return to TITLE screen // Press enter to return to TITLE screen
if (rl.IsKeyPressed(rl.KeyboardKey.ENTER)) { if (rl.IsKeyPressed(rl.KeyboardKey.ENTER)) {
previous_screen = screen previous_screen = screen
screen = .TITLE screen = .TITLE
log.info("Updated screen enum", screen) log.info("Updated screen enum", screen)
} }
} }
} }
if last_frame_screen != screen { if last_frame_screen != screen {
log.infof("Current screen: %v, previous screen: %v", screen, previous_screen) log.infof("Current screen: %v, previous screen: %v", screen, previous_screen)
} }
last_frame_screen = screen last_frame_screen = screen
} }
draw_screen :: proc(state: ^GameState) { draw_screen :: proc(state: ^GameState) {
@ -84,18 +85,16 @@ draw_screen :: proc(state: ^GameState) {
} }
case .ENDING: case .ENDING:
{ {
// TODO: Draw ENDING screen here! draw_ending_screen(state)
rl.DrawRectangle(0, 0, state.screen_width, state.screen_height, rl.BLUE)
rl.DrawText("ENDING SCREEN", 20, 20, 40, rl.DARKBLUE)
rl.DrawText(
"PRESS ENTER or TAP to RETURN to TITLE SCREEN",
120,
220,
20,
rl.DARKBLUE,
)
} }
} }
} }
} }
draw_ending_screen :: proc(state: ^GameState) {
using state
// TODO: Draw ENDING screen here!
rl.DrawRectangle(0, 0, state.screen_width, state.screen_height, rl.BLACK)
rl.DrawText("Game End", 20, 20, 40, rl.WHITE)
rl.DrawText(rl.TextFormat("Player score: %d\nHighscore: %d", player_score, player_high_score), 120, 220, 20, rl.WHITE)
}