Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update the TileMap code in the RPG demo to work with Godot 4.2 #1009

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 14 additions & 11 deletions 2d/role_playing_game/Game.gd
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,29 @@ extends Node
const PLAYER_WIN = "res://dialogue/dialogue_data/player_won.json"
const PLAYER_LOSE = "res://dialogue/dialogue_data/player_lose.json"

@onready var combat_node = get_node(combat_screen);
@onready var exploration_node = get_node(exploration_screen);


func _ready():
exploration_screen = get_node(exploration_screen)
combat_screen = get_node(combat_screen)
combat_screen.combat_finished.connect(self._on_combat_finished)
combat_node.combat_finished.connect(self._on_combat_finished)
for n in $Exploration/Grid.get_children():
if not n.type == n.CellType.ACTOR:
continue
if not n.has_node("DialoguePlayer"):
continue
print(n.get_node(^"DialoguePlayer"))
n.get_node(^"DialoguePlayer").dialogue_finished.connect(self._on_opponent_dialogue_finished.bind(n))
remove_child(combat_screen)
remove_child(combat_node)


func start_combat(combat_actors):
remove_child($Exploration)
$AnimationPlayer.play("fade")
await $AnimationPlayer.animation_finished
add_child(combat_screen)
combat_screen.show()
combat_screen.initialize(combat_actors)
add_child(combat_node)
combat_node.show()
combat_node.initialize(combat_actors)
$AnimationPlayer.play_backwards("fade")


Expand All @@ -38,17 +41,17 @@ func _on_opponent_dialogue_finished(opponent):


func _on_combat_finished(winner, _loser):
remove_child(combat_screen)
remove_child(combat_node)
$AnimationPlayer.play_backwards("fade")
add_child(exploration_screen)
add_child(exploration_node)
var dialogue = load("res://dialogue/dialogue_player/DialoguePlayer.tscn").instantiate()
if winner.name == "Player":
dialogue.dialogue_file = PLAYER_WIN
else:
dialogue.dialogue_file = PLAYER_LOSE
await $AnimationPlayer.animation_finished
var player = $Exploration/Grid/Player
exploration_screen.get_node(^"DialogueUI").show_dialogue(player, dialogue)
combat_screen.clear_combat()
exploration_node.get_node(^"DialogueUI").show_dialogue(player, dialogue)
combat_node.clear_combat()
await dialogue.dialogue_finished
dialogue.queue_free()
4 changes: 2 additions & 2 deletions 2d/role_playing_game/Game.tscn
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[gd_scene load_steps=6 format=3 uid="uid://dlec1sr72eb72"]

[ext_resource type="Script" path="res://Game.gd" id="1"]
[ext_resource type="PackedScene" path="res://combat/Combat.tscn" id="2"]
[ext_resource type="PackedScene" path="res://grid_movement/Exploration.tscn" id="3"]
[ext_resource type="PackedScene" uid="uid://1a847rc1yude" path="res://combat/Combat.tscn" id="2"]
[ext_resource type="PackedScene" uid="uid://343t45ghceqo" path="res://grid_movement/Exploration.tscn" id="3"]

[sub_resource type="Animation" id="1"]
length = 0.5
Expand Down
51 changes: 21 additions & 30 deletions 2d/role_playing_game/combat/Combat.tscn
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
[gd_scene load_steps=7 format=2]
[gd_scene load_steps=7 format=3 uid="uid://1a847rc1yude"]

[ext_resource path="res://combat/Combat.gd" type="Script" id=1]
[ext_resource path="res://combat/TurnQueue.gd" type="Script" id=2]
[ext_resource path="res://theme/theme.tres" type="Theme" id=3]
[ext_resource path="res://combat/interface/UI.gd" type="Script" id=4]
[ext_resource path="res://combat/interface/Info.tscn" type="PackedScene" id=5]
[ext_resource type="Script" path="res://combat/Combat.gd" id="1"]
[ext_resource type="Script" path="res://combat/TurnQueue.gd" id="2"]
[ext_resource type="Theme" uid="uid://dh2px4xx6k8ga" path="res://theme/theme.tres" id="3"]
[ext_resource type="Script" path="res://combat/interface/UI.gd" id="4"]
[ext_resource type="PackedScene" path="res://combat/interface/Info.tscn" id="5"]

[sub_resource type="GDScript" id=1]
[sub_resource type="GDScript" id="1"]
script/source = "extends Node2D

func add_combatant(new_combatant):
Expand All @@ -15,67 +15,58 @@ func add_combatant(new_combatant):
"

[node name="Combat" type="Node2D"]
script = ExtResource( 1 )
script = ExtResource("1")

[node name="Combatants" type="Node2D" parent="."]
position = Vector2(539, 275)
script = SubResource( 1 )
script = SubResource("1")

[node name="TurnQueue" type="Node" parent="."]
script = ExtResource( 2 )
script = ExtResource("2")
combatants_list = NodePath("../Combatants")

[node name="UI" type="Control" parent="."]
layout_mode = 3
anchors_preset = 0
offset_right = 1280.0
offset_bottom = 720.0
theme = ExtResource( 3 )
script = ExtResource( 4 )
__meta__ = {
"_edit_use_anchors_": false
}
combatants_node = NodePath("../Combatants")
info_scene = ExtResource( 5 )
theme = ExtResource("3")
script = ExtResource("4")
info_scene = ExtResource("5")

[node name="Combatants" type="HBoxContainer" parent="UI"]
layout_mode = 0
offset_top = 77.0
offset_right = 1280.0
offset_bottom = 328.0
custom_constants/separation = 360

[node name="Buttons" type="PanelContainer" parent="UI"]
layout_mode = 0
offset_left = 80.0
offset_top = 376.0
offset_right = 1200.0
offset_bottom = 698.0

[node name="GridContainer" type="GridContainer" parent="UI/Buttons"]
offset_left = 90.0
offset_top = 35.0
offset_right = 1030.0
offset_bottom = 277.0
layout_mode = 2
size_flags_horizontal = 3
size_flags_vertical = 3
columns = 2

[node name="Attack" type="Button" parent="UI/Buttons/GridContainer"]
offset_right = 468.0
offset_bottom = 119.0
layout_mode = 2
size_flags_horizontal = 3
size_flags_vertical = 3
text = "Attack"

[node name="Defend" type="Button" parent="UI/Buttons/GridContainer"]
offset_left = 472.0
offset_right = 940.0
offset_bottom = 119.0
layout_mode = 2
size_flags_horizontal = 3
size_flags_vertical = 3
text = "Defend"

[node name="Flee" type="Button" parent="UI/Buttons/GridContainer"]
offset_top = 123.0
offset_right = 468.0
offset_bottom = 242.0
layout_mode = 2
size_flags_horizontal = 3
size_flags_vertical = 3
text = "Flee"
Expand Down
10 changes: 6 additions & 4 deletions 2d/role_playing_game/combat/TurnQueue.gd
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
extends Node


@export var combatants_list: NodePath

@onready var combatants_list_node = get_node(combatants_list)

var queue = []:
set(value):
# TODO: Manually copy the code from this method.
set_queue(value)

var active_combatant = null:
set(value):
# TODO: Manually copy the code from this method.
_set_active_combatant(value)

signal active_combatant_changed(active_combatant)

func _ready():
combatants_list = get_node(combatants_list)


func initialize():
set_queue(combatants_list.get_children())
set_queue(combatants_list_node.get_children())
play_turn()


Expand Down
6 changes: 3 additions & 3 deletions 2d/role_playing_game/combat/interface/UI.gd
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
extends Control

@export var combatants_node: NodePath

@export var combatants_node_path: NodePath
@export var info_scene: PackedScene

func _ready():
combatants_node = get_node(combatants_node)
@onready var combatants_node = get_node(combatants_node_path)


func initialize():
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
extends Node

@export var dialogue_file: String, FILE, "*.json"

@export_file("*.json") var dialogue_file

var dialogue_keys = []
var dialogue_name = ""
var current = 0
Expand Down Expand Up @@ -34,9 +36,8 @@ func index_dialogue():


func load_dialogue(file_path):
var file = File.new()
if file.file_exists(file_path):
file.open(file_path, file.READ)
if FileAccess.file_exists(file_path):
var file = FileAccess.open(file_path, FileAccess.READ)
var json = JSON.new()
json.parse(file.get_as_text())
var dialogue = json.get_data()
Expand Down
57 changes: 26 additions & 31 deletions 2d/role_playing_game/grid_movement/Exploration.tscn

Large diffs are not rendered by default.

15 changes: 7 additions & 8 deletions 2d/role_playing_game/grid_movement/grid/Grid.gd
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,26 @@ enum CellType { ACTOR, OBSTACLE, OBJECT }

func _ready():
for child in get_children():
set_cellv(world_to_map(child.position), child.type)

set_cell(0, local_to_map(child.position), child.type)

func get_cell_pawn(cell, type = CellType.ACTOR):
for node in get_children():
if node.type != type:
continue
if world_to_map(node.position) == cell:
if local_to_map(node.position) == cell:
return(node)


func request_move(pawn, direction):
var cell_start = world_to_map(pawn.position)
var cell_start = local_to_map(pawn.position)
var cell_target = cell_start + direction

var cell_tile_id = get_cellv(cell_target)
var cell_tile_id = get_cell_source_id(0, cell_target)
match cell_tile_id:
-1:
set_cellv(cell_target, CellType.ACTOR)
set_cellv(cell_start, -1)
return map_to_world(cell_target) + cell_size / 2
set_cell(0, cell_target, CellType.ACTOR)
set_cell(0, cell_start, -1)
return map_to_local(cell_target) + Vector2(tile_set.tile_size.x / 2, tile_set.tile_size.y / 2)
CellType.OBJECT, CellType.ACTOR:
var target_pawn = get_cell_pawn(cell_target, cell_tile_id)
print("Cell %s contains %s" % [cell_target, target_pawn.name])
Expand Down
4 changes: 2 additions & 2 deletions 2d/role_playing_game/project.godot
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ config/name="JRPG Demo"
config/description="This shows a method of creating grid-based movement with Godot
and GDScript. It also includes a simple JRPG-style dialogue and
battle system on top of it."
config/tags=PackedStringArray("2d", "demo", "gui", "official", "tilemap")
run/main_scene="res://Game.tscn"
config/features=PackedStringArray("4.0")
config/features=PackedStringArray("4.2")
config/icon="res://icon.webp"
config/tags=PackedStringArray("2d", "demo", "gui", "official", "tilemap")

[display]

Expand Down