From f33b7048d7f1255a4f9c04057340f2c71d6fb982 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Sun, 8 Sep 2024 20:25:06 -0400 Subject: Turn into map --- examples/game/world.tm | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) (limited to 'examples/game/world.tm') diff --git a/examples/game/world.tm b/examples/game/world.tm index 6af7cc75..d921841b 100644 --- a/examples/game/world.tm +++ b/examples/game/world.tm @@ -38,9 +38,9 @@ func solve_overlap(a_pos:Vec2, a_size:Vec2, b_pos:Vec2, b_size:Vec2)->Vec2: return Vec2(0, 0) -struct World(player:@Player, boxes:[@Box], dt_accum=0.0): +struct World(player:@Player, goal:@Box, boxes:[@Box], dt_accum=0.0, won=no): DT := 1./60. - CURRENT := @World(@Player(Vec2(0,0), Vec2(0,0)), [:@Box]) + CURRENT := @World(@Player(Vec2(0,0), Vec2(0,0)), @Box(Vec2(0,0), Vec2(0,0), Color.GOAL), [:@Box]) STIFFNESS := 0.3 func update(w:&World, dt:Num): @@ -52,6 +52,9 @@ struct World(player:@Player, boxes:[@Box], dt_accum=0.0): func update_once(w:&World): w.player:update() + if solve_overlap(w.player.pos, Player.SIZE, w.goal.pos, w.goal.size) != Vec2(0,0): + w.won = yes + # Resolve player overlapping with any boxes: for i in 3: for b in w.boxes: @@ -60,19 +63,29 @@ struct World(player:@Player, boxes:[@Box], dt_accum=0.0): func draw(w:&World): for b in w.boxes: b:draw() + w.goal:draw() w.player:draw() + if w.won: + inline C { + DrawText("WINNER", GetScreenWidth()/2-48*3, GetScreenHeight()/2-24, 48, (Color){0,0,0,0xFF}); + } + func load_map(w:&World, map:Text): - map = map:replace_all({$/[]/: "X", $/@{1..}/: "@", $/ /: " "}) + if map:has($/[]/): + map = map:replace_all({$/[]/: "#", $/@{1..}/: "@", $/ /: " "}) w.boxes = [:@Box] box_size := Vec2(50., 50.) for y,line in map:lines(): for x,cell in line:split(): - if cell == "X": + if cell == "#": pos := Vec2((Num(x)-1) * box_size.x, (Num(y)-1) * box_size.y) box := @Box(pos, size=box_size, color=Color.GRAY) (&w.boxes):insert(box) else if cell == "@": pos := Vec2((Num(x)-1) * box_size.x, (Num(y)-1) * box_size.y) w.player = @Player(pos,pos) + else if cell == "?": + pos := Vec2((Num(x)-1) * box_size.x, (Num(y)-1) * box_size.y) + w.goal = @Box(pos, size=box_size, color=Color.GOAL) -- cgit v1.2.3