init, working screens from raylib screens example and basic bullets mechanics
This commit is contained in:
commit
178cf92f44
3 changed files with 301 additions and 0 deletions
117
game.odin
Normal file
117
game.odin
Normal file
|
|
@ -0,0 +1,117 @@
|
|||
package space_invaders
|
||||
|
||||
import "core:c"
|
||||
import "core:log"
|
||||
import glm "core:math/linalg/glsl"
|
||||
|
||||
import rl "vendor:raylib"
|
||||
|
||||
GLOBAL_SPRITE_SCALE :: 2
|
||||
|
||||
ALIEN_ROWS :: 5
|
||||
ALIENS_PER_ROW :: 11
|
||||
ALIENS :: ALIEN_ROWS * ALIENS_PER_ROW
|
||||
ALIEN_SIDE_STEP :: 20 * GLOBAL_SPRITE_SCALE
|
||||
ALIEN_RECT :: glm.ivec2{16 * GLOBAL_SPRITE_SCALE, 16 * GLOBAL_SPRITE_SCALE}
|
||||
|
||||
MAX_BULLETS :: 100
|
||||
BULLET_SPEED :: 240
|
||||
BULLET_RECT :: glm.ivec2{16 * GLOBAL_SPRITE_SCALE, 16 * GLOBAL_SPRITE_SCALE}
|
||||
|
||||
MAX_PLAYER_HEALTH :: 3
|
||||
PLAYER_SPEED :: 40
|
||||
PLAYER_RECT :: glm.ivec2{16 * GLOBAL_SPRITE_SCALE, 16 * GLOBAL_SPRITE_SCALE}
|
||||
|
||||
Alien :: struct {
|
||||
alive: bool,
|
||||
position: glm.vec2,
|
||||
id: int,
|
||||
}
|
||||
|
||||
Bullet :: struct {
|
||||
alive: bool,
|
||||
position: glm.vec2,
|
||||
}
|
||||
|
||||
setup_game :: proc(state: ^GameState) {
|
||||
using state
|
||||
player_pos = glm.vec2{f32(screen_width / 2), f32(screen_height - PLAYER_RECT.x)}
|
||||
|
||||
// setup the initial positions of the aliens
|
||||
for row in 0 ..< ALIEN_ROWS {
|
||||
for alien in 0 ..< ALIENS_PER_ROW {
|
||||
aliens[(row * ALIENS_PER_ROW) + alien].position = glm.vec2 {
|
||||
f32((alien + 1) * int(ALIEN_RECT.x + 10)),
|
||||
f32((row + 1) * int(ALIEN_RECT.x + 10)),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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 {
|
||||
setup_game(state)
|
||||
log.info("Done setting up game")
|
||||
}
|
||||
|
||||
// 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.alive = true
|
||||
bullet.position = player_pos
|
||||
bullet.position.y = bullet.position.y - f32((PLAYER_RECT.y / 2) + BULLET_RECT.y / 2)
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
// movement update
|
||||
{
|
||||
for &bullet in bullets {
|
||||
if bullet.alive {
|
||||
bullet.position.y = bullet.position.y - f32(BULLET_SPEED * delta_time)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
draw_game :: proc(state: ^GameState) {
|
||||
using state
|
||||
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 {
|
||||
for alien in 0 ..< ALIENS_PER_ROW {
|
||||
alien := &aliens[(row * ALIENS_PER_ROW) + alien]
|
||||
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)
|
||||
}
|
||||
|
||||
// rl.DrawText("GAMEPLAY SCREEN", 20, 20, 40, rl.MAROON)
|
||||
}
|
||||
83
main.odin
Normal file
83
main.odin
Normal file
|
|
@ -0,0 +1,83 @@
|
|||
package space_invaders
|
||||
|
||||
import "core:c"
|
||||
import "core:log"
|
||||
import glm "core:math/linalg/glsl"
|
||||
|
||||
import rl "vendor:raylib"
|
||||
|
||||
// orignal resolution of space invaders
|
||||
// 256 x 224 px
|
||||
|
||||
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,
|
||||
aliens: #soa[ALIENS]Alien,
|
||||
bullets: #soa[MAX_BULLETS]Bullet,
|
||||
bullet_index: int,
|
||||
player_pos: glm.vec2,
|
||||
player_health: c.int,
|
||||
player_score: c.int,
|
||||
}
|
||||
state: GameState
|
||||
|
||||
setup :: proc(state: ^GameState) {
|
||||
using state
|
||||
target_fps = 60
|
||||
screen_width = 800
|
||||
screen_height = 600
|
||||
title = "Space Invaders (raylib+odin-lang edition)"
|
||||
|
||||
current_frame_time = rl.GetTime()
|
||||
previous_screen = .LOGO
|
||||
screen = .GAMEPLAY
|
||||
|
||||
rl.SetTargetFPS(target_fps)
|
||||
}
|
||||
|
||||
update :: proc(state: ^GameState) {
|
||||
using state
|
||||
|
||||
frame_counter += 1
|
||||
|
||||
last_frame_time = current_frame_time
|
||||
current_frame_time = rl.GetTime()
|
||||
delta_time = current_frame_time - last_frame_time
|
||||
|
||||
update_screen(state)
|
||||
}
|
||||
|
||||
draw :: proc(state: ^GameState) {
|
||||
rl.BeginDrawing()
|
||||
rl.ClearBackground(rl.RAYWHITE)
|
||||
draw_screen(state)
|
||||
rl.EndDrawing()
|
||||
}
|
||||
|
||||
main :: proc() {
|
||||
context.logger = log.create_console_logger()
|
||||
|
||||
log.info(state.screen)
|
||||
|
||||
setup(&state)
|
||||
|
||||
rl.InitWindow(state.screen_width, state.screen_height, state.title)
|
||||
defer rl.CloseWindow()
|
||||
|
||||
for !rl.WindowShouldClose() {
|
||||
update(&state)
|
||||
draw(&state)
|
||||
}
|
||||
}
|
||||
101
screens.odin
Normal file
101
screens.odin
Normal file
|
|
@ -0,0 +1,101 @@
|
|||
package space_invaders
|
||||
|
||||
import "core:log"
|
||||
import rl "vendor:raylib"
|
||||
|
||||
GameScreen :: enum {
|
||||
LOGO,
|
||||
TITLE,
|
||||
GAMEPLAY,
|
||||
ENDING,
|
||||
}
|
||||
|
||||
|
||||
update_screen :: proc(state: ^GameState) {
|
||||
using state
|
||||
|
||||
switch screen {
|
||||
case .LOGO:
|
||||
{
|
||||
// Wait for 2 seconds (120 frames) before jumping to TITLE screen
|
||||
if (frame_counter > int(target_fps * 2)) {
|
||||
previous_screen = screen
|
||||
screen = .TITLE
|
||||
log.info("Updated screen enum", screen)
|
||||
}
|
||||
}
|
||||
case .TITLE:
|
||||
{
|
||||
// Press enter to change to GAMEPLAY screen
|
||||
if (rl.IsKeyPressed(rl.KeyboardKey.ENTER)) {
|
||||
previous_screen = screen
|
||||
screen = .GAMEPLAY
|
||||
log.info("Updated screen enum", screen)
|
||||
}
|
||||
}
|
||||
case .GAMEPLAY:
|
||||
{
|
||||
update_game(state)
|
||||
}
|
||||
case .ENDING:
|
||||
{
|
||||
// Press enter to return to TITLE screen
|
||||
if (rl.IsKeyPressed(rl.KeyboardKey.ENTER)) {
|
||||
previous_screen = screen
|
||||
screen = .TITLE
|
||||
log.info("Updated screen enum", screen)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if last_frame_screen != screen {
|
||||
log.infof("Current screen: %v, previous screen: %v", screen, previous_screen)
|
||||
}
|
||||
last_frame_screen = screen
|
||||
}
|
||||
|
||||
draw_screen :: proc(state: ^GameState) {
|
||||
{
|
||||
switch state.screen {
|
||||
case .LOGO:
|
||||
{
|
||||
// TODO: Draw LOGO screen here!
|
||||
rl.DrawText("SPACE INVADERS", 220, 220, 40, rl.GREEN)
|
||||
rl.DrawText("WAIT for 2 SECONDS...", 290, 400, 20, rl.GRAY)
|
||||
|
||||
}
|
||||
case .TITLE:
|
||||
{
|
||||
// TODO: Draw TITLE screen here!
|
||||
rl.DrawRectangle(0, 0, state.screen_width, state.screen_height, rl.GREEN)
|
||||
rl.DrawText("TITLE SCREEN", 20, 20, 40, rl.DARKGREEN)
|
||||
rl.DrawText(
|
||||
"PRESS ENTER or TAP to JUMP to GAMEPLAY SCREEN",
|
||||
120,
|
||||
220,
|
||||
20,
|
||||
rl.DARKGREEN,
|
||||
)
|
||||
|
||||
}
|
||||
case .GAMEPLAY:
|
||||
{
|
||||
draw_game(state)
|
||||
}
|
||||
case .ENDING:
|
||||
{
|
||||
// TODO: Draw ENDING screen here!
|
||||
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,
|
||||
)
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue