aboutsummaryrefslogtreecommitdiff
path: root/examples/game/world.tm
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2024-09-08 20:25:06 -0400
committerBruce Hill <bruce@bruce-hill.com>2024-09-08 20:25:06 -0400
commitf33b7048d7f1255a4f9c04057340f2c71d6fb982 (patch)
tree8b7ea09409eef75696ba60a8ff5f60b8af1ec83d /examples/game/world.tm
parent9f7f4eb858d723b0d4fc954b1575feb05f0f8134 (diff)
Turn into map
Diffstat (limited to 'examples/game/world.tm')
-rw-r--r--examples/game/world.tm21
1 files changed, 17 insertions, 4 deletions
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)