package space_invaders import "core:c" import "core:log" import glm "core:math/linalg/glsl" import rl "vendor:raylib" GLOBAL_SPRITE_SCALE :: 2 SPRITE_CELL :: 16 ALIEN_ROWS :: 5 ALIENS_PER_ROW :: 11 ALIENS :: ALIEN_ROWS * ALIENS_PER_ROW ALIEN_SIDE_STEP :: 20 * GLOBAL_SPRITE_SCALE ALIEN_RECT :: glm.vec2{SPRITE_CELL * GLOBAL_SPRITE_SCALE, SPRITE_CELL * GLOBAL_SPRITE_SCALE} MAX_BULLETS :: 100 BULLET_SPEED :: 240 BULLET_RECT :: glm.vec2{SPRITE_CELL * GLOBAL_SPRITE_SCALE, SPRITE_CELL * GLOBAL_SPRITE_SCALE} MAX_PLAYER_HEALTH :: 3 PLAYER_SPEED :: 120 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 := [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, 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 { alive: bool, position: glm.vec2, id: AlienKind, } Bullet :: struct { 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 { 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 * 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 { 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 = row % 2 == 0 ? .ORANGE : .GREEN } } if player_score > player_high_score { player_high_score = player_score } reset_game = false } update_game :: proc(state: ^GameState) { using state // 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) } } // 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 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]) } } } } } // 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 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 {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, ALIEN_RECT.x, ALIEN_RECT.y}, {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.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 {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) // rl.DrawText("GAMEPLAY SCREEN", 20, 20, 40, rl.MAROON) }