This commit is contained in:
Lilith Schier 2026-10-04 15:33:52 +02:00
parent 1ef7c8c0d8
commit d96f8c3396
9 changed files with 319 additions and 30 deletions

BIN
src/assets/Besley-Bold.ttf Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
src/assets/chalice.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 KiB

BIN
src/assets/coin.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.8 KiB

BIN
src/assets/images.kra Normal file

Binary file not shown.

BIN
src/assets/sword.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

BIN
src/assets/wand.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

View file

@ -8,9 +8,24 @@ import hm "core:container/handle_map"
import "core:container/queue" import "core:container/queue"
import "core:fmt" import "core:fmt"
import "core:log" import "core:log"
import "core:math/rand"
import "core:mem" import "core:mem"
import "core:slice"
import "core:strings"
import ray "vendor:raylib" import ray "vendor:raylib"
MAJOR_COUNT :: 22
MINOR_COUNT :: 14
TAROT_COUNT :: MAJOR_COUNT + 4 * MINOR_COUNT
CARD_PROPORTIONS :: 4.75 / 2.75
CARD_SIZE :: Vec2{100, 100 * CARD_PROPORTIONS}
CARD_ROUNDNESS :: 0.05
SUIT_TEX_SIZE :: Vec2{128, 128}
COLUMN_COUNT :: 11
ROUNDED_RECT_SEGMENTS :: 6
GAP :: 16
CARD_SEPARATION :: 24
game: Game game: Game
Game :: struct { Game :: struct {
run: bool, run: bool,
@ -21,7 +36,9 @@ Game :: struct {
static_allocator: runtime.Allocator, static_allocator: runtime.Allocator,
cards: [TAROT_COUNT]Card, cards: [TAROT_COUNT]Card,
entities: [TAROT_COUNT]Card_Entity, entities: [TAROT_COUNT]Card_Entity,
font: ray.Font, state: State,
card_font: ray.Font,
suit_textures: [Suit]ray.Texture,
} }
Vec2 :: [2]f32 Vec2 :: [2]f32
Card :: struct { Card :: struct {
@ -35,17 +52,42 @@ Suit :: enum {
Sword = 3, Sword = 3,
Wand = 4, Wand = 4,
} }
MAJOR_COUNT :: 22
MINOR_COUNT :: 14
TAROT_COUNT :: MAJOR_COUNT + 4 * MINOR_COUNT
Card_Entity :: struct { Card_Entity :: struct {
position: Vec2, position: Vec2,
target_position: Vec2,
velocity: Vec2,
}
Card_Handle :: distinct i8
State :: struct {
columns: [COLUMN_COUNT][dynamic; 32]Card_Handle,
major_low: [dynamic; MAJOR_COUNT]Card_Handle,
major_high: [dynamic; MAJOR_COUNT]Card_Handle,
suits: [4][dynamic; MINOR_COUNT]Card_Handle,
aside: [dynamic; 1]Card_Handle,
}
Move :: struct {
card: Card_Handle,
from: Place,
to: Place,
}
Place :: struct {
kind: Place_Kind,
index: i8,
}
Place_Kind :: enum {
Column,
Major_Low,
Major_High,
Suit,
Aside,
} }
Card_Handle :: distinct u8
init :: proc() { init :: proc() {
game.run = true game.run = true
ray.SetConfigFlags({.WINDOW_RESIZABLE, .VSYNC_HINT, .MSAA_4X_HINT})
ray.InitWindow(1600, 900, "Fortune's Foundation")
// NOTE(lilith): Sorting statically included assets // NOTE(lilith): Sorting statically included assets
assets := #load_directory("assets") assets := #load_directory("assets")
game.assets = make( game.assets = make(
@ -57,8 +99,14 @@ init :: proc() {
game.assets[asset.name] = asset game.assets[asset.name] = asset
} }
ray.SetConfigFlags({.WINDOW_RESIZABLE, .VSYNC_HINT}) suit_texture_files := []string{"chalice.png", "coin.png", "sword.png", "wand.png"}
ray.InitWindow(1280, 720, "Fortune's Foundation") for file_name, index in suit_texture_files {
suit := Suit(index + 1)
image := ray.LoadImageFromMemory(".png", psz_from_slice(game.assets[file_name].data))
game.suit_textures[suit] = ray.LoadTextureFromImage(image)
ray.GenTextureMipmaps(&game.suit_textures[suit])
ray.SetTextureFilter(game.suit_textures[suit], .TRILINEAR)
}
for number in 0 ..< MAJOR_COUNT { for number in 0 ..< MAJOR_COUNT {
game.cards[number] = Card { game.cards[number] = Card {
@ -76,38 +124,217 @@ init :: proc() {
} }
} }
for &obj, index in soa_zip(card = game.cards[:], entity = game.entities[:]) { game.card_font = ray.LoadFontFromMemory(
obj.entity.position.y = f32(index * 28)
log.infof("card: %#v", obj)
}
game.font = ray.LoadFontFromMemory(
".ttf", ".ttf",
psz_from_slice(game.assets["Besley-Regular.ttf"].data), psz_from_slice(game.assets["Besley-Bold.ttf"].data),
fontSize = 24, fontSize = 24,
codepoints = nil, codepoints = nil,
codepointCount = 0, codepointCount = 0,
) )
state_init(&game.state)
log.infof("%#v", game.state)
} }
update :: proc() { update :: proc() {
free_all(context.temp_allocator) free_all(context.temp_allocator)
ray.BeginDrawing(); defer ray.EndDrawing() ray.BeginDrawing(); defer ray.EndDrawing()
ray.ClearBackground({0, 120, 153, 255}) ray.ClearBackground({30, 50, 40, 255})
for obj, index in soa_zip(card = game.cards[:], entity = game.entities[:]) { major_low_offset := Vec2{GAP, GAP}
// ray.DrawRectangle draw_slot(major_low_offset)
major_high_offset := Vec2{1 * (CARD_SIZE.x + GAP) + GAP, GAP}
draw_slot(major_high_offset)
suits_section_offset := Vec2{7 * (CARD_SIZE.x + GAP) + GAP, GAP}
for suit in Suit.Chalice ..= Suit.Wand {
suit_index := int(suit) - 1
suit_offset := Vec2{f32(suit_index) * (CARD_SIZE.x + GAP), 0} + suits_section_offset
draw_slot(suit_offset)
}
column_section_offset := Vec2{0, CARD_SIZE.y + 2 * GAP}
for column, column_index in game.state.columns {
column_offset :=
Vec2{f32(column_index) * (CARD_SIZE.x + GAP) + GAP, 0} + column_section_offset
draw_slot(column_offset)
for handle, cc_index in column {
obj := &game_cards_zipped()[int(handle)]
obj.entity.target_position = Vec2{0, f32(cc_index) * CARD_SEPARATION} + column_offset
obj.entity.position = obj.entity.target_position
draw_card(handle)
}
}
}
draw_slot :: proc(position: Vec2) {
ray.DrawRectangleRounded(
rrect_from_pos_sz(position, CARD_SIZE),
CARD_ROUNDNESS,
ROUNDED_RECT_SEGMENTS,
color = ray.Color{0, 0, 0, 90},
)
}
draw_card :: proc(handle: Card_Handle) {
obj := game_cards_zipped()[int(handle)]
card := obj.card
entity := obj.entity
ray.DrawRectangleRounded(
rrect_from_pos_sz(entity.position, CARD_SIZE),
CARD_ROUNDNESS,
ROUNDED_RECT_SEGMENTS,
color = background_color(card),
)
ray.DrawRectangleRoundedLinesEx(
rrect_from_pos_sz(entity.position, CARD_SIZE),
CARD_ROUNDNESS,
ROUNDED_RECT_SEGMENTS,
lineThick = 2,
color = outline_color(card),
)
text := strings.clone_to_cstring(short_title(card), context.temp_allocator)
font_size := f32(24)
text_size := ray.MeasureTextEx(game.card_font, text, font_size, spacing = 0)
offset: Vec2
#partial switch card.suit {
case .Major:
offset = {(CARD_SIZE.x - text_size.x) * 0.5, 0}
case:
offset = {4, 0}
}
ray.DrawTextEx( ray.DrawTextEx(
game.font, game.card_font,
fmt.ctprintf("%d%s", obj.card.number, ""), text,
position = obj.entity.position, position = entity.position + offset,
fontSize = 24, fontSize = font_size,
spacing = 0, spacing = 0,
tint = ray.WHITE, tint = text_color(card),
)
if card.suit != .Major {
ray.DrawTexturePro(
game.suit_textures[card.suit],
source = rrect_from_pos_sz({0, 0}, SUIT_TEX_SIZE),
dest = rrect_from_pos_sz(
entity.position + offset + {text_size.x, 3},
text_size.yy * 0.7,
),
origin = {0, 0},
rotation = 0,
tint = text_color(card),
) )
} }
} }
state_init :: proc(state: ^State) {
deck := [TAROT_COUNT]Card_Handle{}
for &handle, index in deck {handle = Card_Handle(index)}
rand.shuffle(deck[:])
target_column: int
for handle, index in deck {
append(&state.columns[target_column], handle)
target_column += 1
if target_column == 5 {target_column += 1}
if target_column >= COLUMN_COUNT {target_column = 0}
}
}
state_update :: proc(state: ^State, move: Move) -> (effects: []Move, ok: bool) {
if !state_is_move_valid(state, move) {return nil, false}
moved_card, pop_ok := state_pop(state, move.from)
assert(pop_ok)
assert(moved_card == move.card)
state_push(state, move.to, moved_card)
effects = make([]Move, 1)
effects[0] = move
return effects, true
}
state_pop :: proc(state: ^State, place: Place) -> (handle: Card_Handle, ok: bool) {
switch place.kind {
case .Column:
return pop_safe(&state.columns[place.index])
case .Major_Low:
return pop_safe(&state.major_low)
case .Major_High:
return pop_safe(&state.major_high)
case .Suit:
return pop_safe(&state.suits[place.index])
case .Aside:
return pop_safe(&state.aside)
}
unreachable()
}
state_push :: proc(state: ^State, place: Place, handle: Card_Handle) {
switch place.kind {
case .Column:
append(&state.columns[place.index], handle)
case .Major_Low:
append(&state.major_low, handle)
case .Major_High:
append(&state.major_high, handle)
case .Suit:
append(&state.suits[place.index], handle)
case .Aside:
append(&state.aside, handle)
}
unreachable()
}
state_is_move_valid :: proc(state: ^State, move: Move) -> (ok: bool) {
if move.from == move.to {return false}
moved_card := card_from_handle(move.card)
from_pile := state_get_pile(state, move.from)
if len(from_pile) == 0 {return false}
if slice.last(from_pile) != move.card {return false}
to_pile := state_get_pile(state, move.to)
switch move.to.kind {
case .Column:
if len(to_pile) == 0 {return true}
top_card := card_from_handle(slice.last(to_pile))
return top_card.suit == moved_card.suit && abs(top_card.number - moved_card.number) == 1
case .Major_Low:
if moved_card.suit != .Major {return false}
if len(to_pile) == 0 {return moved_card.number == 0}
return card_from_handle(slice.last(to_pile)).number == moved_card.number - 1
case .Major_High:
if moved_card.suit != .Major {return false}
if len(to_pile) == 0 {return moved_card.number == MAJOR_COUNT - 1}
return card_from_handle(slice.last(to_pile)).number == moved_card.number + 1
case .Suit:
if len(state.aside) > 0 {return false}
if i8(moved_card.suit) - 1 != move.to.index {return false}
if len(to_pile) == 0 {return moved_card.number == 0}
return card_from_handle(slice.last(to_pile)).number == moved_card.number - 1
case .Aside:
return len(to_pile) == 0
}
unreachable()
}
state_get_pile :: proc(state: ^State, place: Place) -> (pile: []Card_Handle) {
switch place.kind {
case .Column:
assert(0 <= place.index && place.index < COLUMN_COUNT)
return state.columns[place.index][:]
case .Major_Low:
return state.major_low[:]
case .Major_High:
return state.major_high[:]
case .Suit:
assert(0 <= place.index && place.index < 4)
return state.suits[place.index][:]
case .Aside:
return state.aside[:]
}
unreachable()
}
card_from_handle :: proc(handle: Card_Handle) -> Card {return game.cards[int(handle)]}
index_from_card :: proc(card: Card) -> int { index_from_card :: proc(card: Card) -> int {
suit_offset: int suit_offset: int
switch card.suit { switch card.suit {
@ -126,12 +353,26 @@ index_from_card :: proc(card: Card) -> int {
return suit_offset + int(number_offset) return suit_offset + int(number_offset)
} }
game_cards_zipped :: proc() -> #soa[]struct {
card: Card,
entity: Card_Entity,
} {
return soa_zip(card = game.cards[:], entity = game.entities[:])
}
text_color :: proc(card: Card) -> ray.Color { text_color :: proc(card: Card) -> ray.Color {
switch card.suit { switch card.suit {
case .Major: case .Major:
return ray.WHITE // return ray.Color{250, 220, 120, 255}
case .Chalice, .Coin, .Sword, .Wand: return ray.Color{250, 230, 190, 255}
return ray.Color{220, 220, 220, 255} case .Chalice:
return ray.Color{20, 50, 70, 255}
case .Coin:
return ray.Color{12, 90, 10, 255}
case .Sword:
return ray.Color{90, 80, 50, 255}
case .Wand:
return ray.Color{80, 0, 0, 255}
} }
unreachable() unreachable()
} }
@ -139,13 +380,61 @@ text_color :: proc(card: Card) -> ray.Color {
background_color :: proc(card: Card) -> ray.Color { background_color :: proc(card: Card) -> ray.Color {
switch card.suit { switch card.suit {
case .Major: case .Major:
return ray.WHITE return ray.Color{0, 0, 0, 255}
case .Chalice, .Coin, .Sword, .Wand: case .Chalice, .Coin, .Sword, .Wand:
return ray.Color{220, 220, 220, 255} return ray.Color{250, 240, 235, 255}
} }
unreachable() unreachable()
} }
outline_color :: proc(card: Card) -> ray.Color {
switch card.suit {
case .Major:
return ray.Color{250, 230, 190, 255}
case .Chalice:
return ray.Color{20, 50, 70, 255}
case .Coin:
return ray.Color{12, 90, 10, 255}
case .Sword:
return ray.Color{90, 80, 50, 255}
case .Wand:
return ray.Color{80, 0, 0, 255}
}
unreachable()
}
short_title :: proc(card: Card) -> string {
suit_name: string
switch card.suit {
case .Major:
return fmt.tprintf("%d", card.number)
case .Chalice:
suit_name = "Chalices"
case .Coin:
suit_name = "Coins"
case .Sword:
suit_name = "Swords"
case .Wand:
suit_name = "Wands"
}
number_name: string
switch card.number {
case 1:
number_name = "A"
case 11:
number_name = "P"
case 12:
number_name = "J"
case 13:
number_name = "Q"
case 14:
number_name = "K"
case:
return fmt.tprintf("%d", card.number)
}
return number_name
}
// In a web build, this is called when browser changes size. Remove the // In a web build, this is called when browser changes size. Remove the
// `ray.SetWindowSize` call if you don't want a resizable game. // `ray.SetWindowSize` call if you don't want a resizable game.
parent_window_size_changed :: proc(w, h: int) { parent_window_size_changed :: proc(w, h: int) {