hour 1
This commit is contained in:
commit
1ef7c8c0d8
17 changed files with 1047 additions and 0 deletions
176
src/game.odin
Normal file
176
src/game.odin
Normal file
|
|
@ -0,0 +1,176 @@
|
|||
package game
|
||||
|
||||
import "base:builtin"
|
||||
import "base:intrinsics"
|
||||
import "base:runtime"
|
||||
import "core:c"
|
||||
import hm "core:container/handle_map"
|
||||
import "core:container/queue"
|
||||
import "core:fmt"
|
||||
import "core:log"
|
||||
import "core:mem"
|
||||
import ray "vendor:raylib"
|
||||
|
||||
game: Game
|
||||
Game :: struct {
|
||||
run: bool,
|
||||
assets: map[string]runtime.Load_Directory_File,
|
||||
static_arena: mem.Arena,
|
||||
static_arena_data: [1 * mem.Megabyte]byte,
|
||||
// Allocations here will never be freed
|
||||
static_allocator: runtime.Allocator,
|
||||
cards: [TAROT_COUNT]Card,
|
||||
entities: [TAROT_COUNT]Card_Entity,
|
||||
font: ray.Font,
|
||||
}
|
||||
Vec2 :: [2]f32
|
||||
Card :: struct {
|
||||
suit: Suit,
|
||||
number: i8,
|
||||
}
|
||||
Suit :: enum {
|
||||
Major = 0,
|
||||
Chalice = 1,
|
||||
Coin = 2,
|
||||
Sword = 3,
|
||||
Wand = 4,
|
||||
}
|
||||
MAJOR_COUNT :: 22
|
||||
MINOR_COUNT :: 14
|
||||
TAROT_COUNT :: MAJOR_COUNT + 4 * MINOR_COUNT
|
||||
Card_Entity :: struct {
|
||||
position: Vec2,
|
||||
}
|
||||
Card_Handle :: distinct u8
|
||||
|
||||
init :: proc() {
|
||||
game.run = true
|
||||
|
||||
// NOTE(lilith): Sorting statically included assets
|
||||
assets := #load_directory("assets")
|
||||
game.assets = make(
|
||||
map[string]runtime.Load_Directory_File,
|
||||
len(assets),
|
||||
allocator = game.static_allocator,
|
||||
)
|
||||
for asset in assets {
|
||||
game.assets[asset.name] = asset
|
||||
}
|
||||
|
||||
ray.SetConfigFlags({.WINDOW_RESIZABLE, .VSYNC_HINT})
|
||||
ray.InitWindow(1280, 720, "Fortune's Foundation")
|
||||
|
||||
for number in 0 ..< MAJOR_COUNT {
|
||||
game.cards[number] = Card {
|
||||
suit = .Major,
|
||||
number = i8(number),
|
||||
}
|
||||
}
|
||||
for suit in Suit.Chalice ..= Suit.Wand {
|
||||
suit_offset := MAJOR_COUNT + (int(suit) - 1) * MINOR_COUNT
|
||||
for number in 1 ..= MINOR_COUNT {
|
||||
game.cards[suit_offset + number - 1] = Card {
|
||||
suit = suit,
|
||||
number = i8(number),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for &obj, index in soa_zip(card = game.cards[:], entity = game.entities[:]) {
|
||||
obj.entity.position.y = f32(index * 28)
|
||||
log.infof("card: %#v", obj)
|
||||
}
|
||||
|
||||
game.font = ray.LoadFontFromMemory(
|
||||
".ttf",
|
||||
psz_from_slice(game.assets["Besley-Regular.ttf"].data),
|
||||
fontSize = 24,
|
||||
codepoints = nil,
|
||||
codepointCount = 0,
|
||||
)
|
||||
}
|
||||
|
||||
update :: proc() {
|
||||
free_all(context.temp_allocator)
|
||||
ray.BeginDrawing(); defer ray.EndDrawing()
|
||||
ray.ClearBackground({0, 120, 153, 255})
|
||||
|
||||
for obj, index in soa_zip(card = game.cards[:], entity = game.entities[:]) {
|
||||
// ray.DrawRectangle
|
||||
ray.DrawTextEx(
|
||||
game.font,
|
||||
fmt.ctprintf("%d%s", obj.card.number, ""),
|
||||
position = obj.entity.position,
|
||||
fontSize = 24,
|
||||
spacing = 0,
|
||||
tint = ray.WHITE,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
index_from_card :: proc(card: Card) -> int {
|
||||
suit_offset: int
|
||||
switch card.suit {
|
||||
case .Major:
|
||||
suit_offset = 0
|
||||
case .Chalice:
|
||||
suit_offset = MAJOR_COUNT
|
||||
case .Coin:
|
||||
suit_offset = MAJOR_COUNT + 1 * MINOR_COUNT
|
||||
case .Sword:
|
||||
suit_offset = MAJOR_COUNT + 2 * MINOR_COUNT
|
||||
case .Wand:
|
||||
suit_offset = MAJOR_COUNT + 3 * MINOR_COUNT
|
||||
}
|
||||
number_offset := card.suit == .Major ? card.number : card.number - 1
|
||||
return suit_offset + int(number_offset)
|
||||
}
|
||||
|
||||
text_color :: proc(card: Card) -> ray.Color {
|
||||
switch card.suit {
|
||||
case .Major:
|
||||
return ray.WHITE
|
||||
case .Chalice, .Coin, .Sword, .Wand:
|
||||
return ray.Color{220, 220, 220, 255}
|
||||
}
|
||||
unreachable()
|
||||
}
|
||||
|
||||
background_color :: proc(card: Card) -> ray.Color {
|
||||
switch card.suit {
|
||||
case .Major:
|
||||
return ray.WHITE
|
||||
case .Chalice, .Coin, .Sword, .Wand:
|
||||
return ray.Color{220, 220, 220, 255}
|
||||
}
|
||||
unreachable()
|
||||
}
|
||||
|
||||
// In a web build, this is called when browser changes size. Remove the
|
||||
// `ray.SetWindowSize` call if you don't want a resizable game.
|
||||
parent_window_size_changed :: proc(w, h: int) {
|
||||
ray.SetWindowSize(c.int(w), c.int(h))
|
||||
}
|
||||
|
||||
shutdown :: proc() {
|
||||
ray.CloseWindow()
|
||||
}
|
||||
|
||||
should_run :: proc() -> bool {
|
||||
when ODIN_OS != .JS {
|
||||
// Never run this proc in browser. It contains a 16 ms sleep on web!
|
||||
if ray.WindowShouldClose() {
|
||||
game.run = false
|
||||
}
|
||||
}
|
||||
|
||||
return game.run
|
||||
}
|
||||
|
||||
rrect_from_pos_sz :: proc(pos: Vec2, sz: Vec2) -> ray.Rectangle {
|
||||
return {pos.x, pos.y, sz.x, sz.y}
|
||||
}
|
||||
|
||||
psz_from_slice :: proc(slice: []$T) -> (rawptr, i32) {
|
||||
return raw_data(slice), i32(len(slice))
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue