Something went wrong. Try again.
A card game engine for Magic: The Gathering inspired by Card Forge
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135const Self = @This();
const rl = @import("raylib");const consts = @import("consts.zig");const zecs = @import("ecs");const std = @import("std");const assets = @import("assets.zig");const core = @import("core");
reg: zecs.Registry,font: rl.Font,zones: std.StringHashMap(std.StringHashMap(rl.Rectangle)),cardWidth: f32,noCardImage: rl.Texture,game: core.Game,io: std.Io,alloc: std.mem.Allocator,
pub fn new(alloc: std.mem.Allocator, io: std.Io) Self { var reg = zecs.Registry.init(alloc);
const playerNames = [4][]const u8{ "Player 1", "Player 2", "Player 3", "Player 4", };
return Self{ .reg = reg, .font = undefined, .zones = .init(alloc), .cardWidth = 0.0, .noCardImage = undefined, .game = .init(®, io, alloc, &playerNames), .io = io, .alloc = alloc, };}
pub fn run(app: *Self) anyerror!void { rl.initWindow(consts.SCREEN_WIDTH, consts.SCREEN_HEIGHT, "ManaGrove"); defer rl.closeWindow(); rl.setTargetFPS(60); try app.init_game();
while (!rl.windowShouldClose()) { app.update_game();
{ rl.beginDrawing(); defer rl.endDrawing(); rl.clearBackground(.black); app.draw_game(); } }}
pub fn clean(app: *Self) void { app.reg.deinit();}
const randomType = struct { val: i32 = 0,};
fn init_game(app: *Self) anyerror!void { const noCardLoadedImage = try rl.loadImageFromMemory(".png", assets.images.get("no_card.png").?); app.cardWidth = @round((1080.0 / 6.0) * (@as(f32, @floatFromInt(noCardLoadedImage.width)) / @as(f32, @floatFromInt(noCardLoadedImage.height)))); app.noCardImage = try rl.loadTextureFromImage(noCardLoadedImage); rl.unloadImage(noCardLoadedImage);
_ = try app.game.setupTestData(app.io, app.alloc);
const random_entity = app.reg.create(); app.reg.add(random_entity, randomType{}); app.font = try rl.loadFontFromMemory(".ttf", assets.fonts.get("Carlito-Regular.ttf").?, 24, null);
try app.zones.put("topRight", .init(app.zones.allocator)); try app.zones.put("topLeft", .init(app.zones.allocator)); try app.zones.put("bottomLeft", .init(app.zones.allocator)); var bottomRight: std.StringHashMap(rl.Rectangle) = .init(app.zones.allocator);
try bottomRight.put("battlefield", .init((@as(f32, @floatFromInt(consts.SCREEN_WIDTH)) - 361.0) / 2.0, @as(f32, @floatFromInt(consts.SCREEN_HEIGHT)) / 2.0, (@as(f32, @floatFromInt(consts.SCREEN_WIDTH)) - 361.0) / 2.0, @as(f32, @floatFromInt(consts.SCREEN_HEIGHT)) / 2.0 * (2.0 / 3.0))); try bottomRight.put("command", .init(@as(f32, @floatFromInt(consts.SCREEN_WIDTH)) - 361.0 - app.cardWidth, @as(f32, @floatFromInt(consts.SCREEN_HEIGHT)) * (5.0 / 6.0), app.cardWidth, @as(f32, @floatFromInt(consts.SCREEN_HEIGHT)) / 6.0)); try bottomRight.put("exile", .init(@as(f32, @floatFromInt(consts.SCREEN_WIDTH)) - 361.0 - (app.cardWidth * 2.0), @as(f32, @floatFromInt(consts.SCREEN_HEIGHT)) * (5.0 / 6.0), app.cardWidth, @as(f32, @floatFromInt(consts.SCREEN_HEIGHT)) / 6.0)); try bottomRight.put("graveyard", .init(@as(f32, @floatFromInt(consts.SCREEN_WIDTH)) - 361.0 - (app.cardWidth * 3.0), @as(f32, @floatFromInt(consts.SCREEN_HEIGHT)) * (5.0 / 6.0), app.cardWidth, @as(f32, @floatFromInt(consts.SCREEN_HEIGHT)) / 6.0)); try bottomRight.put("library", .init(@as(f32, @floatFromInt(consts.SCREEN_WIDTH)) - 361.0 - (app.cardWidth * 4.0), @as(f32, @floatFromInt(consts.SCREEN_HEIGHT)) * (5.0 / 6.0), app.cardWidth, @as(f32, @floatFromInt(consts.SCREEN_HEIGHT)) / 6.0)); try bottomRight.put("hand", .init((@as(f32, @floatFromInt(consts.SCREEN_WIDTH)) - 361.0) / 2.0, @as(f32, @floatFromInt(consts.SCREEN_HEIGHT)) * (5.0 / 6.0), ((@as(f32, @floatFromInt(consts.SCREEN_WIDTH)) - 361.0) / 2.0) - (app.cardWidth * 4.0), @as(f32, @floatFromInt(consts.SCREEN_HEIGHT)) / 6.0)); try app.zones.put("bottomRight", bottomRight);}
fn update_game(app: *Self) void { const view = app.reg.view(.{randomType}, .{}); var viewIter = view.entityIterator();
while (viewIter.next()) |entity| { const data = app.reg.getConst(randomType, entity); std.debug.print("val: {}\n", .{data.val}); }}
fn draw_game(app: *Self) void { app.render_debug_areas();}
fn render_debug_areas(app: *Self) void { // Player 2 Stuff rl.drawRectangleRec(.init(0.0, 0.0, (@as(f32, @floatFromInt(consts.SCREEN_WIDTH)) - 361.0) / 2.0, @as(f32, @floatFromInt(consts.SCREEN_HEIGHT)) / 2.0), .init(0x7f, 0x00, 0x00, 0xff)); rl.drawTextEx(app.font, "Player 2", .init((@as(f32, @floatFromInt(consts.SCREEN_WIDTH)) - 361.0) / 4.0, @as(f32, @floatFromInt(consts.SCREEN_HEIGHT)) / 4.0), 24.0, 1.0, .white); // Player 3 Stuff rl.drawRectangleRec(.init(0.0, @as(f32, @floatFromInt(consts.SCREEN_HEIGHT)) / 2.0, (@as(f32, @floatFromInt(consts.SCREEN_WIDTH)) - 361.0) / 2.0, @as(f32, @floatFromInt(consts.SCREEN_HEIGHT)) / 2.0), .init(0x55, 0x00, 0x00, 0xff)); rl.drawTextEx(app.font, "Player 3", .init((@as(f32, @floatFromInt(consts.SCREEN_WIDTH)) - 361.0) / 4.0, @as(f32, @floatFromInt(consts.SCREEN_HEIGHT)) * (3.0 / 4.0)), 24.0, 1.0, .white); // Player 4 Stuff rl.drawRectangleRec(.init((@as(f32, @floatFromInt(consts.SCREEN_WIDTH)) - 361.0) / 2.0, 0.0, (@as(f32, @floatFromInt(consts.SCREEN_WIDTH)) - 361.0) / 2.0, @as(f32, @floatFromInt(consts.SCREEN_HEIGHT)) / 2.0), .init(0x00, 0x7f, 0x00, 0xff)); rl.drawTextEx(app.font, "Player 4", .init((@as(f32, @floatFromInt(consts.SCREEN_WIDTH)) - 361.0) * (3.0 / 4.0), @as(f32, @floatFromInt(consts.SCREEN_HEIGHT)) / 4.0), 24.0, 1.0, .white); // Player 1 stuff rl.drawRectangleRec(.init((@as(f32, @floatFromInt(consts.SCREEN_WIDTH)) - 361.0) / 2.0, @as(f32, @floatFromInt(consts.SCREEN_HEIGHT)) / 2.0, (@as(f32, @floatFromInt(consts.SCREEN_WIDTH)) - 361.0) / 2.0, @as(f32, @floatFromInt(consts.SCREEN_HEIGHT)) / 2.0), .init(0x00, 0x55, 0x00, 0xff)); // // Command Zone rl.drawRectangleRec(app.zones.get("bottomRight").?.get("command").?, .init(0x00, 0xff, 0x00, 0xff)); rl.drawTextEx(app.font, "Command", .init(@as(f32, @floatFromInt(consts.SCREEN_WIDTH)) - 361.0 - app.cardWidth, @as(f32, @floatFromInt(consts.SCREEN_HEIGHT)) * (11.0 / 12.0)), 24.0, 1.0, .black); // // Exile Zone rl.drawRectangleRec(app.zones.get("bottomRight").?.get("exile").?, .init(0x3f, 0x3f, 0xff, 0xff)); rl.drawTextEx(app.font, "Exile", .init(@as(f32, @floatFromInt(consts.SCREEN_WIDTH)) - 361.0 - (app.cardWidth * 2.0), @as(f32, @floatFromInt(consts.SCREEN_HEIGHT)) * (11.0 / 12.0)), 24.0, 1.0, .init(0xee, 0xee, 0xee, 0xff)); // // Graveyard Zone rl.drawRectangleRec(app.zones.get("bottomRight").?.get("graveyard").?, .init(0xff, 0x3f, 0x3f, 0xff)); rl.drawTextEx(app.font, "Graveyard", .init(@as(f32, @floatFromInt(consts.SCREEN_WIDTH)) - 361.0 - (app.cardWidth * 3.0), @as(f32, @floatFromInt(consts.SCREEN_HEIGHT)) * (11.0 / 12.0)), 24.0, 1.0, .black); // // Library Zone rl.drawRectangleRec(app.zones.get("bottomRight").?.get("library").?, .init(0xff, 0xff, 0x00, 0xff)); rl.drawTextEx(app.font, "Library", .init(@as(f32, @floatFromInt(consts.SCREEN_WIDTH)) - 361.0 - (app.cardWidth * 4.0), @as(f32, @floatFromInt(consts.SCREEN_HEIGHT)) * (11.0 / 12.0)), 24.0, 1.0, .black); // // Hand Zone rl.drawRectangleRec(app.zones.get("bottomRight").?.get("hand").?, .init(0x00, 0xff, 0xff, 0xff)); rl.drawTextEx(app.font, "Hand", .init((@as(f32, @floatFromInt(consts.SCREEN_WIDTH)) - 361.0) / 2.0, @as(f32, @floatFromInt(consts.SCREEN_HEIGHT)) * (11.0 / 12.0)), 24.0, 1.0, .black);}