Files
HanjaKanaMun/src/card/card.odin
T
2026-10-01 20:40:13 +02:00

82 lines
3.3 KiB
Odin

package card
import raylib "vendor:raylib"
import themes "../themes"
import assets "../assets"
Card :: struct {
x: i32,
y: i32,
character: cstring,
is_face_up: bool,
is_matched: bool,
pair_id: i32,
flip_progress: f32, // 0 = face down, 1 = face up
is_flipping: bool,
flip_direction: bool, // true = to face up, false = to face down
}
draw_card :: proc(card: ^Card, mouse_x, mouse_y: i32, theme_color: raylib.Color) {
draw_color := theme_color
if card.is_matched {
draw_color = themes.LIGHT_GRAY
}
is_hovered := mouse_x >= card.x && mouse_x <= card.x + themes.CARD_WIDTH &&
mouse_y >= card.y && mouse_y <= card.y + themes.CARD_HEIGHT
if is_hovered && !card.is_face_up && !card.is_matched && !card.is_flipping {
draw_color = raylib.Color{
byte(f32(theme_color.r) * 0.8),
byte(f32(theme_color.g) * 0.8),
byte(f32(theme_color.b) * 0.8),
theme_color.a,
}
}
// Draw card background with rounded corners
raylib.DrawRectangleRounded(raylib.Rectangle{f32(card.x), f32(card.y), f32(themes.CARD_WIDTH), f32(themes.CARD_HEIGHT)}, 0.2, 10, draw_color)
raylib.DrawRectangleRoundedLines(raylib.Rectangle{f32(card.x), f32(card.y), f32(themes.CARD_WIDTH), f32(themes.CARD_HEIGHT)}, 0.2, 10, themes.BLACK)
// Handle flip animation
if card.is_flipping {
// Draw the flip animation
flip_angle := card.flip_progress * 180.0
if !card.flip_direction {
flip_angle = 180.0 - flip_angle
}
// Draw the card as a rectangle that's rotating
center_x := f32(card.x + themes.CARD_WIDTH / 2)
center_y := f32(card.y + themes.CARD_HEIGHT / 2)
// For simplicity, just show the character when flip_progress > 0.5
show_face := card.flip_progress > 0.5
if card.flip_direction {
show_face = card.flip_progress >= 0.5
} else {
show_face = card.flip_progress < 0.5
}
if show_face || card.is_face_up || card.is_matched {
font := assets.get_font()
text_w := raylib.MeasureText(card.character, 40)
text_x := card.x + (themes.CARD_WIDTH - text_w) / 2
text_y := card.y + (themes.CARD_HEIGHT - 40) / 2 - 8
raylib.DrawTextEx(font, card.character, raylib.Vector2{f32(text_x + 1), f32(text_y + 1)}, 40.0, 0, themes.BLACK)
raylib.DrawTextEx(font, card.character, raylib.Vector2{f32(text_x), f32(text_y)}, 40.0, 0, themes.WHITE)
}
} else if card.is_face_up || card.is_matched {
font := assets.get_font()
text_w := raylib.MeasureText(card.character, 40)
text_x := card.x + (themes.CARD_WIDTH - text_w) / 2
text_y := card.y + (themes.CARD_HEIGHT - 40) / 2 - 8
raylib.DrawTextEx(font, card.character, raylib.Vector2{f32(text_x + 1), f32(text_y + 1)}, 40.0, 0, themes.BLACK)
raylib.DrawTextEx(font, card.character, raylib.Vector2{f32(text_x), f32(text_y)}, 40.0, 0, themes.WHITE)
}
}
is_card_clicked :: proc(card: ^Card, mouse_x, mouse_y: i32) -> bool {
return mouse_x >= card.x && mouse_x <= card.x + themes.CARD_WIDTH &&
mouse_y >= card.y && mouse_y <= card.y + themes.CARD_HEIGHT
}