383 lines
9 KiB
Odin
383 lines
9 KiB
Odin
package space_invaders
|
|
|
|
import "core:c"
|
|
import "core:log"
|
|
import glm "core:math/linalg/glsl"
|
|
import "core:math/rand"
|
|
import "core:time"
|
|
|
|
import rl "vendor:raylib"
|
|
|
|
ShuffleDirection :: enum {
|
|
RIGHT,
|
|
LEFT,
|
|
DOWN,
|
|
}
|
|
|
|
STEP_MULTIPLIER_DEFAULT :: 10.0
|
|
step_multiplier := STEP_MULTIPLIER_DEFAULT
|
|
|
|
AlienKind :: enum {
|
|
ORANGE,
|
|
GREEN,
|
|
YELLOW,
|
|
RED,
|
|
}
|
|
|
|
Alien :: struct {
|
|
alive: bool,
|
|
playing_death_animation: bool,
|
|
death_animation_index: int,
|
|
position: glm.vec2,
|
|
id: AlienKind,
|
|
last_time_fired: f64,
|
|
}
|
|
|
|
Bullet :: struct {
|
|
alive: bool,
|
|
player_bullet: bool,
|
|
position: glm.vec2,
|
|
}
|
|
|
|
rng := rand.Rand{}
|
|
|
|
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) {
|
|
using state
|
|
|
|
player_pos = glm.vec2 {
|
|
f32(screen_width) / 2,
|
|
f32(screen_height) - 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 {
|
|
for alien in 0 ..< ALIENS_PER_ROW {
|
|
alien_ptr := &aliens[(row * ALIENS_PER_ROW) + alien]
|
|
alien_ptr.position = glm.vec2 {
|
|
f32((alien + 1) * int(ALIEN_RECT.x + 10)),
|
|
f32((row + 1) * int(ALIEN_RECT.x + 10)),
|
|
}
|
|
alien_ptr.alive = true
|
|
alien_ptr.id = transmute(AlienKind)(row % 4)
|
|
}
|
|
}
|
|
for &bullet in bullets {
|
|
bullet.alive = false
|
|
}
|
|
|
|
if player_score > player_high_score {
|
|
player_high_score = player_score
|
|
}
|
|
|
|
reset_game = false
|
|
}
|
|
|
|
update_game :: proc(state: ^GameState) {
|
|
using state
|
|
|
|
rand.init(&rng, transmute(u64)time.time_to_unix_nano(time.now()))
|
|
|
|
// 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 space to change to fire
|
|
if (rl.IsKeyPressed(rl.KeyboardKey.SPACE) || rl.IsKeyPressed(rl.KeyboardKey.ENTER)) {
|
|
fire_bullet(&bullets, &bullet_index)
|
|
}
|
|
|
|
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)
|
|
}
|
|
|
|
}
|
|
|
|
// Check ending scenarios
|
|
{
|
|
game_over := false
|
|
if player_health <= 0 {
|
|
game_over = true
|
|
game_end = .PlayerDied
|
|
}
|
|
|
|
all_aliens_dead := true
|
|
for &alien in aliens {
|
|
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
|
|
log.info("Game over!", game_end)
|
|
return
|
|
}
|
|
}
|
|
|
|
// Go over the aliens on the bottom of the column & roll rng
|
|
// to shoot a bullet towards the player
|
|
for alien_idx in 0 ..< ALIENS_PER_ROW {
|
|
result := rand.uint32(&rng)
|
|
for row := ALIEN_ROWS - 1; row >= 0; row -= 1 {
|
|
alien := aliens[(row * ALIENS_PER_ROW) + alien_idx]
|
|
if alien.alive {
|
|
if result % 6 == 0 && frame_counter % int(target_fps * 1) == 0 {
|
|
fire_bullet(&bullets, &bullet_index, false, &alien)
|
|
}
|
|
break
|
|
}
|
|
}
|
|
}
|
|
|
|
// Update bullets & aliens
|
|
{
|
|
for &bullet, bi in bullets {
|
|
if !bullet.alive {continue}
|
|
// Update bullet pos first
|
|
bullet_dir: f32 = bullet.player_bullet ? -1 : 1
|
|
bullet.position.y += f32(BULLET_SPEED * delta_time) * bullet_dir
|
|
}
|
|
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) - 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 bullet.player_bullet {
|
|
if collideAABB(ALIEN_RECT, alien.position, BULLET_RECT, bullet.position) {
|
|
bullet, alien = damage_alien(state, bullets[bi], aliens[ai])
|
|
}
|
|
} else {
|
|
if collideAABB(PLAYER_RECT, player_pos, BULLET_RECT, bullet.position) {
|
|
bullet = damage_player(state, bullets[bi])
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
fire_bullet :: proc(
|
|
bullets: ^#soa[MAX_BULLETS]Bullet,
|
|
bullet_index: ^int,
|
|
shot_from_player := true,
|
|
alien: ^Alien = nil,
|
|
) {
|
|
using state
|
|
|
|
bullet := &bullets[bullet_index];bullet_index = (bullet_index + 1) % MAX_BULLETS
|
|
bullet.alive = true
|
|
if shot_from_player {
|
|
bullet.player_bullet = true
|
|
bullet.position = player_pos
|
|
bullet.position.y = bullet.position.y - ((PLAYER_RECT.y / 2) + BULLET_RECT.y / 2)
|
|
} else if alien != nil {
|
|
bullet.player_bullet = false
|
|
bullet.position = alien.position
|
|
bullet.position.y = bullet.position.y + ((ALIEN_RECT.y / 2) + ALIEN_RECT.y / 2)
|
|
}
|
|
log.info("Fired bullet: ", bullet)
|
|
}
|
|
|
|
// Since I'm using a #soa array I can't directly modify the alien & bullet entities...I think?
|
|
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
|
|
alien.playing_death_animation = true
|
|
alien.death_animation_index = 0
|
|
// Count score or take player health depending whose bullet it is
|
|
player_score += 10
|
|
log.info("Hit alien: ", alien)
|
|
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
|
|
|
|
rl.DrawRectangle(0, 0, state.screen_width, state.screen_height, rl.BLACK)
|
|
|
|
rl.DrawTexturePro(
|
|
texture_atlas,
|
|
{SHIP_TO.x, SHIP_TO.y, SPRITE_CELL, SPRITE_CELL},
|
|
{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 && !alien.playing_death_animation {continue}
|
|
alien_to := glm.vec2{ALIENS_TO[alien.id].x, ALIENS_TO[alien.id].y}
|
|
if alien.playing_death_animation {
|
|
if alien.death_animation_index < ALIEN_DEATH_FRAMES {
|
|
alien_to = ALIEN_DEATH_ANIMATION_TO[alien.death_animation_index]
|
|
if frame_counter % (int(target_fps) / 4) == 0 {
|
|
alien.death_animation_index += 1
|
|
}
|
|
} else {
|
|
alien.playing_death_animation = false
|
|
continue
|
|
}
|
|
}
|
|
rl.DrawTexturePro(
|
|
texture_atlas,
|
|
{alien_to.x, alien_to.y, SPRITE_CELL, SPRITE_CELL},
|
|
{alien.position.x, alien.position.y, ALIEN_RECT.x, ALIEN_RECT.y},
|
|
{SPRITE_CELL, SPRITE_CELL},
|
|
0,
|
|
rl.WHITE,
|
|
)
|
|
}
|
|
|
|
for &bullet in bullets {
|
|
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.position.x, bullet.position.y, BULLET_RECT.x, BULLET_RECT.y},
|
|
{SPRITE_CELL, SPRITE_CELL},
|
|
0,
|
|
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 && !alien.playing_death_animation {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), 20, 0, 20, rl.WHITE)
|
|
for heart, hi in 0..= player_health {
|
|
rl.DrawTexturePro(
|
|
texture_atlas,
|
|
{HEART_TO.x, HEART_TO.y, SPRITE_CELL, SPRITE_CELL},
|
|
{
|
|
f32(screen_width) - f32(hi * SPRITE),
|
|
0,
|
|
f32(PLAYER_RECT.x),
|
|
f32(PLAYER_RECT.y),
|
|
},
|
|
{0,0},
|
|
0,
|
|
rl.WHITE,
|
|
)
|
|
}
|
|
|
|
// rl.DrawText("GAMEPLAY SCREEN", 20, 20, 40, rl.MAROON)
|
|
}
|