Turn into map

This commit is contained in:
Bruce Hill 2024-09-08 20:25:06 -04:00
parent 9f7f4eb858
commit f33b7048d7
5 changed files with 43 additions and 28 deletions

View File

@ -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)

View File

@ -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 {

View File

@ -1,17 +1,18 @@
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
[] [] []
[] [] []
[] [] []
[] [] []
[] [] []
[] [] [] []
[] @@ [] []
[] [] []
[] [] []
[] [] []
[] [] []
[] [] []
[] [] []
[] [] []
[] [] []
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
################################
#@ # # #
# #### #### # # #### #####
# # # # # #
# ####### # ####### # # ###
# # # # # # #
# #### ########## # #### #
# # # # # # #
#### # # # # # # # # ##
# # # # # # # #
####### ########## # ########
# # # # #
# ########## #### # # # #
# # # # # # # # #
# ####### # # ########## #
# # # # # # # ? #
# # # # # # #
################################

View File

@ -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)

View File

@ -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)