feat: add menu to start new game or quit
parent
4a6e419213
commit
57825e9534
|
@ -16,8 +16,8 @@
|
||||||
// internet connectivity.
|
// internet connectivity.
|
||||||
.dependencies = .{
|
.dependencies = .{
|
||||||
.seizer = .{
|
.seizer = .{
|
||||||
.url = "https://github.com/leroycep/seizer/archive/d425db26fbbf5a99faa70ea07fb7d2b028f690d1.tar.gz",
|
.url = "https://github.com/leroycep/seizer/archive/2be76fb909fc487da4983a487d0695fc749f714a.tar.gz",
|
||||||
.hash = "1220250791e125cf4d342020eb3af65c36a8b971559ae9ffafc187c0cb116ee3d6e2",
|
.hash = "12207072224809e8a8d2c1342b5e14db5b6fbb1fbbd7f5c6410d71d3708c9241261e",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
.paths = .{
|
.paths = .{
|
||||||
|
|
|
@ -385,7 +385,7 @@ pub fn loadSolitaireCards(gpa: std.mem.Allocator) !DeckSprites {
|
||||||
.blank = 13,
|
.blank = 13,
|
||||||
.back = 27,
|
.back = 27,
|
||||||
.margin = 4,
|
.margin = 4,
|
||||||
.hand_offset = .{ 13, 12 },
|
.hand_offset = .{ 10, 12 },
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
105
src/main.zig
105
src/main.zig
|
@ -2,6 +2,7 @@ pub const main = seizer.main;
|
||||||
|
|
||||||
var gpa: std.mem.Allocator = undefined;
|
var gpa: std.mem.Allocator = undefined;
|
||||||
var prng: std.rand.DefaultPrng = undefined;
|
var prng: std.rand.DefaultPrng = undefined;
|
||||||
|
var window_global: *seizer.Window = undefined;
|
||||||
var canvas: seizer.Canvas = undefined;
|
var canvas: seizer.Canvas = undefined;
|
||||||
var card_tilemap: ?DeckSprites = null;
|
var card_tilemap: ?DeckSprites = null;
|
||||||
|
|
||||||
|
@ -19,11 +20,19 @@ var hovered_card: usize = 0;
|
||||||
var selected_deck: ?*std.ArrayListUnmanaged(Card) = null;
|
var selected_deck: ?*std.ArrayListUnmanaged(Card) = null;
|
||||||
var selected_card: usize = 0;
|
var selected_card: usize = 0;
|
||||||
|
|
||||||
|
var show_menu: bool = false;
|
||||||
|
var menu_item_selected: u32 = 0;
|
||||||
|
const MENU_ITEMS = [_][]const u8{
|
||||||
|
"Continue",
|
||||||
|
"New Game",
|
||||||
|
"Quit",
|
||||||
|
};
|
||||||
|
|
||||||
pub fn init(context: *seizer.Context) !void {
|
pub fn init(context: *seizer.Context) !void {
|
||||||
gpa = context.gpa;
|
gpa = context.gpa;
|
||||||
prng = std.rand.DefaultPrng.init(@bitCast(std.time.timestamp()));
|
prng = std.rand.DefaultPrng.init(@bitCast(std.time.timestamp()));
|
||||||
|
|
||||||
_ = try context.createWindow(.{
|
window_global = try context.createWindow(.{
|
||||||
.title = "Seizer Solitaire",
|
.title = "Seizer Solitaire",
|
||||||
.on_render = render,
|
.on_render = render,
|
||||||
.on_destroy = destroy,
|
.on_destroy = destroy,
|
||||||
|
@ -34,15 +43,7 @@ pub fn init(context: *seizer.Context) !void {
|
||||||
|
|
||||||
card_tilemap = try assets.loadSolitaireCards(context.gpa);
|
card_tilemap = try assets.loadSolitaireCards(context.gpa);
|
||||||
|
|
||||||
draw_pile = std.ArrayListUnmanaged(Card).fromOwnedSlice(try makeStandardDeck(gpa));
|
try resetGame();
|
||||||
prng.random().shuffle(Card, draw_pile.items);
|
|
||||||
|
|
||||||
for (&stacks, 0..) |*stack, i| {
|
|
||||||
for (0..i + 1) |_| {
|
|
||||||
const drawn_card = draw_pile.pop();
|
|
||||||
try stack.append(gpa, drawn_card);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
try context.addButtonInput(.{
|
try context.addButtonInput(.{
|
||||||
.title = "move_up",
|
.title = "move_up",
|
||||||
|
@ -74,6 +75,41 @@ pub fn init(context: *seizer.Context) !void {
|
||||||
.on_event = deselect,
|
.on_event = deselect,
|
||||||
.default_bindings = &.{.b},
|
.default_bindings = &.{.b},
|
||||||
});
|
});
|
||||||
|
try context.addButtonInput(.{
|
||||||
|
.title = "toggle_menu",
|
||||||
|
.on_event = toggleMenu,
|
||||||
|
.default_bindings = &.{.start},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
fn resetGame() !void {
|
||||||
|
draw_pile.shrinkRetainingCapacity(0);
|
||||||
|
draw_pile_exhausted = false;
|
||||||
|
drawn_cards.shrinkRetainingCapacity(0);
|
||||||
|
for (&stacks) |*stack| {
|
||||||
|
stack.shrinkRetainingCapacity(0);
|
||||||
|
}
|
||||||
|
for (&foundations) |*foundation| {
|
||||||
|
foundation.shrinkRetainingCapacity(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
last_hovered_stack = 0;
|
||||||
|
last_hovered_foundation = 0;
|
||||||
|
hovered_deck = null;
|
||||||
|
hovered_card = 0;
|
||||||
|
|
||||||
|
selected_deck = null;
|
||||||
|
selected_card = 0;
|
||||||
|
|
||||||
|
draw_pile = std.ArrayListUnmanaged(Card).fromOwnedSlice(try makeStandardDeck(gpa));
|
||||||
|
prng.random().shuffle(Card, draw_pile.items);
|
||||||
|
|
||||||
|
for (&stacks, 0..) |*stack, i| {
|
||||||
|
for (0..i + 1) |_| {
|
||||||
|
const drawn_card = draw_pile.pop();
|
||||||
|
try stack.append(gpa, drawn_card);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn destroy(window: *seizer.Window) void {
|
fn destroy(window: *seizer.Window) void {
|
||||||
|
@ -212,6 +248,25 @@ fn render(window: *seizer.Window) !void {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (show_menu) {
|
||||||
|
canvas.rect(
|
||||||
|
.{ canvas.window_size[0] / 3, canvas.window_size[1] / 3 },
|
||||||
|
.{ canvas.window_size[0] / 3, canvas.window_size[1] / 3 },
|
||||||
|
.{ .color = .{ 0, 0, 0, 0xFF } },
|
||||||
|
);
|
||||||
|
const x: f32 = canvas.window_size[0] / 2;
|
||||||
|
var y: f32 = canvas.window_size[1] / 3 + (canvas.font.lineHeight * scalef);
|
||||||
|
for (MENU_ITEMS, 0..) |menu_item, i| {
|
||||||
|
if (i == menu_item_selected) {
|
||||||
|
const size = canvas.printText(.{ x, y }, "> {s} <", .{menu_item}, .{ .@"align" = .center, .scale = scalef });
|
||||||
|
y += size[1];
|
||||||
|
} else {
|
||||||
|
const size = canvas.writeText(.{ x, y }, menu_item, .{ .@"align" = .center, .scale = scalef });
|
||||||
|
y += size[1];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
canvas.end();
|
canvas.end();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -235,12 +290,25 @@ pub fn makeStandardDeck(allocator: std.mem.Allocator) ![]Card {
|
||||||
|
|
||||||
pub fn deselect(pressed: bool) !void {
|
pub fn deselect(pressed: bool) !void {
|
||||||
if (!pressed) return;
|
if (!pressed) return;
|
||||||
|
if (show_menu) return;
|
||||||
selected_deck = null;
|
selected_deck = null;
|
||||||
selected_card = 0;
|
selected_card = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn doSelectOrPlace(pressed: bool) !void {
|
pub fn doSelectOrPlace(pressed: bool) !void {
|
||||||
if (!pressed) return;
|
if (!pressed) return;
|
||||||
|
if (show_menu) {
|
||||||
|
const menu_item = MENU_ITEMS[menu_item_selected];
|
||||||
|
if (std.mem.eql(u8, menu_item, "Continue")) {
|
||||||
|
show_menu = false;
|
||||||
|
} else if (std.mem.eql(u8, menu_item, "New Game")) {
|
||||||
|
try resetGame();
|
||||||
|
show_menu = false;
|
||||||
|
} else if (std.mem.eql(u8, menu_item, "Quit")) {
|
||||||
|
window_global.setShouldClose(true);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (selected_deck == null) {
|
if (selected_deck == null) {
|
||||||
if (hovered_deck == &draw_pile and !draw_pile_exhausted) {
|
if (hovered_deck == &draw_pile and !draw_pile_exhausted) {
|
||||||
drawn_cards.ensureUnusedCapacity(gpa, 3) catch return;
|
drawn_cards.ensureUnusedCapacity(gpa, 3) catch return;
|
||||||
|
@ -346,6 +414,7 @@ pub fn doSelectOrPlace(pressed: bool) !void {
|
||||||
|
|
||||||
pub fn moveLeft(pressed: bool) !void {
|
pub fn moveLeft(pressed: bool) !void {
|
||||||
if (!pressed) return;
|
if (!pressed) return;
|
||||||
|
if (show_menu) return;
|
||||||
if (hovered_deck == null) {
|
if (hovered_deck == null) {
|
||||||
hovered_deck = &draw_pile;
|
hovered_deck = &draw_pile;
|
||||||
hovered_card = 0;
|
hovered_card = 0;
|
||||||
|
@ -378,6 +447,7 @@ pub fn moveLeft(pressed: bool) !void {
|
||||||
|
|
||||||
pub fn moveRight(pressed: bool) !void {
|
pub fn moveRight(pressed: bool) !void {
|
||||||
if (!pressed) return;
|
if (!pressed) return;
|
||||||
|
if (show_menu) return;
|
||||||
if (hovered_deck == null) {
|
if (hovered_deck == null) {
|
||||||
hovered_deck = &draw_pile;
|
hovered_deck = &draw_pile;
|
||||||
hovered_card = 0;
|
hovered_card = 0;
|
||||||
|
@ -411,7 +481,10 @@ pub fn moveRight(pressed: bool) !void {
|
||||||
pub fn moveUp(pressed: bool) !void {
|
pub fn moveUp(pressed: bool) !void {
|
||||||
if (!pressed) return;
|
if (!pressed) return;
|
||||||
|
|
||||||
if (hovered_deck == null) {
|
if (show_menu) {
|
||||||
|
if (menu_item_selected == 0) menu_item_selected = MENU_ITEMS.len;
|
||||||
|
menu_item_selected -= 1;
|
||||||
|
} else if (hovered_deck == null) {
|
||||||
hovered_deck = &draw_pile;
|
hovered_deck = &draw_pile;
|
||||||
hovered_card = 0;
|
hovered_card = 0;
|
||||||
} else if (hovered_deck == &draw_pile) {
|
} else if (hovered_deck == &draw_pile) {
|
||||||
|
@ -452,7 +525,10 @@ pub fn moveUp(pressed: bool) !void {
|
||||||
|
|
||||||
pub fn moveDown(pressed: bool) !void {
|
pub fn moveDown(pressed: bool) !void {
|
||||||
if (!pressed) return;
|
if (!pressed) return;
|
||||||
if (hovered_deck == null) {
|
if (show_menu) {
|
||||||
|
menu_item_selected += 1;
|
||||||
|
menu_item_selected %= MENU_ITEMS.len;
|
||||||
|
} else if (hovered_deck == null) {
|
||||||
hovered_deck = &draw_pile;
|
hovered_deck = &draw_pile;
|
||||||
hovered_card = 0;
|
hovered_card = 0;
|
||||||
} else if (hovered_deck == &draw_pile) {
|
} else if (hovered_deck == &draw_pile) {
|
||||||
|
@ -491,6 +567,11 @@ pub fn moveDown(pressed: bool) !void {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn toggleMenu(pressed: bool) !void {
|
||||||
|
if (!pressed) return;
|
||||||
|
show_menu = !show_menu;
|
||||||
|
}
|
||||||
|
|
||||||
pub fn indexOfTopOfStack(cards: []Card) usize {
|
pub fn indexOfTopOfStack(cards: []Card) usize {
|
||||||
if (cards.len < 2) return 0;
|
if (cards.len < 2) return 0;
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue