hour 5
This commit is contained in:
parent
dbb9d0ac0f
commit
064a29b8d6
1 changed files with 149 additions and 23 deletions
172
src/game.odin
172
src/game.odin
|
|
@ -6,6 +6,7 @@ import "base:runtime"
|
||||||
import "core:c"
|
import "core:c"
|
||||||
import hm "core:container/handle_map"
|
import hm "core:container/handle_map"
|
||||||
import "core:container/queue"
|
import "core:container/queue"
|
||||||
|
import "core:container/topological_sort"
|
||||||
import "core:fmt"
|
import "core:fmt"
|
||||||
import "core:log"
|
import "core:log"
|
||||||
import "core:math/rand"
|
import "core:math/rand"
|
||||||
|
|
@ -26,6 +27,7 @@ ROUNDED_RECT_SEGMENTS :: 6
|
||||||
GAP :: 16
|
GAP :: 16
|
||||||
CARD_SEPARATION :: 24
|
CARD_SEPARATION :: 24
|
||||||
CARD_DAMPING :: 70
|
CARD_DAMPING :: 70
|
||||||
|
AUTO_MOVE_INTERVAL :: 0.2
|
||||||
|
|
||||||
game: Game
|
game: Game
|
||||||
Game :: struct {
|
Game :: struct {
|
||||||
|
|
@ -38,10 +40,13 @@ Game :: struct {
|
||||||
cards: [TAROT_COUNT]Card,
|
cards: [TAROT_COUNT]Card,
|
||||||
entities: [TAROT_COUNT]Card_Entity,
|
entities: [TAROT_COUNT]Card_Entity,
|
||||||
state: State,
|
state: State,
|
||||||
|
displayed_state: State,
|
||||||
card_font: ray.Font,
|
card_font: ray.Font,
|
||||||
suit_textures: [Suit]ray.Texture,
|
suit_textures: [Suit]ray.Texture,
|
||||||
dragged_card: Maybe(Card_Handle),
|
dragged_card: Maybe(Card_Handle),
|
||||||
dragged_card_position: Vec2,
|
dragged_card_position: Vec2,
|
||||||
|
auto_moves: queue.Queue(Move),
|
||||||
|
auto_move_cooldown: f32,
|
||||||
}
|
}
|
||||||
Vec2 :: [2]f32
|
Vec2 :: [2]f32
|
||||||
Card :: struct {
|
Card :: struct {
|
||||||
|
|
@ -66,6 +71,7 @@ Card_Entity :: struct {
|
||||||
Card_State_Kind :: enum {
|
Card_State_Kind :: enum {
|
||||||
Idle,
|
Idle,
|
||||||
Dragged,
|
Dragged,
|
||||||
|
Locked,
|
||||||
}
|
}
|
||||||
State :: struct {
|
State :: struct {
|
||||||
columns: [COLUMN_COUNT][dynamic; 32]Card_Handle,
|
columns: [COLUMN_COUNT][dynamic; 32]Card_Handle,
|
||||||
|
|
@ -108,6 +114,8 @@ init :: proc() {
|
||||||
game.assets[asset.name] = asset
|
game.assets[asset.name] = asset
|
||||||
}
|
}
|
||||||
|
|
||||||
|
queue.init(&game.auto_moves, 128, game.static_allocator)
|
||||||
|
|
||||||
suit_texture_files := []string{"chalice.png", "coin.png", "sword.png", "wand.png"}
|
suit_texture_files := []string{"chalice.png", "coin.png", "sword.png", "wand.png"}
|
||||||
for file_name, index in suit_texture_files {
|
for file_name, index in suit_texture_files {
|
||||||
suit := Suit(index + 1)
|
suit := Suit(index + 1)
|
||||||
|
|
@ -146,6 +154,7 @@ init :: proc() {
|
||||||
obj.entity.position = card_position({.Column, 5})
|
obj.entity.position = card_position({.Column, 5})
|
||||||
obj.entity.is_draggable = true
|
obj.entity.is_draggable = true
|
||||||
}
|
}
|
||||||
|
game.displayed_state = game.state
|
||||||
log.infof("%#v", game.state)
|
log.infof("%#v", game.state)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -155,29 +164,74 @@ update :: proc() {
|
||||||
ray.BeginDrawing(); defer ray.EndDrawing()
|
ray.BeginDrawing(); defer ray.EndDrawing()
|
||||||
ray.ClearBackground({30, 50, 40, 255})
|
ray.ClearBackground({30, 50, 40, 255})
|
||||||
|
|
||||||
|
for &entity in game.entities {entity.is_draggable = false}
|
||||||
|
for &column in game.state.columns {
|
||||||
|
if len(column) > 0 {
|
||||||
|
game.entities[int(slice.last(column[:]))].is_draggable = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if len(game.state.aside) > 0 {
|
||||||
|
game.entities[game.state.aside[0]].is_draggable = true
|
||||||
|
}
|
||||||
|
|
||||||
mouse_position := ray.GetMousePosition()
|
mouse_position := ray.GetMousePosition()
|
||||||
game.dragged_card_position = mouse_position - CARD_SIZE * 0.5
|
game.dragged_card_position = mouse_position - CARD_SIZE * 0.5
|
||||||
if ray.IsMouseButtonPressed(.LEFT) {
|
if ray.IsMouseButtonPressed(.LEFT) {
|
||||||
for &obj, index in game_cards_zipped() {
|
for &obj, index in game_cards_zipped() {
|
||||||
handle := Card_Handle(index)
|
handle := Card_Handle(index)
|
||||||
if rect_contains(rrect_from_pos_sz(obj.entity.position, CARD_SIZE), mouse_position) {
|
if rect_contains(rrect_from_pos_sz(obj.entity.position, CARD_SIZE), mouse_position) &&
|
||||||
|
game.entities[int(handle)].is_draggable {
|
||||||
game.dragged_card = handle
|
game.dragged_card = handle
|
||||||
obj.entity.state = .Dragged
|
obj.entity.state = .Dragged
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if game.dragged_card != nil {
|
if dragged_card, ok := game.dragged_card.?; ok {
|
||||||
obj := &game_cards_zipped()[int(game.dragged_card.?)]
|
obj := &game_cards_zipped()[int(dragged_card)]
|
||||||
if ray.IsMouseButtonReleased(.LEFT) {
|
if ray.IsMouseButtonReleased(.LEFT) {
|
||||||
game.dragged_card = nil
|
old_place := state_card_place(&game.state, dragged_card)
|
||||||
|
new_place := place_from_position(mouse_position)
|
||||||
|
move := Move {
|
||||||
|
from = old_place,
|
||||||
|
to = new_place,
|
||||||
|
card = dragged_card,
|
||||||
|
}
|
||||||
|
effects, ok := state_perform_move(&game.state, move)
|
||||||
obj.entity.state = .Idle
|
obj.entity.state = .Idle
|
||||||
|
game.dragged_card = nil
|
||||||
|
for auto_move in effects {
|
||||||
|
queue.push(&game.auto_moves, auto_move)
|
||||||
|
auto_obj := &game_cards_zipped()[int(auto_move.card)]
|
||||||
|
auto_obj.entity.state = .Locked
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
game.auto_move_cooldown -= ray.GetFrameTime()
|
||||||
|
if game.auto_move_cooldown <= 0 {
|
||||||
|
if auto_move, ok := queue.pop_front_safe(&game.auto_moves); ok {
|
||||||
|
game.auto_move_cooldown = AUTO_MOVE_INTERVAL
|
||||||
|
state_apply_single_move(&game.displayed_state, auto_move)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
game.auto_move_cooldown = max(0, game.auto_move_cooldown)
|
||||||
|
if queue.len(game.auto_moves) == 0 {
|
||||||
|
for &entity in game.entities {
|
||||||
|
if entity.state == .Locked {entity.state = .Idle}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// NOTE(lilith): Draw slots and set card target positions
|
// NOTE(lilith): Draw slots and set card target positions
|
||||||
slot_draw(card_position({kind = .Major_Low}))
|
slot_draw(card_position({.Major_Low, 0}))
|
||||||
slot_draw(card_position({kind = .Major_High}))
|
for handle in game.displayed_state.major_low {
|
||||||
|
game.entities[int(handle)].target_position = card_position({.Major_Low, 0})
|
||||||
|
}
|
||||||
|
slot_draw(card_position({.Major_High, 0}))
|
||||||
|
for handle in game.displayed_state.major_high {
|
||||||
|
game.entities[int(handle)].target_position = card_position({.Major_High, 0})
|
||||||
|
}
|
||||||
|
|
||||||
for suit in Suit.Chalice ..= Suit.Wand {
|
for suit in Suit.Chalice ..= Suit.Wand {
|
||||||
suit_index := int(suit) - 1
|
suit_index := int(suit) - 1
|
||||||
|
|
@ -193,18 +247,16 @@ update :: proc() {
|
||||||
rotation = 0,
|
rotation = 0,
|
||||||
tint = {255, 255, 255, 100},
|
tint = {255, 255, 255, 100},
|
||||||
)
|
)
|
||||||
for handle in game.state.suits[suit_index] {
|
for handle in game.displayed_state.suits[suit_index] {
|
||||||
obj := &game_cards_zipped()[int(handle)]
|
game.entities[int(handle)].target_position = suit_position
|
||||||
obj.entity.target_position = suit_position
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for column, column_index in game.state.columns {
|
for column, column_index in game.displayed_state.columns {
|
||||||
slot_draw(card_position({.Column, column_index}))
|
slot_draw(card_position({.Column, column_index}))
|
||||||
for handle, cc_index in column {
|
for handle, cc_index in column {
|
||||||
position := card_position({.Column, column_index}, cc_index)
|
position := card_position({.Column, column_index}, cc_index)
|
||||||
obj := &game_cards_zipped()[int(handle)]
|
game.entities[int(handle)].target_position = position
|
||||||
obj.entity.target_position = position
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -222,18 +274,24 @@ update :: proc() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// NOTE(lilith): Draw cards
|
// NOTE(lilith): Draw cards
|
||||||
|
for handle in game.displayed_state.major_low {
|
||||||
|
card_draw(handle)
|
||||||
|
}
|
||||||
|
for handle in game.displayed_state.major_high {
|
||||||
|
card_draw(handle)
|
||||||
|
}
|
||||||
for suit in Suit.Chalice ..= Suit.Wand {
|
for suit in Suit.Chalice ..= Suit.Wand {
|
||||||
suit_index := int(suit) - 1
|
suit_index := int(suit) - 1
|
||||||
for handle in game.state.suits[suit_index] {
|
for handle in game.displayed_state.suits[suit_index] {
|
||||||
card_draw(handle)
|
card_draw(handle)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(game.state.aside) > 0 {
|
if len(game.displayed_state.aside) > 0 {
|
||||||
card_draw(game.state.aside[0])
|
card_draw(game.displayed_state.aside[0])
|
||||||
}
|
}
|
||||||
|
|
||||||
for column, column_index in game.state.columns {
|
for column, column_index in game.displayed_state.columns {
|
||||||
for handle, cc_index in column {
|
for handle, cc_index in column {
|
||||||
card_draw(handle)
|
card_draw(handle)
|
||||||
}
|
}
|
||||||
|
|
@ -315,17 +373,57 @@ state_init :: proc(state: ^State) {
|
||||||
if target_column == 5 {target_column += 1}
|
if target_column == 5 {target_column += 1}
|
||||||
if target_column >= COLUMN_COUNT {target_column = 0}
|
if target_column >= COLUMN_COUNT {target_column = 0}
|
||||||
}
|
}
|
||||||
|
for column, column_index in game.state.columns {
|
||||||
|
for handle, cc_index in column {
|
||||||
|
position := card_position({.Column, column_index}, cc_index)
|
||||||
|
obj := &game_cards_zipped()[int(handle)]
|
||||||
|
obj.entity.target_position = position
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
state_update :: proc(state: ^State, move: Move) -> (effects: []Move, ok: bool) {
|
state_perform_move :: proc(
|
||||||
if !state_is_move_valid(state, move) {return nil, false}
|
state: ^State,
|
||||||
|
move: Move,
|
||||||
|
allocator := context.allocator,
|
||||||
|
) -> (
|
||||||
|
auto_moves: [dynamic]Move,
|
||||||
|
ok: bool,
|
||||||
|
) {
|
||||||
|
if !state_apply_single_move(state, move) {return nil, false}
|
||||||
|
auto_moves = make([dynamic]Move, allocator = allocator)
|
||||||
|
append(&auto_moves, move)
|
||||||
|
|
||||||
|
if move.from.kind == .Column && move.to.kind == .Column {
|
||||||
|
from_col := &state.columns[move.from.index]
|
||||||
|
last_moved_handle := move.card
|
||||||
|
last_moved_card := card_from_handle(move.card)
|
||||||
|
for len(from_col) > 0 {
|
||||||
|
next_handle := slice.last(from_col[:])
|
||||||
|
next_card := card_from_handle(next_handle)
|
||||||
|
if next_card.suit == last_moved_card.suit &&
|
||||||
|
abs(next_card.number - last_moved_card.number) == 1 {
|
||||||
|
next_move := move
|
||||||
|
next_move.card = next_handle
|
||||||
|
state_apply_single_move(state, next_move)
|
||||||
|
append(&auto_moves, next_move)
|
||||||
|
} else {break}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
state_get_auto_moves(state, &auto_moves)
|
||||||
|
return auto_moves, true
|
||||||
|
}
|
||||||
|
|
||||||
|
state_get_auto_moves :: proc(state: ^State, auto_moves: ^[dynamic]Move) {
|
||||||
|
}
|
||||||
|
|
||||||
|
state_apply_single_move :: proc(state: ^State, move: Move) -> (ok: bool) {
|
||||||
|
if !state_is_move_valid(state, move) {return false}
|
||||||
moved_card, pop_ok := state_pop(state, move.from)
|
moved_card, pop_ok := state_pop(state, move.from)
|
||||||
assert(pop_ok)
|
assert(pop_ok)
|
||||||
assert(moved_card == move.card)
|
assert(moved_card == move.card)
|
||||||
state_push(state, move.to, moved_card)
|
state_push(state, move.to, moved_card)
|
||||||
effects = make([]Move, 1)
|
return true
|
||||||
effects[0] = move
|
|
||||||
return effects, true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
state_pop :: proc(state: ^State, place: Place) -> (handle: Card_Handle, ok: bool) {
|
state_pop :: proc(state: ^State, place: Place) -> (handle: Card_Handle, ok: bool) {
|
||||||
|
|
@ -357,7 +455,6 @@ state_push :: proc(state: ^State, place: Place, handle: Card_Handle) {
|
||||||
case .Aside:
|
case .Aside:
|
||||||
append(&state.aside, handle)
|
append(&state.aside, handle)
|
||||||
}
|
}
|
||||||
unreachable()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
state_is_move_valid :: proc(state: ^State, move: Move) -> (ok: bool) {
|
state_is_move_valid :: proc(state: ^State, move: Move) -> (ok: bool) {
|
||||||
|
|
@ -384,7 +481,7 @@ state_is_move_valid :: proc(state: ^State, move: Move) -> (ok: bool) {
|
||||||
case .Suit:
|
case .Suit:
|
||||||
if len(state.aside) > 0 {return false}
|
if len(state.aside) > 0 {return false}
|
||||||
if int(moved_card.suit) - 1 != move.to.index {return false}
|
if int(moved_card.suit) - 1 != move.to.index {return false}
|
||||||
if len(to_pile) == 0 {return moved_card.number == 0}
|
if len(to_pile) == 0 {return moved_card.number == 1}
|
||||||
return card_from_handle(slice.last(to_pile)).number == moved_card.number - 1
|
return card_from_handle(slice.last(to_pile)).number == moved_card.number - 1
|
||||||
case .Aside:
|
case .Aside:
|
||||||
return len(to_pile) == 0
|
return len(to_pile) == 0
|
||||||
|
|
@ -410,6 +507,19 @@ state_get_pile :: proc(state: ^State, place: Place) -> (pile: []Card_Handle) {
|
||||||
unreachable()
|
unreachable()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
state_card_place :: proc(state: ^State, handle: Card_Handle) -> Place {
|
||||||
|
for column_index in 0 ..< COLUMN_COUNT {
|
||||||
|
if slice.contains(state.columns[column_index][:], handle) {return {.Column, column_index}}
|
||||||
|
}
|
||||||
|
if slice.contains(state.major_low[:], handle) {return {.Major_Low, 0}}
|
||||||
|
if slice.contains(state.major_high[:], handle) {return {.Major_High, 0}}
|
||||||
|
for suit in 0 ..< 4 {
|
||||||
|
if slice.contains(state.suits[suit][:], handle) {return {.Suit, suit}}
|
||||||
|
}
|
||||||
|
if slice.contains(state.aside[:], handle) {return {.Aside, 0}}
|
||||||
|
unreachable()
|
||||||
|
}
|
||||||
|
|
||||||
card_from_handle :: proc(handle: Card_Handle) -> Card {return game.cards[int(handle)]}
|
card_from_handle :: proc(handle: Card_Handle) -> Card {return game.cards[int(handle)]}
|
||||||
|
|
||||||
card_position :: proc(place: Place, pile_index: int = 0) -> (position: Vec2) {
|
card_position :: proc(place: Place, pile_index: int = 0) -> (position: Vec2) {
|
||||||
|
|
@ -433,6 +543,22 @@ card_position :: proc(place: Place, pile_index: int = 0) -> (position: Vec2) {
|
||||||
unreachable()
|
unreachable()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
place_from_position :: proc(p: Vec2) -> (place: Place) {
|
||||||
|
for column_index in 0 ..< COLUMN_COUNT {
|
||||||
|
place := Place{.Column, column_index}
|
||||||
|
pos := card_position(place)
|
||||||
|
rect := rrect_from_pos_sz(pos, {CARD_SIZE.x, 10000})
|
||||||
|
if rect_contains(rect, p) {return place}
|
||||||
|
}
|
||||||
|
for place in ([]Place{{.Major_Low, 0}, {.Major_High, 0}, {.Suit, 0}, {.Suit, 1}, {.Suit, 2}, {.Suit, 3}}) {
|
||||||
|
pos := card_position(place)
|
||||||
|
rect := rrect_from_pos_sz(pos, CARD_SIZE)
|
||||||
|
if rect_contains(rect, p) {return place}
|
||||||
|
}
|
||||||
|
|
||||||
|
return {}
|
||||||
|
}
|
||||||
|
|
||||||
card_is_draggable :: proc(handle: Card_Handle) -> bool {
|
card_is_draggable :: proc(handle: Card_Handle) -> bool {
|
||||||
return game.entities[int(handle)].is_draggable
|
return game.entities[int(handle)].is_draggable
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue