Skip to content

Commit

Permalink
feat: add tile chunks from entities.txt
Browse files Browse the repository at this point in the history
Should probably pull the chunks into their own text file
  • Loading branch information
russmatney committed Mar 29, 2024
1 parent 72812a7 commit 1e901fe
Show file tree
Hide file tree
Showing 6 changed files with 165 additions and 26 deletions.
15 changes: 14 additions & 1 deletion addons/brick/GridDef.gd
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,16 @@ func size() -> Vector2i:
func rect() -> Rect2i:
return Rect2i(Vector2i(), size())

func get_shape_dict():
func get_shape_dict(opts={}):
var shape_dict = {}
for coord in coords(false):
shape_dict[Vector2i(coord.coord)] = coord.cell

if opts.get("drop_entity"):
for k in shape_dict.keys():
if shape_dict.get(k) == [opts.get("drop_entity")]:
shape_dict[k] = null

return shape_dict

# Returns an array of dicts like [{"coord": Vector2, "cell": Array[String]}]
Expand All @@ -59,3 +65,10 @@ func coords(skip_nulls=true) -> Array:
else:
crds.append({coord=coord, cell=cell})
return crds

func get_coords_for_entity(ent: String):
var entity_coords = []
for coord in coords():
if ent in coord.cell:
entity_coords.append(Vector2i(coord.coord))
return entity_coords
11 changes: 7 additions & 4 deletions addons/brick/GridDefs.gd
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ func filter(opts: Dictionary):

# only keep if any matching truthy piece of room 'meta' data
if len(opts.flags) > 0:
xs = xs.filter(func(r):
xs = xs.filter(func(x):
for flag in opts.flags:
return r.has_flag(flag))
return x.has_flag(flag))

# skip if any matching flag
if len(opts.skip_flags) > 0:
xs = xs.filter(func(r):
xs = xs.filter(func(x):
for flag in opts.skip_flags:
if r.has_flag(flag):
if x.has_flag(flag):
return false
return true)

Expand All @@ -48,3 +48,6 @@ func filter(opts: Dictionary):

func grids_with_entity(entity_label):
return grids.filter(func(g): return g.has_label(entity_label))

func grids_with_flag(flag):
return grids.filter(func(g): return g.has_flag(flag))
3 changes: 1 addition & 2 deletions project.godot
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ SoundManager="*res://addons/sound_manager/sound_manager.gd"

window/size/viewport_width=1920
window/size/viewport_height=1080
window/stretch/mode.runner="viewport"
window/stretch/aspect.runner="keep"
window/stretch/mode="viewport"

[editor]

Expand Down
88 changes: 88 additions & 0 deletions src/dino/modes/vania/entities.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ l = Leaf
o = Target
p = Player
x = Tile
n = NewTile
- = OneWayPlatform

==============
Expand Down Expand Up @@ -94,3 +95,90 @@ name Leaf on floor
...
.l.
xxx

name Tile
tile_chunk

...
...
...
nnn
nnn
...
...
...
...

name Tile
tile_chunk

.....
.....
.....
.....
.....
nnnnn
nnnnn
.....
.....
.....
.....
xxxxx

name Tile
tile_chunk

.....
.....
.....
.....
nnnnn
.....
.....
.....
.....
xxxxx

name Tile
tile_chunk

...
...
...
...
nnn
nnn
xxx

name Tile
tile_chunk

.....
.....
.....
.....
nnnnn
nnnnn
xxxxx

name Tile
tile_chunk

xxx
nnn
nnn
...
...
...
...

name Tile
tile_chunk

xxxxx
nnnnn
nnnnn
.....
.....
.....
.....
73 changes: 54 additions & 19 deletions src/dino/modes/vania/maps/VaniaRoom.gd
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func _ready():
Log.warn("No room_def on vania room!")

setup_walls_and_doors()
add_tile_chunks()
add_entities()

## setup_walls_and_doors ##############################################################
Expand Down Expand Up @@ -133,15 +134,40 @@ func get_door_cells_to_neighbor(neighbor):
var rect = Reptile.rect_to_local_rect(tilemap, door_rect)
return Reptile.cells_in_rect(rect)

## add_entities ##############################################################
## add_tile_chunks ##############################################################

func build_tilemap_data():
var tmap_data = {}
for cell in Reptile.cells_in_rect(tilemap.get_used_rect()):
tmap_data[cell] = null
for cell in tilemap.get_used_cells(0):
tmap_data[cell] = ["Tile"]
return tmap_data
func add_tile_chunks():
Log.pr("Adding tile chunks")
var grids = room_def.entity_defs.grids_with_flag("tile_chunk")
if grids.is_empty():
Log.warn("No tile chunks!")
return

var tmap_data = build_tilemap_data()

# var count = [1, 2, 3].pick_random()
var count = 3

var tile_coords = []
for i in range(count):
var tile_chunk = grids.pick_random()
var start_coords = possible_positions(tmap_data,
tile_chunk.get_shape_dict({drop_entity="NewTile"}))
var start_coord = start_coords.pick_random()
if start_coord == null:
Log.warn("No position found for tile chunk!", tile_chunk)
# TODO try a different chunk
continue

for e_coord in tile_chunk.get_coords_for_entity("NewTile"):
tile_coords.append(e_coord + start_coord)

tilemap.set_cells_terrain_connect(0, tile_coords, 0, 0)

tilemap.force_update()


## add_entities ##############################################################

func add_entities():
Log.pr("Adding entities", room_def.entities)
Expand All @@ -154,25 +180,34 @@ func add_entities():
var grid = grids.pick_random()

# find place to put entity
var shape_dict = grid.get_shape_dict()
var entity_coords = []
for k in shape_dict.keys():
if shape_dict.get(k) == [ent]:
entity_coords.append(k)
shape_dict[k] = null
var shape_dict = grid.get_shape_dict({drop_entity=ent})

# TODO exclude doorways
var valid_starting_coords = possible_positions(tmap_data, shape_dict)
var start_coords = possible_positions(tmap_data, shape_dict)

for e_coord in entity_coords:
var starting_coord = valid_starting_coords.pick_random()
valid_starting_coords.erase(starting_coord)
for e_coord in grid.get_coords_for_entity(ent):
var start_coord = start_coords.pick_random()
if start_coord == null:
Log.warn("No position found for entity!", ent)
continue
start_coords.erase(start_coord)

var pos = tilemap.map_to_local(e_coord + starting_coord)
# place entity at random start cord
var pos = tilemap.map_to_local(e_coord + start_coord)
var entity = entity_opts.scene.instantiate()
entity.position = pos
add_child(entity)

## fit helpers ##############################################################

func build_tilemap_data():
var tmap_data = {}
for cell in Reptile.cells_in_rect(tilemap.get_used_rect()):
tmap_data[cell] = null
for cell in tilemap.get_used_cells(0):
tmap_data[cell] = ["Tile"]
return tmap_data

func possible_positions(tmap_data, entity_shape):
var positions = []
var rect = Reptile.rect_to_local_rect(tilemap, Rect2(Vector2(), room_instance.get_size()))
Expand Down
1 change: 1 addition & 0 deletions src/dino/modes/vania/maps/VaniaRoom.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ script = ExtResource("1_x4efj")
tile_set = ExtResource("1_r8bn3")
format = 2
layer_0/name = "base"
layer_0/tile_data = PackedInt32Array(1114143, 196609, 4, 1048607, 196609, 3, 983071, 196609, 3, 917535, 196609, 3, 851999, 196609, 3, 786463, 196609, 3, 720927, 196609, 3, 655391, 196609, 3, 589855, 196609, 3, 524319, 196609, 3, 458783, 196609, 3, 393247, 196609, 3, 327711, 196609, 3, 262175, 196609, 3, 196639, 196609, 3, 131103, 196609, 3, 65567, 196609, 3, 31, 196609, 2, 1114142, 131073, 4, 1048606, 131073, 3, 983070, 131073, 3, 917534, 131073, 3, 851998, 131073, 3, 786462, 131073, 3, 720926, 131073, 3, 655390, 131073, 3, 589854, 131073, 3, 524318, 131073, 3, 458782, 131073, 3, 393246, 131073, 3, 327710, 131073, 3, 262174, 131073, 3, 196638, 131073, 3, 131102, 131073, 3, 65566, 131073, 3, 30, 131073, 2, 1114141, 131073, 4, 1048605, 131073, 3, 983069, 131073, 3, 917533, 131073, 3, 851997, 131073, 3, 786461, 131073, 3, 720925, 131073, 3, 655389, 131073, 3, 589853, 131073, 3, 524317, 131073, 3, 458781, 131073, 3, 393245, 131073, 3, 327709, 131073, 3, 262173, 131073, 3, 196637, 131073, 3, 131101, 131073, 3, 65565, 131073, 3, 29, 131073, 2, 1114140, 131073, 4, 1048604, 131073, 3, 983068, 131073, 3, 917532, 458753, 4, 851996, 65537, 3, 786460, 65537, 3, 720924, 65537, 3, 655388, 65537, 3, 589852, 65537, 3, 524316, 65537, 3, 458780, 65537, 3, 393244, 65537, 3, 327708, 65537, 3, 262172, 65537, 3, 196636, 458753, 3, 131100, 131073, 3, 65564, 131073, 3, 28, 131073, 2, 1114139, 131073, 4, 1048603, 131073, 3, 983067, 131073, 3, 917531, 131073, 2, 196635, 131073, 4, 131099, 131073, 3, 65563, 131073, 3, 27, 131073, 2, 1114138, 131073, 4, 1048602, 131073, 3, 983066, 131073, 3, 917530, 131073, 2, 196634, 131073, 4, 131098, 131073, 3, 65562, 131073, 3, 26, 131073, 2, 1114137, 131073, 4, 1048601, 131073, 3, 983065, 131073, 3, 917529, 131073, 2, 196633, 131073, 4, 131097, 131073, 3, 65561, 131073, 3, 25, 131073, 2, 1114136, 131073, 4, 1048600, 131073, 3, 983064, 131073, 3, 917528, 131073, 2, 196632, 131073, 4, 131096, 131073, 3, 65560, 131073, 3, 24, 131073, 2, 1114135, 131073, 4, 1048599, 131073, 3, 983063, 131073, 3, 917527, 131073, 2, 196631, 131073, 4, 131095, 131073, 3, 65559, 131073, 3, 23, 131073, 2, 1114134, 131073, 4, 1048598, 131073, 3, 983062, 131073, 3, 917526, 131073, 2, 196630, 131073, 4, 131094, 131073, 3, 65558, 131073, 3, 22, 131073, 2, 1114133, 131073, 4, 1048597, 131073, 3, 983061, 131073, 3, 917525, 131073, 2, 196629, 131073, 4, 131093, 131073, 3, 65557, 131073, 3, 21, 131073, 2, 1114132, 131073, 4, 1048596, 131073, 3, 983060, 131073, 3, 917524, 131073, 2, 196628, 131073, 4, 131092, 131073, 3, 65556, 131073, 3, 20, 131073, 2, 1114131, 131073, 4, 1048595, 131073, 3, 983059, 131073, 3, 917523, 131073, 2, 196627, 131073, 4, 131091, 131073, 3, 65555, 131073, 3, 19, 131073, 2, 1114130, 131073, 4, 1048594, 131073, 3, 983058, 131073, 3, 917522, 131073, 2, 196626, 131073, 4, 131090, 131073, 3, 65554, 131073, 3, 18, 131073, 2, 1114129, 131073, 4, 1048593, 131073, 3, 983057, 131073, 3, 917521, 131073, 2, 196625, 131073, 4, 131089, 131073, 3, 65553, 131073, 3, 17, 131073, 2, 1114128, 131073, 4, 1048592, 131073, 3, 983056, 131073, 3, 917520, 131073, 2, 196624, 131073, 4, 131088, 131073, 3, 65552, 131073, 3, 16, 131073, 2, 1114127, 131073, 4, 1048591, 131073, 3, 983055, 131073, 3, 917519, 131073, 2, 196623, 131073, 4, 131087, 131073, 3, 65551, 131073, 3, 15, 131073, 2, 1114126, 131073, 4, 1048590, 131073, 3, 983054, 131073, 3, 917518, 131073, 2, 196622, 131073, 4, 131086, 131073, 3, 65550, 131073, 3, 14, 131073, 2, 1114125, 131073, 4, 1048589, 131073, 3, 983053, 131073, 3, 917517, 131073, 2, 196621, 131073, 4, 131085, 131073, 3, 65549, 131073, 3, 13, 131073, 2, 1114124, 131073, 4, 1048588, 131073, 3, 983052, 131073, 3, 917516, 131073, 2, 196620, 131073, 4, 131084, 131073, 3, 65548, 131073, 3, 12, 131073, 2, 1114123, 131073, 4, 1048587, 131073, 3, 983051, 131073, 3, 917515, 131073, 2, 196619, 131073, 4, 131083, 131073, 3, 65547, 131073, 3, 11, 131073, 2, 1114122, 131073, 4, 1048586, 131073, 3, 983050, 131073, 3, 917514, 131073, 2, 196618, 131073, 4, 131082, 131073, 3, 65546, 131073, 3, 10, 131073, 2, 1114121, 131073, 4, 1048585, 131073, 3, 983049, 131073, 3, 917513, 131073, 2, 196617, 131073, 4, 131081, 131073, 3, 65545, 131073, 3, 9, 131073, 2, 1114120, 131073, 4, 1048584, 131073, 3, 983048, 131073, 3, 917512, 131073, 2, 196616, 131073, 4, 131080, 131073, 3, 65544, 131073, 3, 8, 131073, 2, 1114119, 131073, 4, 1048583, 131073, 3, 983047, 131073, 3, 917511, 131073, 2, 196615, 131073, 4, 131079, 131073, 3, 65543, 131073, 3, 7, 131073, 2, 1114118, 131073, 4, 1048582, 131073, 3, 983046, 131073, 3, 917510, 131073, 2, 196614, 131073, 4, 131078, 131073, 3, 65542, 131073, 3, 6, 131073, 2, 1114117, 131073, 4, 1048581, 131073, 3, 983045, 131073, 3, 917509, 131073, 2, 196613, 131073, 4, 131077, 131073, 3, 65541, 131073, 3, 5, 131073, 2, 1114116, 131073, 4, 1048580, 131073, 3, 983044, 131073, 3, 917508, 131073, 2, 196612, 131073, 4, 131076, 131073, 3, 65540, 131073, 3, 4, 131073, 2, 1114115, 131073, 4, 1048579, 131073, 3, 983043, 131073, 3, 917507, 393217, 4, 851971, 196609, 3, 786435, 196609, 3, 720899, 196609, 3, 655363, 196609, 3, 589827, 196609, 3, 524291, 196609, 3, 458755, 196609, 3, 393219, 196609, 3, 327683, 196609, 3, 262147, 196609, 3, 196611, 393217, 3, 131075, 131073, 3, 65539, 131073, 3, 3, 131073, 2, 1114114, 131073, 4, 1048578, 131073, 3, 983042, 131073, 3, 917506, 131073, 3, 851970, 131073, 3, 786434, 131073, 3, 720898, 131073, 3, 655362, 131073, 3, 589826, 131073, 3, 524290, 131073, 3, 458754, 131073, 3, 393218, 131073, 3, 327682, 131073, 3, 262146, 131073, 3, 196610, 131073, 3, 131074, 131073, 3, 65538, 131073, 3, 2, 131073, 2, 1114113, 131073, 4, 1048577, 131073, 3, 983041, 131073, 3, 917505, 131073, 3, 851969, 131073, 3, 786433, 131073, 3, 720897, 131073, 3, 655361, 131073, 3, 589825, 131073, 3, 524289, 131073, 3, 458753, 131073, 3, 393217, 131073, 3, 327681, 131073, 3, 262145, 131073, 3, 196609, 131073, 3, 131073, 131073, 3, 65537, 131073, 3, 1, 131073, 2, 1114112, 65537, 4, 1048576, 65537, 3, 983040, 65537, 3, 917504, 65537, 3, 851968, 65537, 3, 786432, 65537, 3, 720896, 65537, 3, 655360, 65537, 3, 589824, 65537, 3, 524288, 65537, 3, 458752, 65537, 3, 393216, 65537, 3, 327680, 65537, 3, 262144, 65537, 3, 196608, 65537, 3, 131072, 65537, 3, 65536, 65537, 3, 0, 65537, 2)

[node name="RoomInstance" parent="." instance=ExtResource("2_1wsgr")]

Expand Down

0 comments on commit 1e901fe

Please sign in to comment.