From 4a7df22256d27123f6871052f91ad70f86ebe66c Mon Sep 17 00:00:00 2001 From: abux Date: Tue, 14 Jul 2026 14:14:27 +0100 Subject: [PATCH] simple juice and cleanup --- src/assets/textures/black_triangle.png | Bin 0 -> 312 bytes src/assets/textures/proj.png | Bin 0 -> 1002 bytes src/component/animate.zig | 4 + src/component/health.zig | 2 +- src/component/material.zig | 6 +- src/component/transform.zig | 1 + src/event/juice/damage_flash.zig | 2 + src/event/juice/root.zig | 1 + src/event/root.zig | 1 + src/plugin/default.zig | 26 ++- src/resource/assets.zig | 15 ++ src/resource/game.zig | 2 +- src/resource/root.zig | 1 + src/system/ai.zig | 29 +++- src/system/collision.zig | 113 ++++++++++++ src/system/combat.zig | 54 ++++++ src/system/core.zig | 15 +- src/system/draw.zig | 33 +++- src/system/frame.zig | 230 +------------------------ src/system/health.zig | 51 ++++++ src/system/inputs.zig | 28 ++- src/system/juice.zig | 0 src/system/root.zig | 6 + src/system/timer.zig | 35 ++++ src/system/transform.zig | 32 ++++ 25 files changed, 428 insertions(+), 259 deletions(-) create mode 100644 src/assets/textures/black_triangle.png create mode 100644 src/assets/textures/proj.png create mode 100644 src/event/juice/damage_flash.zig create mode 100644 src/event/juice/root.zig create mode 100644 src/resource/assets.zig create mode 100644 src/system/collision.zig create mode 100644 src/system/combat.zig create mode 100644 src/system/health.zig create mode 100644 src/system/juice.zig create mode 100644 src/system/timer.zig create mode 100644 src/system/transform.zig diff --git a/src/assets/textures/black_triangle.png b/src/assets/textures/black_triangle.png new file mode 100644 index 0000000000000000000000000000000000000000..2e5db0d099ce9a1903e99613b83c5066a4d7e242 GIT binary patch literal 312 zcmeAS@N?(olHy`uVBq!ia0vp^3LwnE1|*BCs=fdz&H|6fVg?31We{epSZZGe6ny3B z;usR)es%Ieu4V(Cw!1ghipMA1_PnI^1B)7hd5SvZq5GIXd<%p=l*TJ zo0*f(Xxmv#Jjki7Sn^Q7>DvBHc8j{1Eq&)~T-CDU@zO61R}R%nE_j(=JO9{kgEPkR zDq^do6U;YEu`7COwMO~NUDF%9Q<}Dhn_MifviFU!KPLE^#89=b2g6o8b@ z3)2a+`Tw7Dmmhme-_^!iOwo9Qaw(@?|+Q*nGTc-DJ#}gT#uNgdD{an^L HB{Ts5z0iIT literal 0 HcmV?d00001 diff --git a/src/assets/textures/proj.png b/src/assets/textures/proj.png new file mode 100644 index 0000000000000000000000000000000000000000..64ede2bd9d23f10cd14cfe1791a2ca073be9b81c GIT binary patch literal 1002 zcmV*y-#yLK(oVzCCM_8#95$zURxZ&Fg3YJo0{K>{dx^LmKTPpQ6LT9X>4YdVjo#1 z9^x9vl98!*l$e^|f|W~{h7eMn-@#Ydtve4X<{ba{LNTHdXW*5I=P3$@v$K?(*5$`o zUKr*-H1+WwO+1U%J$(c9SCWOfWFeO>4@GNJUah+CBhEYrett2;4JPL{DK=1dP!_tn zuTt#G=MS$awxJbvv8#__VDR<>ie2!mJ3jc|zJBvQ#Ywozr}zewR){POS%soBn=md) zB1xbr&Xndt5Zd4eOJ`@j?V@Rf0kxCws>MbKVZhGE*A&~pav8-}hAKa6ht!t%w*7`; z1L*r$|BIQ1OwMh_c4}2lj&`H3&ZYSXGjU~{nQeGH&2fZ`Ybck_Oe>Qx2~9^zBxM{Y zL&jCcXE!K$L}{bbE2YUWK+3YbFiWKES}qMJPL$S0hi|rpz$&e5StgOCG^RyUB3(eX z@r&zHl|)War7>^JOpAn#POo6Qa&)jwahouN$ZnKY>I_3d4G8i?8uFWfG^emKXIs|) z7hghT84w$Yymj14N!rdEoJOFbdbmsCq(KRmOU+aR#3l@;8z`q7B#JVTHpY&X-Y<&af)SsIJuZ2l z-oK32{C_g7EX~B8*iKDKF7kZDx$qIDDSt_bqsJx#PBin$a@Ln7D29&bov(rK;}90? z99l&hf+rqq;q&md@@(5#gqg<*M$ivyb@&u b_pos[0] + b.offset[0] and + a_pos[1] + a.offset[1] < b_pos[1] + b.offset[1] + b.size[1] and + a_pos[1] + a.offset[1] + a.size[1] > b_pos[1] + b.offset[1]; +} diff --git a/src/system/combat.zig b/src/system/combat.zig new file mode 100644 index 0000000..921419d --- /dev/null +++ b/src/system/combat.zig @@ -0,0 +1,54 @@ +const std = @import("std"); +const App = @import("app"); + +const component = @import("../component/root.zig"); +const resource = @import("../resource/root.zig"); + +/// ---------------------------------------------------- +/// ---------------------------------------------------- +pub fn cast(ctx: App.scheduler.Context) !void { + const res = try ctx.app.resources.getMany(struct { + time: *resource.Time, + assets: *resource.Assets, + }); + + // --- QUERY --- + var query = ctx.app.world.query(struct { + caster: *component.Caster, + transform: *component.Transform, + }, .{}); + while (query.next()) |e| { + // --- SUBTRACT COOLDOWN --- + e.caster.current_cooldown -= + @max(0, res.time.dt); + + // --- SKIP IF ON COOLDOWN --- + if (e.caster.current_cooldown >= 0) + continue; + + // --- SPAWN PROJECTILE --- + try ctx.app.world.spawn(.{ + component.Transform{ + .pos = .{ + e.transform.pos[0] + (e.transform.size[0] / 2.0), + e.transform.pos[1] + (e.transform.size[1] / 2.0), + }, + .size = .{ 109, 32 }, + }, + component.AABB{}, + component.Velocity{ + .value = .{ 600, 0 }, + }, + component.Material{ .style = .{ .textured = .{ + .handle = res.assets.projectile, + } } }, + component.Projectile{ + .owner = query.entity(), + }, + }); + + // --- SET COOLDOWN --- + e.caster.current_cooldown = + e.caster.cooldown; + } +} diff --git a/src/system/core.zig b/src/system/core.zig index facd23c..92fe8a8 100644 --- a/src/system/core.zig +++ b/src/system/core.zig @@ -44,10 +44,17 @@ pub fn init(ctx: App.scheduler.Context) !void { resource.Game{}, ); - // --- ASSETS --- + // --- ASSETS LOADERS --- _ = try ctx.app.resources.getOrSet(resource.Fonts.init(ctx.app.alloc)); const textures = try ctx.app.resources.getOrSet(resource.Textures.init(ctx.app.alloc)); + // --- ASSETS --- + const assets = try ctx.app.resources.getOrSet(resource.Assets{ + .enemy = try textures.load("src/assets/textures/black_triangle.png"), + .player = try textures.load("src/assets/textures/player.png"), + .projectile = try textures.load("src/assets/textures/projectile.png"), + }); + // --- DEBUG --- std.debug.print("\x1b[35m┏━ WINDOW\n", .{}); std.debug.print("\x1b[35m┃\x1b[0m Title: \"{s}\"\n", .{window.title}); @@ -71,9 +78,9 @@ pub fn init(ctx: App.scheduler.Context) !void { component.Friction{}, component.Velocity{}, component.Transform{ .size = .{ 64, 64 } }, - component.Material{ .style = .{ - .textured = try textures.load("src/assets/textures/player.png"), - } }, + component.Material{ .style = .{ .textured = .{ + .handle = assets.player, + } } }, component.CameraTarget{}, }); diff --git a/src/system/draw.zig b/src/system/draw.zig index f0f824a..62d678e 100644 --- a/src/system/draw.zig +++ b/src/system/draw.zig @@ -21,9 +21,9 @@ pub fn draw(ctx: App.scheduler.Context) !void { // --- SET CLEAR COLOR --- _ = sdl.SDL_SetRenderDrawColor( res.renderer.raw, - 10, - 10, - 10, + 200, + 200, + 200, 255, ); @@ -50,6 +50,22 @@ pub fn draw(ctx: App.scheduler.Context) !void { .bob => |bob| { draw_pos[1] += @sin(res.time.elapsed * bob.frequency + bob.phase) * bob.amplitude; }, + .spawn => |spawn| { + const tmp_height: @Vector(2, f32) = .{ 32, 32 }; + e.transform.size[1] = 0; + + const t = @min(res.time.elapsed / spawn.duration, 1.0); + const scale = 1.0 - @exp(-8.0 * t) * @cos(20.0 * t); + + e.transform.size = tmp_height * @as( + @Vector(2, f32), + @splat(scale), + ); + + if (t >= 1.0) { + animate.style = spawn.next.*; + } + }, } } @@ -67,15 +83,20 @@ pub fn draw(ctx: App.scheduler.Context) !void { _ = sdl.SDL_SetRenderDrawColor(res.renderer.raw, albedo[0], albedo[1], albedo[2], albedo[3]); _ = sdl.SDL_RenderFillRect(res.renderer.raw, &rect); }, - .textured => |handle| { - const gpu = res.textures.data.get(handle) orelse continue; + .textured => |tex| { + const gpu = res.textures.data.get(tex.handle) orelse continue; const raw = if (gpu.raw) |*v| v else continue; - _ = sdl.SDL_RenderTexture( + _ = sdl.SDL_SetTextureColorMod(raw.*, tex.tint[0], tex.tint[1], tex.tint[2]); + _ = sdl.SDL_SetTextureAlphaMod(raw.*, tex.alpha); + _ = sdl.SDL_RenderTextureRotated( res.renderer.raw, raw.*, null, &rect, + e.transform.angle, + null, + 0, ); }, } diff --git a/src/system/frame.zig b/src/system/frame.zig index f9a7e50..7e4c758 100644 --- a/src/system/frame.zig +++ b/src/system/frame.zig @@ -1,3 +1,6 @@ +//! ---------------------------------------------------- +//! ---------------------------------------------------- + const std = @import("std"); const sdl = @import("sdl"); const gtl = @import("gtl"); @@ -127,230 +130,3 @@ pub fn endFrame(ctx: App.scheduler.Context) !void { sdl.SDL_Delay(@intFromFloat(delay_ms)); } } - -/// ---------------------------------------------------- -/// ---------------------------------------------------- -pub fn updateTransforms(ctx: App.scheduler.Context) !void { - const res = try ctx.app.resources.getMany(struct { - time: *resource.Time, - }); - - // --- QUERY --- - var query = ctx.app.world.query(struct { - transform: *component.Transform, - velocity: *component.Velocity, - friction: ?*component.Friction, - }, .{}); - while (query.next()) |e| { - // --- APPLY VELOCITY --- - e.transform.pos[0] += e.velocity.value[0] * res.time.dt; - e.transform.pos[1] += e.velocity.value[1] * res.time.dt; - - // --- APPLY FRICTION --- - if (e.friction) |friction| { - const damping = std.math.exp(-friction.value * res.time.dt); - e.velocity.value[0] *= damping; - e.velocity.value[1] *= damping; - } - } -} - -/// ---------------------------------------------------- -/// ---------------------------------------------------- -pub fn updateTimers(ctx: App.scheduler.Context) !void { - const res = try ctx.app.resources.getMany(struct { - time: *resource.Time, - }); - - var query = ctx.app.world.query(struct { - timer: *component.Timer, - }, .{}); - while (query.next()) |e| { - // --- # --- - if (e.timer.finished and !e.timer.repeat) - continue; - - // --- PROGRESS --- - e.timer.remaining -= - res.time.dt; - - if (e.timer.remaining <= 0.0) { - e.timer.finished = true; - - if (e.timer.repeat) { - e.timer.remaining = e.timer.duration; - e.timer.finished = false; - } - } - } -} - -/// ---------------------------------------------------- -/// ---------------------------------------------------- -pub fn cast(ctx: App.scheduler.Context) !void { - const res = try ctx.app.resources.getMany(struct { - time: *resource.Time, - textures: *resource.Textures, - }); - - // --- QUERY --- - var query = ctx.app.world.query(struct { - caster: *component.Caster, - transform: *component.Transform, - }, .{}); - while (query.next()) |e| { - // --- SUBTRACT COOLDOWN --- - e.caster.current_cooldown -= - @max(0, res.time.dt); - - // --- SKIP IF ON COOLDOWN --- - if (e.caster.current_cooldown >= 0) - continue; - - // --- SPAWN PROJECTILE --- - try ctx.app.world.spawn(.{ - component.Transform{ - .pos = .{ - e.transform.pos[0] + (e.transform.size[0] / 2.0), - e.transform.pos[1] + (e.transform.size[1] / 2.0), - }, - }, - component.AABB{}, - component.Velocity{ - .value = .{ 600, 0 }, - }, - component.Material{ .style = .{ - .textured = try res.textures.load("src/assets/textures/projectile.png"), - } }, - component.Projectile{ - .owner = query.entity(), - }, - }); - - // --- SET COOLDOWN --- - e.caster.current_cooldown = - e.caster.cooldown; - } -} - -/// ---------------------------------------------------- -/// ---------------------------------------------------- -pub fn resolveHealth(ctx: App.scheduler.Context) !void { - if (ctx.app.events.get(event.TakeDamage)) |queue| { - for (queue.items()) |take_dmg| { - const health = ctx.app.world.getComponent(component.Health, take_dmg.target) orelse continue; - if (health.current <= 0) continue; - health.current -= take_dmg.amount; - - std.debug.print("{s}[TAKE_DAMAGE]{s} source({}) target({}) amount({})\n", .{ - gtl.ansi.red, - gtl.ansi.reset, - take_dmg.source, - take_dmg.target, - take_dmg.amount, - }); - } - queue.clear(); - } - - var query = ctx.app.world.query(struct { - health: *component.Health, - }, .{}); - while (query.next()) |e| { - if (e.health.current <= 0) { - ctx.app.world.despawn(query.entity()); - } - } -} - -/// ---------------------------------------------------- -/// ---------------------------------------------------- -pub fn resolveCollisions(ctx: App.scheduler.Context) !void { - if (ctx.app.events.get(event.Collision)) |queue| { - for (queue.items()) |coll| { - const proj_a = ctx.app.world.getComponent(component.Projectile, coll.a); - const proj_b = ctx.app.world.getComponent(component.Projectile, coll.b); - - if (proj_a) |projectile| { - if (projectile.owner != coll.b and - ctx.app.world.hasComponent(component.Health, coll.b)) - { - try ctx.app.events.send(event.TakeDamage{ - .target = coll.b, - .source = coll.a, - .amount = projectile.damage, - }); - - ctx.app.world.despawn(coll.a); - } - } - - if (proj_b) |projectile| { - if (projectile.owner != coll.a and - ctx.app.world.hasComponent(component.Health, coll.a)) - { - try ctx.app.events.send(event.TakeDamage{ - .target = coll.a, - .source = coll.b, - .amount = projectile.damage, - }); - - ctx.app.world.despawn(coll.b); - } - } - } - - queue.clear(); - } -} - -/// ---------------------------------------------------- -/// ---------------------------------------------------- -pub fn checkAABB(ctx: App.scheduler.Context) !void { - var outer = ctx.app.world.query(struct { - aabb: *component.AABB, - transform: *component.Transform, - }, .{}); - - while (outer.next()) |a| { - const entity_a = outer.entity(); - - var inner = ctx.app.world.query(struct { - aabb: *component.AABB, - transform: *component.Transform, - }, .{}); - - while (inner.next()) |b| { - const entity_b = inner.entity(); - - // --- PREVENT SAME ENTITY --- - if (entity_a == entity_b) - continue; - - // --- CHECK IF OVERLAPS --- - if (overlaps( - a.transform.pos, - a.aabb, - b.transform.pos, - b.aabb, - )) { - try ctx.app.events.send(event.Collision{ - .a = entity_a, - .b = entity_b, - }); - } - } - } -} - -fn overlaps( - a_pos: @Vector(2, f32), - a: *component.AABB, - b_pos: @Vector(2, f32), - b: *component.AABB, -) bool { - return a_pos[0] + a.offset[0] < b_pos[0] + b.offset[0] + b.size[0] and - a_pos[0] + a.offset[0] + a.size[0] > b_pos[0] + b.offset[0] and - a_pos[1] + a.offset[1] < b_pos[1] + b.offset[1] + b.size[1] and - a_pos[1] + a.offset[1] + a.size[1] > b_pos[1] + b.offset[1]; -} diff --git a/src/system/health.zig b/src/system/health.zig new file mode 100644 index 0000000..e408c84 --- /dev/null +++ b/src/system/health.zig @@ -0,0 +1,51 @@ +const std = @import("std"); +const sdl = @import("sdl"); +const gtl = @import("gtl"); +const App = @import("app"); + +const component = @import("../component/root.zig"); +const resource = @import("../resource/root.zig"); +const event = @import("../event/root.zig"); + +/// ---------------------------------------------------- +/// ---------------------------------------------------- +pub fn resolve(ctx: App.scheduler.Context) !void { + if (ctx.app.events.get(event.TakeDamage)) |queue| { + for (queue.items()) |take_dmg| { + const health = ctx.app.world.getComponent(component.Health, take_dmg.target) orelse continue; + if (health.current <= 0) continue; + health.current -= take_dmg.amount; + + std.debug.print("{s}[TAKE_DAMAGE]{s} source({}) target({}) amount({})\n", .{ + gtl.ansi.red, + gtl.ansi.reset, + take_dmg.source, + take_dmg.target, + take_dmg.amount, + }); + } + queue.clear(); + } + + var query = ctx.app.world.query(struct { + health: *component.Health, + }, .{}); + while (query.next()) |e| { + if (e.health.current <= 0) { + ctx.app.world.despawn(query.entity()); + } + } +} + +/// ---------------------------------------------------- +/// ---------------------------------------------------- +pub fn juice(ctx: App.scheduler.Context) !void { + if (ctx.app.events.get(event.juice.DamageFlash)) |queue| { + for (queue.items()) |dmg_flash| { + const material = ctx.app.world.getComponent(component.Material, dmg_flash.target) orelse continue; + + material.style.textured.tint = dmg_flash.tint; + } + queue.clear(); + } +} diff --git a/src/system/inputs.zig b/src/system/inputs.zig index 541aa69..c864522 100644 --- a/src/system/inputs.zig +++ b/src/system/inputs.zig @@ -27,13 +27,33 @@ pub fn pollEvents(ctx: App.scheduler.Context) !void { var query = ctx.app.world.query(struct { player: *component.Player, + transform: *component.Transform, velocity: *component.Velocity, speed: *component.Speed, }, .{}); while (query.next()) |e| { - if (keyboard[sdl.SDL_SCANCODE_W]) e.velocity.value[1] = -e.speed.value; - if (keyboard[sdl.SDL_SCANCODE_S]) e.velocity.value[1] = e.speed.value; - if (keyboard[sdl.SDL_SCANCODE_A]) e.velocity.value[0] = -e.speed.value; - if (keyboard[sdl.SDL_SCANCODE_D]) e.velocity.value[0] = e.speed.value; + e.transform.angle = 0; + + // --- UP --- + if (keyboard[sdl.SDL_SCANCODE_W]) { + e.velocity.value[1] = -e.speed.value; + } + + // --- DOWN --- + if (keyboard[sdl.SDL_SCANCODE_S]) { + e.velocity.value[1] = e.speed.value; + } + + // --- LEFT --- + if (keyboard[sdl.SDL_SCANCODE_A]) { + e.velocity.value[0] = -e.speed.value; + e.transform.angle = -5; + } + + // --- RIGHT --- + if (keyboard[sdl.SDL_SCANCODE_D]) { + e.velocity.value[0] = e.speed.value; + e.transform.angle = 5; + } } } diff --git a/src/system/juice.zig b/src/system/juice.zig new file mode 100644 index 0000000..e69de29 diff --git a/src/system/root.zig b/src/system/root.zig index 97290a4..cbd8dee 100644 --- a/src/system/root.zig +++ b/src/system/root.zig @@ -3,3 +3,9 @@ pub const frame = @import("frame.zig"); pub const inputs = @import("inputs.zig"); pub const draw = @import("draw.zig"); pub const ai = @import("ai.zig"); +pub const collision = @import("collision.zig"); +pub const health = @import("health.zig"); +pub const transform = @import("transform.zig"); +pub const combat = @import("combat.zig"); +pub const timer = @import("timer.zig"); +pub const juice = @import("juice.zig"); diff --git a/src/system/timer.zig b/src/system/timer.zig new file mode 100644 index 0000000..e0aff07 --- /dev/null +++ b/src/system/timer.zig @@ -0,0 +1,35 @@ +const std = @import("std"); +const App = @import("app"); + +const component = @import("../component/root.zig"); +const resource = @import("../resource/root.zig"); + +/// ---------------------------------------------------- +/// ---------------------------------------------------- +pub fn update(ctx: App.scheduler.Context) !void { + const res = try ctx.app.resources.getMany(struct { + time: *resource.Time, + }); + + var query = ctx.app.world.query(struct { + timer: *component.Timer, + }, .{}); + while (query.next()) |e| { + // --- # --- + if (e.timer.finished and !e.timer.repeat) + continue; + + // --- PROGRESS --- + e.timer.remaining -= + res.time.dt; + + if (e.timer.remaining <= 0.0) { + e.timer.finished = true; + + if (e.timer.repeat) { + e.timer.remaining = e.timer.duration; + e.timer.finished = false; + } + } + } +} diff --git a/src/system/transform.zig b/src/system/transform.zig new file mode 100644 index 0000000..230ceb3 --- /dev/null +++ b/src/system/transform.zig @@ -0,0 +1,32 @@ +const std = @import("std"); +const App = @import("app"); + +const component = @import("../component/root.zig"); +const resource = @import("../resource/root.zig"); + +/// ---------------------------------------------------- +/// ---------------------------------------------------- +pub fn update(ctx: App.scheduler.Context) !void { + const res = try ctx.app.resources.getMany(struct { + time: *resource.Time, + }); + + // --- QUERY --- + var query = ctx.app.world.query(struct { + transform: *component.Transform, + velocity: *component.Velocity, + friction: ?*component.Friction, + }, .{}); + while (query.next()) |e| { + // --- APPLY VELOCITY --- + e.transform.pos[0] += e.velocity.value[0] * res.time.dt; + e.transform.pos[1] += e.velocity.value[1] * res.time.dt; + + // --- APPLY FRICTION --- + if (e.friction) |friction| { + const damping = std.math.exp(-friction.value * res.time.dt); + e.velocity.value[0] *= damping; + e.velocity.value[1] *= damping; + } + } +}