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:
Penelope 2025-04-07 22:58:52 -03:00
parent 68a1e19ebe
commit 10fc584d1e
24 changed files with 162 additions and 70 deletions

View File

@ -122,8 +122,11 @@ animations = [{
"speed": 5.0
}]
[node name="Mob" type="Node2D"]
[node name="Mob" type="Node2D" node_paths=PackedStringArray("animator", "collisionRay_1", "collisionRay_2")]
script = ExtResource("1_mu6cs")
animator = NodePath("AnimatedSprite2D")
collisionRay_1 = NodePath("CollisionRayCast_1")
collisionRay_2 = NodePath("CollisionRayCast_2")
[node name="AnimatedSprite2D" type="AnimatedSprite2D" parent="."]
position = Vector2(8, 8)

View File

@ -5,3 +5,6 @@
[node name="Player" instance=ExtResource("1_7y2qh")]
script = ExtResource("2_us7jc")
[node name="Camera2D" type="Camera2D" parent="." index="3"]
zoom = Vector2(2, 2)

File diff suppressed because one or more lines are too long

View File

@ -1,26 +1,21 @@
[gd_scene load_steps=7 format=3 uid="uid://c0b5w48jk67qd"]
[gd_scene load_steps=8 format=3 uid="uid://c0b5w48jk67qd"]
[ext_resource type="Script" uid="uid://c0uvlwmkm3r8o" path="res://scripts/game/game_screen.gd" id="1_uwrxv"]
[ext_resource type="Script" uid="uid://dcqqr8b42tn0x" path="res://scripts/game/CommandProcessor.gd" id="2_lbhrr"]
[ext_resource type="Texture2D" uid="uid://ccxs3ctob15es" path="res://gfx/ui/main_frame.png" id="2_lnu2h"]
[ext_resource type="FontFile" uid="uid://1u28cjgctsn7" path="res://fonts/alagard_by_pix3m-d6awiwp.ttf" id="3_lbhrr"]
[ext_resource type="Script" uid="uid://b63jt1uexm120" path="res://scripts/game/maps/MapContainer.gd" id="3_p57ef"]
[ext_resource type="Script" uid="uid://dv3fd112uj8o1" path="res://scripts/ui/Message Console.gd" id="5_iywne"]
[ext_resource type="Script" uid="uid://c0uvlwmkm3r8o" path="res://scripts/game/game_screen.gd" id="1_dthvl"]
[ext_resource type="Script" uid="uid://dcqqr8b42tn0x" path="res://scripts/game/CommandProcessor.gd" id="2_r01d7"]
[ext_resource type="Script" uid="uid://b63jt1uexm120" path="res://scripts/game/maps/MapContainer.gd" id="3_vnpv3"]
[ext_resource type="Script" uid="uid://bjic7g285omo8" path="res://scripts/ui/UI.gd" id="4_4m8lb"]
[ext_resource type="Texture2D" uid="uid://ccxs3ctob15es" path="res://gfx/ui/main_frame.png" id="5_mjl5p"]
[ext_resource type="FontFile" uid="uid://1u28cjgctsn7" path="res://fonts/alagard_by_pix3m-d6awiwp.ttf" id="6_2yu03"]
[ext_resource type="Script" uid="uid://dv3fd112uj8o1" path="res://scripts/ui/Message Console.gd" id="7_5vo8s"]
[node name="Game" type="Node" node_paths=PackedStringArray("map")]
script = ExtResource("1_uwrxv")
map = NodePath("MapContainer")
script = ExtResource("1_dthvl")
map = NodePath("UI/Map View/SubViewport/MapContainer")
[node name="Command Processor" type="Timer" parent="."]
wait_time = 3.0
autostart = true
script = ExtResource("2_lbhrr")
[node name="MapContainer" type="Node" parent="." node_paths=PackedStringArray("entities")]
script = ExtResource("3_p57ef")
entities = NodePath("Entities")
[node name="Entities" type="Node" parent="MapContainer"]
script = ExtResource("2_r01d7")
[node name="UI" type="Control" parent="."]
layout_mode = 3
@ -29,6 +24,7 @@ anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
script = ExtResource("4_4m8lb")
[node name="Background Frame" type="TextureRect" parent="UI"]
layout_mode = 1
@ -37,7 +33,7 @@ anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
texture = ExtResource("2_lnu2h")
texture = ExtResource("5_mjl5p")
[node name="Left Display" type="MarginContainer" parent="UI"]
layout_mode = 0
@ -58,7 +54,7 @@ layout_mode = 2
[node name="Pass Button" type="Button" parent="UI/Left Display/Contents/Command Menu"]
layout_mode = 2
theme_override_fonts/font = ExtResource("3_lbhrr")
theme_override_fonts/font = ExtResource("6_2yu03")
text = "Pass"
[node name="Message Console" type="MarginContainer" parent="UI" node_paths=PackedStringArray("scrollContainer", "messages")]
@ -67,7 +63,7 @@ offset_left = 13.0
offset_top = 231.0
offset_right = 627.0
offset_bottom = 345.0
script = ExtResource("5_iywne")
script = ExtResource("7_5vo8s")
scrollContainer = NodePath("ScrollContainer")
messages = NodePath("ScrollContainer/Messages")
@ -83,6 +79,24 @@ horizontal_scroll_mode = 0
layout_mode = 2
size_flags_horizontal = 3
[node name="Map View" type="SubViewportContainer" parent="UI"]
layout_mode = 0
offset_left = 181.0
offset_top = 15.0
offset_right = 627.0
offset_bottom = 216.0
stretch = true
[node name="SubViewport" type="SubViewport" parent="UI/Map View"]
size = Vector2i(446, 201)
[node name="MapContainer" type="Node" parent="UI/Map View/SubViewport" node_paths=PackedStringArray("entities")]
script = ExtResource("3_vnpv3")
entities = NodePath("Entities")
[node name="Entities" type="Node2D" parent="UI/Map View/SubViewport/MapContainer"]
z_index = 1
[connection signal="BROADCAST_COMMAND" from="Command Processor" to="UI/Message Console" method="_on_command"]
[connection signal="timeout" from="Command Processor" to="Command Processor" method="_on_timeout"]
[connection signal="pressed" from="UI/Left Display/Contents/Command Menu/Pass Button" to="." method="_on_pass_button_pressed"]
[connection signal="pressed" from="UI/Left Display/Contents/Command Menu/Pass Button" to="UI" method="_on_pass_button_pressed"]

View File

@ -4,7 +4,7 @@
[ext_resource type="Script" uid="uid://bklj1ua1qok02" path="res://scripts/title/title_screen.gd" id="1_r23qo"]
[ext_resource type="FontFile" uid="uid://1u28cjgctsn7" path="res://fonts/alagard_by_pix3m-d6awiwp.ttf" id="2_aqpy5"]
[ext_resource type="Theme" uid="uid://dnxny0n7ti7eq" path="res://themes/default_theme.tres" id="3_w8uqo"]
[ext_resource type="Script" uid="uid://bfc7ybsw6riri" path="res://scripts/title/version_number.gd" id="4_a4ium"]
[ext_resource type="Script" uid="uid://bfc7ybsw6riri" path="res://scripts/title/Version Number.gd" id="4_a4ium"]
[sub_resource type="LabelSettings" id="LabelSettings_w8uqo"]
font = ExtResource("2_aqpy5")

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,6 +12,7 @@ func getTerrainDataForTile(layer, data, x, y):
else:
return null
func spawnPlayerAtPosition(position, facing):
player = load("res://scenes/game/maps/entities/player.tscn").instantiate()

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()
add_child(newMap)
map = load(newMapPath).instantiate()
entities.add_child(newMap.spawnPlayerAtPosition(spawnpoint, facing))
add_child(map)
entities.add_child(map.spawnPlayerAtPosition(spawnpoint, facing))
GameManager.currentMap = newMapPath
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