- Edited
I'm working on Pacman 2.0. Long story short, I went through huge work arounds to get navigation working for the ghost ai, then it was brought to my attention that there is a much simpler way, however, then comes the problem after the simpler route.
I had this mismash that from pacman's global position, finds tile position, finds atlas coordinates of tile at position, checks dictionary, replaces tile if matches dictionary entry. That works fine. It was then I was trying the new navigation route which required a navigation layer to the tiles being replaced, and what they are being replaced with, and I've isolated it down to this. Without the navigation layer in the tileset, pacman flies around eating and scoring with glee. I add the navigation layer for ghost nav, pacman shudders with each dot eaten/tile replaced. Any thoughts?
Here is the tilemap script with the check tile function doing the replacing. It works great, without the navigation layer added to the tiles in the tileset.
extends TileMap
signal initialized
# Access TileMap node.
@onready var tile_map = $/root/BINARY/MAP/TileMap
@onready var pacman = $/root/BINARY/CHARACTERS/PACMAN
func initialize():
if not is_inside_tree():
push_error("TileMap is not in the scene tree.")
return
emit_signal("initialized", self.name)
func _ready():
pass
var tile_replace = {
Vector2i(7, 8): true, # Tile at atlas coordinates (7, 8) needs to be replaced
# Add other tiles as needed
}
var tile_toolbox = {
Vector2i(7, 8): Vector2i(8, 10), # New atlas coordinates to replace the tile at (7, 8)
# Add other tiles as needed
}
var score = 0
var score_display_positions = [
Vector2i(23, 1), Vector2i(24, 1), Vector2i(25, 1),
Vector2i(26, 1), Vector2i(27, 1), Vector2i(28, 1),
Vector2i(29, 1)
]
var tile_digits = {
0: Vector2i(2, 5), 1: Vector2i(2, 6), 2: Vector2i(2, 7), 3: Vector2i(2, 8),
4: Vector2i(1, 9), 5: Vector2i(2, 9), 6: Vector2i(1, 10), 7: Vector2i(2, 10),
8: Vector2i(1, 11), 9: Vector2i(2, 11)
}
func get_atlas_coordinates(tile_pos: Vector2i) -> Vector2i:
return self.get_cell_atlas_coords(0, tile_pos)
func check_tile():
if not tile_map:
pacman.debug_print("TileMap is not initialized!", pacman.DebugLevel.ERROR)
return
var local_pos = tile_map.to_local(pacman.global_position)
var tile_pos = tile_map.local_to_map(local_pos)
var tile_pos_i = Vector2i(floor(tile_pos.x), floor(tile_pos.y))
var atlas_coords = get_atlas_coordinates(tile_pos_i)
if tile_replace.has(atlas_coords):
if tile_toolbox.has(atlas_coords):
var new_atlas_coords = tile_toolbox[atlas_coords]
tile_map.set_cell(0, tile_pos_i, 0, new_atlas_coords)
score += 10
update_score_display()
func display_score_update(digit, position_score):
var atlas_coords = tile_digits[digit]
var position_score_i = Vector2i(round(position_score.x), round(position_score.y))
tile_map.set_cell(0, position_score_i, 0, atlas_coords, false)
tile_map.update_internals()
func update_score_display():
var score_str = str(score).pad_zeros(7)
for i in range(score_display_positions.size()):
var digit_value = int(score_str[i])
var position_display = score_display_positions[i]
display_score_update(digit_value, position_display)
tile_map.update_internals()