Arkadodge/game.odin

88 lines
1.4 KiB
Odin
Raw Normal View History

2024-09-04 22:43:27 +03:00
package main
import rl "vendor:raylib"
2024-09-05 23:42:18 +03:00
import "vendor:raylib/rlgl"
2024-09-05 22:39:52 +03:00
import "core:fmt"
2024-09-04 22:43:27 +03:00
2024-09-05 23:42:18 +03:00
// Virtual game field dimensions
GameField := Vec2{800, 600}
2024-09-04 22:43:27 +03:00
Pad :: struct {
position: Vec2,
2024-09-05 22:39:52 +03:00
size: Vec2,
2024-09-04 22:43:27 +03:00
}
Ball :: struct {
position: Vec2,
velocity: Vec2,
radius: f32,
}
Brick :: struct {
position: Vec2,
size: Vec2,
color: rl.Color,
hits: u8,
}
Game :: struct {
using state: GameState,
lives: u8,
pad: Pad,
balls: [dynamic]Ball,
bricks: [dynamic]Brick,
2024-09-05 23:42:18 +03:00
camera: rl.Camera2D
2024-09-04 22:43:27 +03:00
}
2024-09-05 22:39:52 +03:00
game_init :: proc() -> ^GameState {
state := new(Game)
state.variant = state
state.draw = game_draw
state.update = game_update
state.lives = 3
2024-09-05 23:42:18 +03:00
state.camera = rl.Camera2D{
zoom = 1,
target = GameField / 2,
}
2024-09-05 22:39:52 +03:00
state.pad = Pad{
2024-09-05 23:42:18 +03:00
position = Vec2{GameField.x / 2, GameField.y - 40},
2024-09-05 22:39:52 +03:00
size = {80, 10},
2024-09-04 22:43:27 +03:00
}
2024-09-05 22:39:52 +03:00
return state
2024-09-04 22:43:27 +03:00
}
2024-09-05 22:39:52 +03:00
game_update :: proc(state: ^GameState, delta: f32) {
game := transmute(^Game)state
using game
if rl.IsKeyDown(rl.KeyboardKey.LEFT) {
pad.position.x -= 100 * delta
fmt.println(pad.position)
}
if rl.IsKeyDown(rl.KeyboardKey.RIGHT) {
pad.position.x += 100 * delta
}
2024-09-05 23:42:18 +03:00
game.camera.offset = WINDOWF / 2
2024-09-04 22:43:27 +03:00
}
2024-09-05 22:39:52 +03:00
game_draw :: proc(state: ^GameState) {
game := transmute(^Game)state
using game
2024-09-05 23:42:18 +03:00
rl.BeginMode2D(camera)
2024-09-05 22:39:52 +03:00
rl.ClearBackground(rl.RAYWHITE)
2024-09-05 23:42:18 +03:00
rl.DrawRectangleV({0, 0}, GameField, rl.RED)
2024-09-05 22:39:52 +03:00
rl.DrawRectangleV(pad.position, pad.size, rl.GREEN)
2024-09-05 23:42:18 +03:00
rl.EndMode2D()
2024-09-04 22:43:27 +03:00
}