Fixed names for a better traceability of code errors with orignal code lesson. End of lesson 25. Player is moving on the map viewport!

This commit is contained in:
2025-04-07 22:58:52 -03:00
parent 68a1e19ebe
commit 10fc584d1e
24 changed files with 162 additions and 70 deletions

View File

@@ -1,8 +1,8 @@
extends Node
var defaultMapPath:String = "res://scenes/game/maps/world_map.tscn"
var startMap:String = "res://scenes/game/maps/world_map.tscn"
var currentMapPath
var currentMap:String
func startNewGame():
currentMapPath = defaultMapPath
currentMap = startMap

View File

@@ -13,4 +13,4 @@ signal DISPLAY_MESSAGE(message)
signal DISPLAY_COMMAND_PROMPT
signal DISPLAY_CLEAR
signal LOAD_MAP(currentMap, newMap, spawnpoint, facing)
signal LOAD_MAP(currentMap, newMapPath, spawnpoint, facing)

View File

@@ -11,7 +11,7 @@ func _init(dir):
func execute():
CommandDispatcher.PLAYER_MOVE.emit(direction)
emit_signal("COMMAND_PROCESSED", getCommandText())
COMMAND_PROCESSED.emit(getCommandText())
func getDirectionString() -> String:

View File

@@ -5,8 +5,4 @@ class_name GameScreen
@export var map:Node
func _ready():
CommandDispatcher.LOAD_MAP.emit(null, GameManager.defaultMapPath, null, Map.Direction.North)
func _on_pass_button_pressed():
CommandDispatcher.PROCESS_COMMAND.emit(PassCommand.new())
CommandDispatcher.LOAD_MAP.emit(GameManager.startMap, null, Map.Direction.North)

47
scripts/game/maps/Map.gd Normal file
View File

@@ -0,0 +1,47 @@
extends TileMap
class_name Map
enum Direction { North, East, South, West }
@export var defaultPlayerStartPosition:Vector2i
@export var stepIncrement:float = 1.0
var player
func _ready():
CommandDispatcher.PLAYER_MOVE.connect(onPlayerMove)
func spawnPlayerAtPosition(position, facing):
var spawnposition:Vector2i
if (position == null):
spawnposition = defaultPlayerStartPosition
else:
spawnposition = position
player.position = Vector2(spawnposition.x * 16, spawnposition.y * 16)
func onPlayerMove(direction):
var newPos = Vector2(player.position)
var moveIncrement = tile_set.tile_size.x * stepIncrement
match(direction):
Direction.North:
newPos.y -= moveIncrement
Direction.South:
newPos.y += moveIncrement
Direction.West:
newPos.x -= moveIncrement
Direction.East:
newPos.x += moveIncrement
if (playerCanMoveTo(newPos)):
player.position = newPos
player.updateAnimation(direction)
func playerCanMoveTo(position) -> bool:
return false

View File

@@ -12,9 +12,10 @@ func getTerrainDataForTile(layer, data, x, y):
else:
return null
func spawnPlayerAtPosition(position, facing):
player = load("res://scenes/game/maps/entities/player.tscn").instantiate()
super.spawnPlayerAtPosition(position, facing)
return player

View File

@@ -8,16 +8,39 @@ func _ready():
CommandDispatcher.LOAD_MAP.connect(loadMap)
func loadMap(currentMapPath, newMapPath, spawnpoint, facing):
var newMap:Map = load(newMapPath).instantiate()
func loadMap(newMapPath, spawnpoint, facing):
var newMap:Map
CommandDispatcher.PAUSE_PROCESSOR.emit()
if (currentMapPath != null):
currentMapPath.queue_free()
if (map != null):
map.queue_free()
map = load(newMapPath).instantiate()
add_child(map)
entities.add_child(map.spawnPlayerAtPosition(spawnpoint, facing))
GameManager.currentMap = newMapPath
add_child(newMap)
entities.add_child(newMap.spawnPlayerAtPosition(spawnpoint, facing))
CommandDispatcher.WAIT_FOR_COMMAND.emit()
func _unhandled_key_input(event):
var direction
if (Input.is_action_pressed("ui_right")):
direction = Map.Direction.East
if (Input.is_action_pressed("ui_left")):
direction = Map.Direction.West
if (Input.is_action_pressed("ui_up")):
direction = Map.Direction.North
if (Input.is_action_pressed("ui_down")):
direction = Map.Direction.South
if (direction != null):
CommandDispatcher.PROCESS_COMMAND.emit(MoveCommand.new(direction))

View File

@@ -0,0 +1,20 @@
extends Map2d
class_name WorldMap
func playerCanMoveTo(position:Vector2) -> bool:
var tilesize = tile_set.tile_size.x
var cast_cell_1
var cast_cell_2
player.updateRaycasts(position)
cast_cell_1 = local_to_map(position + player.collisionRay_1.position)
cast_cell_2 = local_to_map(position + player.collisionRay_2.position)
if (getTerrainDataForTile(0, TerrainDataTypes.TerrainType, cast_cell_1.x, cast_cell_1.y) != "Water" and
getTerrainDataForTile(0, TerrainDataTypes.TerrainType, cast_cell_2.x, cast_cell_2.y) != "Water"):
return true
else:
CommandDispatcher.DISPLAY_MESSAGE.emit("Your path is blocked.")
return false

View File

@@ -1,19 +0,0 @@
extends TileMap
class_name Map
enum Direction { North, East, South, West }
@export var defaultPlayerStartPosition:Vector2i
var player
func spawnPlayerAtPosition(position, facing):
var spawnposition
if (position == null):
spawnposition = defaultPlayerStartPosition
else:
spawnposition = position
player.position = Vector2(spawnposition.x * 16, spawnposition.y * 16)

View File

@@ -1,4 +0,0 @@
extends Map2d
#func _ready() -> void:
# super._ready()

View File

@@ -0,0 +1,4 @@
extends Label
func _ready():
text = text + ProjectSettings.get_setting("application/config/version")

View File

@@ -4,4 +4,4 @@ extends Control
func _on_new_game_button_pressed():
GameManager.startNewGame()
Loader.loadScene(self, "res://scenes/game.tscn")
Loader.loadScene(self, "res://scenes/game_screen.tscn")

View File

@@ -1,4 +0,0 @@
extends Label
func _ready():
text += ProjectSettings.get_setting("application/config/version")

6
scripts/ui/UI.gd Normal file
View File

@@ -0,0 +1,6 @@
extends Control
class_name UI
func _on_pass_button_pressed():
CommandDispatcher.PROCESS_COMMAND.emit(PassCommand.new())

1
scripts/ui/UI.gd.uid Normal file
View File

@@ -0,0 +1 @@
uid://bjic7g285omo8