working game loop, lacking alien shooting

This commit is contained in:
Stefan Stefanov 2024-02-10 14:59:13 +02:00
parent 0aa534c4d1
commit 19bff3176d
4 changed files with 194 additions and 76 deletions

236
game.odin
View file

@ -28,6 +28,14 @@ SHIP_TO :: glm.vec2{0, 112}
BULLET_TO := [2]glm.vec2{{0, 80}, {16, 80}}
BULLET_FRAME_ANIM := 0
ShuffleDirection :: enum {
RIGHT,
LEFT,
DOWN,
}
STEP_MULTIPLIER_DEFAULT :: 10.0
step_multiplier := STEP_MULTIPLIER_DEFAULT
AlienKind :: enum {
ORANGE,
GREEN,
@ -50,16 +58,12 @@ Alien :: struct {
}
Bullet :: struct {
alive: bool,
position: glm.vec2,
alive: bool,
player_bullet: bool,
position: glm.vec2,
}
collideAABB :: proc(
a_rect: glm.vec2,
a_pos: ^glm.vec2,
b_rect: glm.vec2,
b_pos: ^glm.vec2,
) -> bool {
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 &&
@ -70,7 +74,15 @@ collideAABB :: proc(
setup_game :: proc(state: ^GameState) {
using state
player_pos = glm.vec2{f32(screen_width / 2), f32(screen_height) - PLAYER_RECT.x}
player_pos = glm.vec2 {
f32(screen_width) / (2 * camera.zoom),
f32(screen_height) / camera.zoom - PLAYER_RECT.x,
}
player_score = 0
player_health = 3
step_multiplier = STEP_MULTIPLIER_DEFAULT
// setup the initial positions of the aliens
for row in 0 ..< ALIEN_ROWS {
@ -81,14 +93,13 @@ setup_game :: proc(state: ^GameState) {
f32((row + 1) * int(ALIEN_RECT.x + 10)),
}
alien_ptr.alive = true
alien_ptr.id = row % 2 == 0 ? .ORANGE : .GREEN
}
}
// aliens[ALIENS - 1].alive = true
if player_score > player_high_score {
player_high_score = player_score
}
player_score = 0
reset_game = false
}
@ -96,72 +107,155 @@ setup_game :: proc(state: ^GameState) {
update_game :: proc(state: ^GameState) {
using state
// If we're entering the game screen then we need to setup initial state of the game variables
if last_frame_screen != .GAMEPLAY || reset_game {
setup_game(state)
log.info("Done setting up game")
// Poll for keyboard commands (input)
{
// If we're entering the game screen then we need to setup initial state of the game variables
if last_frame_screen != .GAMEPLAY || reset_game {
setup_game(state)
log.info("Done setting up game")
}
// update bullet frame idx
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
if (rl.IsKeyPressed(rl.KeyboardKey.SPACE)) {
log.info("FIRE!")
bullet := &bullets[bullet_index];bullet_index = (bullet_index + 1) % MAX_BULLETS
bullet.alive = true
bullet.player_bullet = true
bullet.position = player_pos
bullet.position.y = bullet.position.y - ((PLAYER_RECT.y / 2) + BULLET_RECT.y / 2.0)
}
if (rl.IsKeyDown(rl.KeyboardKey.RIGHT)) {
player_pos.x = player_pos.x + f32(PLAYER_SPEED * delta_time)
}
if (rl.IsKeyDown(rl.KeyboardKey.LEFT)) {
player_pos.x = player_pos.x - f32(PLAYER_SPEED * delta_time)
}
}
// update bullet frame idx
if frame_counter % 10 == 0 { BULLET_FRAME_ANIM = (BULLET_FRAME_ANIM + 1) % len(BULLET_TO) }
// Check ending scenarios
{
game_over := false
if player_health <= 0 {
game_over = true
game_end = .PlayerDied
}
// 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
if (rl.IsKeyPressed(rl.KeyboardKey.SPACE)) {
log.info("FIRE!")
bullet := &bullets[bullet_index];bullet_index = (bullet_index + 1) % MAX_BULLETS
bullet.alive = true
bullet.position = player_pos
bullet.position.y = bullet.position.y - ((PLAYER_RECT.y / 2) + BULLET_RECT.y / 2.0)
}
if (rl.IsKeyDown(rl.KeyboardKey.RIGHT)) {
player_pos.x = player_pos.x + f32(PLAYER_SPEED * delta_time)
}
if (rl.IsKeyDown(rl.KeyboardKey.LEFT)) {
player_pos.x = player_pos.x - f32(PLAYER_SPEED * delta_time)
}
all_aliens_dead := true
for &alien in aliens {
if !alien.alive {continue}
all_aliens_dead = false
}
if all_aliens_dead {
screen = .ENDING
}
// movement update
for &bullet in bullets {
if !bullet.alive {continue}
bullet.position.y = bullet.position.y - f32(BULLET_SPEED * delta_time)
}
for &bullet in bullets {
if !bullet.alive {continue}
all_aliens_dead := true
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")
if alien.alive {
all_aliens_dead = false
if alien.position.y + ALIEN_RECT.y >= player_pos.y {
game_end = .AliensReachedPlayer
game_over = true
break
}
}
}
if all_aliens_dead && !game_over {
game_over = true
game_end = .AllAliensKilled
}
if game_over {
screen = .ENDING
return
}
}
// Update bullets & aliens
{
for &bullet, bi in bullets {
if !bullet.alive {continue}
// Update bullet pos first
bullet.position.y -= f32(BULLET_SPEED * delta_time)
}
corner_alien_pos :=
shuffle_dir == .RIGHT ? aliens[ALIENS_PER_ROW - 1].position : aliens[0].position
if corner_alien_pos.x <= SPRITE_CELL ||
corner_alien_pos.x >= f32(screen_width / GLOBAL_SPRITE_SCALE) - SPRITE_CELL ||
shuffle_dir == .DOWN {
switch shuffle_dir {
case .RIGHT:
fallthrough
case .LEFT:
{
last_shuffle_dir = shuffle_dir
shuffle_dir = .DOWN
}
case .DOWN:
{
step_multiplier += 1.0
shuffle_dir = last_shuffle_dir == .RIGHT ? .LEFT : .RIGHT
last_shuffle_dir = .DOWN
}
}
}
shuffle_step_size: f64 = f64(SPRITE_CELL / 2) * delta_time * step_multiplier
alien_vel: f64 = shuffle_dir == .RIGHT ? shuffle_step_size : -shuffle_step_size
for &alien, ai in aliens {
// Update alien pos first
if shuffle_dir != .DOWN {
alien.position.x += f32(alien_vel)
} else {
alien.position.y += ALIEN_RECT.y
}
// We will update the positions regardless if it's dead or not
// but only check collisions if the alien is alive
if !alien.alive {continue}
for &bullet, bi in bullets {
if !bullet.alive {continue}
// Collision check bullet
if collideAABB(ALIEN_RECT, alien.position, BULLET_RECT, bullet.position) {
bullet, alien = damage_alien(state, bullets[bi], aliens[ai])
}
}
}
}
}
// 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}
// Since I'm using a #soa array I can't directly modify the alien & bullet entities...
damage_alien :: proc(
state: ^GameState,
bullet_in: Bullet,
alien_in: Alien,
) -> (
bullet: Bullet,
alien: Alien,
) {
using state
bullet = bullet_in
alien = alien_in
bullet.alive = false
alien.alive = false
// Count score or take player health depending whose bullet it is
player_score += 10
log.info("HIT 'EM HARD")
return
}
damage_player :: proc(state: ^GameState, bullet_in: Bullet) -> (bullet: Bullet) {
using state
bullet = bullet_in
bullet.alive = false
player_health -= 1
return
}
draw_game :: proc(state: ^GameState) {
using state
@ -199,7 +293,12 @@ draw_game :: proc(state: ^GameState) {
if !bullet.alive {continue}
rl.DrawTexturePro(
texture_atlas,
{BULLET_TO[BULLET_FRAME_ANIM].x, BULLET_TO[BULLET_FRAME_ANIM].y, SPRITE_CELL, SPRITE_CELL},
{
BULLET_TO[BULLET_FRAME_ANIM].x,
BULLET_TO[BULLET_FRAME_ANIM].y,
SPRITE_CELL,
SPRITE_CELL,
},
{bullet.position.x, bullet.position.y, BULLET_RECT.x, BULLET_RECT.y},
{SPRITE_CELL, SPRITE_CELL},
0,
@ -238,7 +337,6 @@ draw_game :: proc(state: ^GameState) {
)
}
rl.DrawText(rl.TextFormat("Score: %d", player_score), 130, 220, 20, rl.MAROON)
rl.DrawText(rl.TextFormat("Score: %d", player_score), 20, 0, 20, rl.WHITE)
// rl.DrawText("GAMEPLAY SCREEN", 20, 20, 40, rl.MAROON)
}