hour 4
This commit is contained in:
parent
d96f8c3396
commit
dbb9d0ac0f
1 changed files with 214 additions and 48 deletions
262
src/game.odin
262
src/game.odin
|
|
@ -25,20 +25,23 @@ COLUMN_COUNT :: 11
|
|||
ROUNDED_RECT_SEGMENTS :: 6
|
||||
GAP :: 16
|
||||
CARD_SEPARATION :: 24
|
||||
CARD_DAMPING :: 70
|
||||
|
||||
game: Game
|
||||
Game :: struct {
|
||||
run: bool,
|
||||
assets: map[string]runtime.Load_Directory_File,
|
||||
static_arena: mem.Arena,
|
||||
static_arena_data: [1 * mem.Megabyte]byte,
|
||||
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,
|
||||
state: State,
|
||||
card_font: ray.Font,
|
||||
suit_textures: [Suit]ray.Texture,
|
||||
static_allocator: runtime.Allocator,
|
||||
cards: [TAROT_COUNT]Card,
|
||||
entities: [TAROT_COUNT]Card_Entity,
|
||||
state: State,
|
||||
card_font: ray.Font,
|
||||
suit_textures: [Suit]ray.Texture,
|
||||
dragged_card: Maybe(Card_Handle),
|
||||
dragged_card_position: Vec2,
|
||||
}
|
||||
Vec2 :: [2]f32
|
||||
Card :: struct {
|
||||
|
|
@ -52,12 +55,18 @@ Suit :: enum {
|
|||
Sword = 3,
|
||||
Wand = 4,
|
||||
}
|
||||
Card_Handle :: distinct i8
|
||||
Card_Entity :: struct {
|
||||
position: Vec2,
|
||||
target_position: Vec2,
|
||||
velocity: Vec2,
|
||||
is_draggable: bool,
|
||||
state: Card_State_Kind,
|
||||
}
|
||||
Card_State_Kind :: enum {
|
||||
Idle,
|
||||
Dragged,
|
||||
}
|
||||
Card_Handle :: distinct i8
|
||||
State :: struct {
|
||||
columns: [COLUMN_COUNT][dynamic; 32]Card_Handle,
|
||||
major_low: [dynamic; MAJOR_COUNT]Card_Handle,
|
||||
|
|
@ -72,7 +81,7 @@ Move :: struct {
|
|||
}
|
||||
Place :: struct {
|
||||
kind: Place_Kind,
|
||||
index: i8,
|
||||
index: int,
|
||||
}
|
||||
Place_Kind :: enum {
|
||||
Column,
|
||||
|
|
@ -133,41 +142,109 @@ init :: proc() {
|
|||
)
|
||||
|
||||
state_init(&game.state)
|
||||
for &obj in game_cards_zipped() {
|
||||
obj.entity.position = card_position({.Column, 5})
|
||||
obj.entity.is_draggable = true
|
||||
}
|
||||
log.infof("%#v", game.state)
|
||||
}
|
||||
|
||||
update :: proc() {
|
||||
free_all(context.temp_allocator)
|
||||
delta := ray.GetFrameTime()
|
||||
ray.BeginDrawing(); defer ray.EndDrawing()
|
||||
ray.ClearBackground({30, 50, 40, 255})
|
||||
|
||||
major_low_offset := Vec2{GAP, GAP}
|
||||
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)
|
||||
mouse_position := ray.GetMousePosition()
|
||||
game.dragged_card_position = mouse_position - CARD_SIZE * 0.5
|
||||
if ray.IsMouseButtonPressed(.LEFT) {
|
||||
for &obj, index in game_cards_zipped() {
|
||||
handle := Card_Handle(index)
|
||||
if rect_contains(rrect_from_pos_sz(obj.entity.position, CARD_SIZE), mouse_position) {
|
||||
game.dragged_card = handle
|
||||
obj.entity.state = .Dragged
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
if game.dragged_card != nil {
|
||||
obj := &game_cards_zipped()[int(game.dragged_card.?)]
|
||||
if ray.IsMouseButtonReleased(.LEFT) {
|
||||
game.dragged_card = nil
|
||||
obj.entity.state = .Idle
|
||||
}
|
||||
}
|
||||
|
||||
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 {
|
||||
// NOTE(lilith): Draw slots and set card target positions
|
||||
slot_draw(card_position({kind = .Major_Low}))
|
||||
slot_draw(card_position({kind = .Major_High}))
|
||||
|
||||
for suit in Suit.Chalice ..= Suit.Wand {
|
||||
suit_index := int(suit) - 1
|
||||
suit_position := card_position({.Suit, suit_index})
|
||||
slot_draw(suit_position)
|
||||
|
||||
icon_size :: Vec2{40, 40}
|
||||
ray.DrawTexturePro(
|
||||
game.suit_textures[suit],
|
||||
source = rrect_from_pos_sz({0, 0}, SUIT_TEX_SIZE),
|
||||
dest = rrect_from_pos_sz(suit_position + CARD_SIZE * 0.5 - icon_size * 0.5, icon_size),
|
||||
origin = {0, 0},
|
||||
rotation = 0,
|
||||
tint = {255, 255, 255, 100},
|
||||
)
|
||||
for handle in game.state.suits[suit_index] {
|
||||
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)
|
||||
obj.entity.target_position = suit_position
|
||||
}
|
||||
}
|
||||
|
||||
for column, column_index in game.state.columns {
|
||||
slot_draw(card_position({.Column, column_index}))
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
// NOTE(lilith): Update card transforms
|
||||
for &obj in game_cards_zipped() {
|
||||
entity := &obj.entity
|
||||
target_position :=
|
||||
obj.entity.state != .Dragged ? entity.target_position : game.dragged_card_position
|
||||
entity.position, entity.velocity = critical_spring_vec2(
|
||||
entity.position,
|
||||
entity.velocity,
|
||||
target_position,
|
||||
dt = delta,
|
||||
)
|
||||
}
|
||||
|
||||
// NOTE(lilith): Draw cards
|
||||
for suit in Suit.Chalice ..= Suit.Wand {
|
||||
suit_index := int(suit) - 1
|
||||
for handle in game.state.suits[suit_index] {
|
||||
card_draw(handle)
|
||||
}
|
||||
}
|
||||
|
||||
if len(game.state.aside) > 0 {
|
||||
card_draw(game.state.aside[0])
|
||||
}
|
||||
|
||||
for column, column_index in game.state.columns {
|
||||
for handle, cc_index in column {
|
||||
card_draw(handle)
|
||||
}
|
||||
}
|
||||
|
||||
if handle, ok := game.dragged_card.?; ok {
|
||||
card_draw(handle)
|
||||
}
|
||||
}
|
||||
|
||||
draw_slot :: proc(position: Vec2) {
|
||||
slot_draw :: proc(position: Vec2) {
|
||||
ray.DrawRectangleRounded(
|
||||
rrect_from_pos_sz(position, CARD_SIZE),
|
||||
CARD_ROUNDNESS,
|
||||
|
|
@ -176,7 +253,7 @@ draw_slot :: proc(position: Vec2) {
|
|||
)
|
||||
}
|
||||
|
||||
draw_card :: proc(handle: Card_Handle) {
|
||||
card_draw :: proc(handle: Card_Handle) {
|
||||
obj := game_cards_zipped()[int(handle)]
|
||||
card := obj.card
|
||||
entity := obj.entity
|
||||
|
|
@ -306,7 +383,7 @@ state_is_move_valid :: proc(state: ^State, move: Move) -> (ok: bool) {
|
|||
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 int(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:
|
||||
|
|
@ -335,6 +412,31 @@ state_get_pile :: proc(state: ^State, place: Place) -> (pile: []Card_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) {
|
||||
global_offset := Vec2{GAP, GAP}
|
||||
top_section_offset := global_offset
|
||||
column_section_offset := global_offset + Vec2{0, CARD_SIZE.y + 2 * GAP}
|
||||
suits_section_offset := global_offset + Vec2{7 * (CARD_SIZE.x + GAP), 0}
|
||||
switch place.kind {
|
||||
case .Column:
|
||||
column_offset := Vec2{f32(place.index) * (CARD_SIZE.x + GAP), 0} + column_section_offset
|
||||
return Vec2{0, f32(pile_index) * CARD_SEPARATION} + column_offset
|
||||
case .Major_Low:
|
||||
return top_section_offset
|
||||
case .Major_High:
|
||||
return top_section_offset + Vec2{CARD_SIZE.x + GAP, 0}
|
||||
case .Suit:
|
||||
return suits_section_offset + Vec2{f32(place.index) * (CARD_SIZE.x + GAP), 0}
|
||||
case .Aside:
|
||||
return suits_section_offset + Vec2{1.5 * (CARD_SIZE.x + GAP), 0}
|
||||
}
|
||||
unreachable()
|
||||
}
|
||||
|
||||
card_is_draggable :: proc(handle: Card_Handle) -> bool {
|
||||
return game.entities[int(handle)].is_draggable
|
||||
}
|
||||
|
||||
index_from_card :: proc(card: Card) -> int {
|
||||
suit_offset: int
|
||||
switch card.suit {
|
||||
|
|
@ -370,7 +472,7 @@ text_color :: proc(card: Card) -> ray.Color {
|
|||
case .Coin:
|
||||
return ray.Color{12, 90, 10, 255}
|
||||
case .Sword:
|
||||
return ray.Color{90, 80, 50, 255}
|
||||
return ray.Color{100, 90, 0, 255}
|
||||
case .Wand:
|
||||
return ray.Color{80, 0, 0, 255}
|
||||
}
|
||||
|
|
@ -388,19 +490,20 @@ background_color :: proc(card: Card) -> ray.Color {
|
|||
}
|
||||
|
||||
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()
|
||||
return text_color(card)
|
||||
// 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 {
|
||||
|
|
@ -463,3 +566,66 @@ rrect_from_pos_sz :: proc(pos: Vec2, sz: Vec2) -> ray.Rectangle {
|
|||
psz_from_slice :: proc(slice: []$T) -> (rawptr, i32) {
|
||||
return raw_data(slice), i32(len(slice))
|
||||
}
|
||||
|
||||
import "core:math"
|
||||
|
||||
critical_spring_scalar :: proc(
|
||||
position: f32,
|
||||
velocity: f32,
|
||||
goal_position: f32,
|
||||
goal_velocity: f32,
|
||||
damping: f32,
|
||||
dt: f32,
|
||||
) -> (
|
||||
new_position: f32,
|
||||
new_velocity: f32,
|
||||
) {
|
||||
y := damping / 2
|
||||
c := goal_position + (damping * goal_velocity / (damping * damping / 4))
|
||||
o := position - c
|
||||
j1 := velocity + (o * y)
|
||||
exponential := math.exp(-y * dt)
|
||||
|
||||
new_position = (exponential * (o + (j1 * dt))) + c
|
||||
new_velocity = exponential * (velocity - (j1 * y * dt))
|
||||
return
|
||||
}
|
||||
|
||||
critical_spring_vec2 :: proc(
|
||||
position: Vec2,
|
||||
velocity: Vec2,
|
||||
goal_position: Vec2,
|
||||
goal_velocity: Vec2 = Vec2{},
|
||||
damping: f32 = CARD_DAMPING,
|
||||
dt: f32 = 1,
|
||||
) -> (
|
||||
new_position: Vec2,
|
||||
new_velocity: Vec2,
|
||||
) {
|
||||
new_position.x, new_velocity.x = critical_spring_scalar(
|
||||
position.x,
|
||||
velocity.x,
|
||||
goal_position.x,
|
||||
goal_velocity.x,
|
||||
damping,
|
||||
dt,
|
||||
)
|
||||
new_position.y, new_velocity.y = critical_spring_scalar(
|
||||
position.y,
|
||||
velocity.y,
|
||||
goal_position.y,
|
||||
goal_velocity.y,
|
||||
damping,
|
||||
dt,
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
rect_contains :: proc(rect: ray.Rectangle, p: Vec2) -> bool {
|
||||
return(
|
||||
rect.x <= p.x &&
|
||||
p.x <= rect.x + rect.width &&
|
||||
rect.y <= p.y &&
|
||||
p.y <= rect.y + rect.height \
|
||||
)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue