aboutsummaryrefslogtreecommitdiff
path: root/examples/game
diff options
context:
space:
mode:
Diffstat (limited to 'examples/game')
-rw-r--r--examples/game/box.tm2
-rw-r--r--examples/game/color.tm9
-rw-r--r--examples/game/map.txt35
-rw-r--r--examples/game/player.tm4
-rw-r--r--examples/game/world.tm21
5 files changed, 43 insertions, 28 deletions
diff --git a/examples/game/box.tm b/examples/game/box.tm
index 88713154..acff269e 100644
--- a/examples/game/box.tm
+++ b/examples/game/box.tm
@@ -5,5 +5,5 @@ use ./world.tm
use ./color.tm
struct Box(pos:Vec2, size=Vec2(50, 50), color=Color.GRAY, blocking=yes):
- func draw(b:&Box):
+ func draw(b:Box):
b.color:draw_rectangle(b.pos, b.size)
diff --git a/examples/game/color.tm b/examples/game/color.tm
index 79807a6d..c7ba557a 100644
--- a/examples/game/color.tm
+++ b/examples/game/color.tm
@@ -3,10 +3,11 @@
use <raylib.h>
use vectors
-struct Color(r,g,b:Num32,a=1.0f32):
- RED := Color(1,0,0)
- GRAY := Color(.2f32,.2f32,.2f32)
- LIGHT_GRAY := Color(.7f32,.7f32,.7f32)
+struct Color(r,g,b:Num,a=1.0):
+ PLAYER := Color(.1,.1,.6,1.)
+ GRAY := Color(.4,.4,.4)
+ LIGHT_GRAY := Color(.7,.7,.7)
+ GOAL := Color(.1,.5,.0)
func draw_rectangle(c:Color, pos:Vec2, size:Vec2):
inline C {
diff --git a/examples/game/map.txt b/examples/game/map.txt
index d0436a0a..46fccd09 100644
--- a/examples/game/map.txt
+++ b/examples/game/map.txt
@@ -1,17 +1,18 @@
-[][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
-[] [] []
-[] [] []
-[] [] []
-[] [] []
-[] [] []
-[] [] [] []
-[] @@ [] []
-[] [] []
-[] [] []
-[] [] []
-[] [] []
-[] [] []
-[] [] []
-[] [] []
-[] [] []
-[][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
+################################
+#@ # # #
+# #### #### # # #### #####
+# # # # # #
+# ####### # ####### # # ###
+# # # # # # #
+# #### ########## # #### #
+# # # # # # #
+#### # # # # # # # # ##
+# # # # # # # #
+####### ########## # ########
+# # # # #
+# ########## #### # # # #
+# # # # # # # # #
+# ####### # # ########## #
+# # # # # # # ? #
+# # # # # # #
+################################
diff --git a/examples/game/player.tm b/examples/game/player.tm
index 5aff9ea5..3461c6ae 100644
--- a/examples/game/player.tm
+++ b/examples/game/player.tm
@@ -10,7 +10,7 @@ struct Player(pos,prev_pos:Vec2):
WALK_SPEED := 500.
ACCEL := 0.3
FRICTION := 0.99
- SIZE := Vec2(50, 50)
+ SIZE := Vec2(30, 30)
func update(p:&Player):
target_x := inline C (
@@ -28,4 +28,4 @@ struct Player(pos,prev_pos:Vec2):
p.prev_pos, p.pos = p.pos, p.pos + World.DT*vel
func draw(p:&Player):
- Color.RED:draw_rectangle(p.pos, Player.SIZE)
+ Color.PLAYER:draw_rectangle(p.pos, Player.SIZE)
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)