Compare commits

..

6 Commits

13 changed files with 1525 additions and 17 deletions

View File

@@ -2,23 +2,65 @@ extends Control
var grid: AStarGrid2D: var grid: AStarGrid2D:
set(v): grid = v; queue_redraw() set(v): grid = v; queue_redraw()
var show_grid_display: bool: var unit_grid: AStarGrid2D:
set(v): unit_grid = v; queue_redraw()
@export var show_grid_display: bool:
set(v): show_grid_display = v; queue_redraw() set(v): show_grid_display = v; queue_redraw()
@export var show_move_range: bool:
set(v): show_move_range = v; queue_redraw()
func toggle_grid_display(on: bool): func toggle_grid_display(on: bool):
grid = MapGlobal.GetGrid() grid = MapGlobal.GetGrid()
show_grid_display = on show_grid_display = on
func toggle_move_range(on: bool):
unit_grid = get_parent().get_parent().get_node("Player").GetMoveGrid()
show_move_range = on
func getNewGrid(): func getNewGrid():
grid = MapGlobal.GetGrid() grid = MapGlobal.GetGrid()
func drawMoveRange():
var walkableCells: Array[Vector2] = []
var activeUnit = get_parent().get_parent().get_node("Player")
var unitCellPosition = activeUnit.pos_to_cell(activeUnit.global_position)
var unitRange = activeUnit.unitData["MoveRange"]
# max right, down, left, up
for x in range(unitCellPosition.x, unitCellPosition.x + unitRange): # right +x
walkableCells.append(Vector2(x, unitCellPosition.y))
for y in range(unitCellPosition.y, unitCellPosition.y + unitRange): # down +y
walkableCells.append(Vector2(unitCellPosition.x, y))
for x in range(unitCellPosition.x, unitCellPosition.x - unitRange, -1): # left -x
walkableCells.append(Vector2(x, unitCellPosition.y))
for y in range(unitCellPosition.x, unitCellPosition.y - unitRange, -1): # up -y
walkableCells.append(Vector2(unitCellPosition.x, y))
# diagonals?
for x in unit_grid.region.size.x:
for y in unit_grid.region.size.y:
var p = Vector2(x + unit_grid.region.position.x, y + unit_grid.region.position.y)
var col = Color(1,0,0,0.3) if unit_grid.is_point_solid(p) else Color(0,1,0,0.3)
draw_rect(Rect2(p*unit_grid.cell_size, unit_grid.cell_size), col)
for cell in walkableCells:
draw_rect(Rect2(cell*grid.cell_size, grid.cell_size), Color(0, 0, 1, 0.5))
func _draw(): func _draw():
if not grid or not show_grid_display: return if unit_grid and show_move_range:
drawMoveRange()
if grid and show_grid_display:
drawNavigationGrid()
func drawNavigationGrid():
for x in grid.region.size.x: for x in grid.region.size.x:
for y in grid.region.size.y: for y in grid.region.size.y:
var p = Vector2(x + grid.region.position.x, y + grid.region.position.y) var p := Vector2(x + grid.region.position.x, y + grid.region.position.y)
var col = Color(1,0,0,0.3) if grid.is_point_solid(p) else Color(0,1,0,0.3) var col := Color(1,0,0,0.3) if grid.is_point_solid(p) else Color(0,1,0,0.3)
draw_rect(Rect2(p*grid.cell_size, grid.cell_size), col) draw_rect(Rect2(p*grid.cell_size, grid.cell_size), col)
func _on_player_move_finished() -> void: func _on_player_move_finished() -> void:

View File

@@ -20,3 +20,9 @@ func _getMousePosition(event: InputEvent):
func get_cell_information(cell): func get_cell_information(cell):
print(cell) print(cell)
func ResetLevel():
get_tree().reload_current_scene()
func ResetPlayer():
$Player.reset(Vector2(50, 50))

View File

@@ -38,6 +38,7 @@ position = Vector2(10, -10)
wait_time = 0.1 wait_time = 0.1
[node name="Sprite2D" type="Sprite2D" parent="."] [node name="Sprite2D" type="Sprite2D" parent="."]
visible = false
position = Vector2(50, 50) position = Vector2(50, 50)
scale = Vector2(2, 2) scale = Vector2(2, 2)
texture = SubResource("PlaceholderTexture2D_y3v7k") texture = SubResource("PlaceholderTexture2D_y3v7k")
@@ -51,6 +52,7 @@ anchors_preset = 0
offset_right = 40.0 offset_right = 40.0
offset_bottom = 40.0 offset_bottom = 40.0
script = ExtResource("8_y3v7k") script = ExtResource("8_y3v7k")
show_grid_display = true
[node name="MarginContainer" type="MarginContainer" parent="HUD/GridDisplay"] [node name="MarginContainer" type="MarginContainer" parent="HUD/GridDisplay"]
layout_mode = 0 layout_mode = 0
@@ -60,13 +62,52 @@ offset_bottom = 40.0
[node name="PanelContainer" type="PanelContainer" parent="HUD/GridDisplay/MarginContainer"] [node name="PanelContainer" type="PanelContainer" parent="HUD/GridDisplay/MarginContainer"]
layout_mode = 2 layout_mode = 2
[node name="ShowGrid" type="CheckBox" parent="HUD/GridDisplay/MarginContainer/PanelContainer"] [node name="VBoxContainer" type="HBoxContainer" parent="HUD/GridDisplay/MarginContainer/PanelContainer"]
layout_mode = 2
[node name="ShowGrid" type="CheckBox" parent="HUD/GridDisplay/MarginContainer/PanelContainer/VBoxContainer"]
layout_mode = 2 layout_mode = 2
text = "Show Grid" text = "Show Grid"
[node name="ShowMove" type="CheckBox" parent="HUD/GridDisplay/MarginContainer/PanelContainer/VBoxContainer"]
layout_mode = 2
text = "Show Movement Range"
[node name="GameControl" type="Control" parent="HUD"]
layout_mode = 3
anchors_preset = 2
anchor_top = 1.0
anchor_bottom = 1.0
offset_top = -40.0
offset_right = 40.0
grow_vertical = 0
size_flags_horizontal = 0
size_flags_vertical = 8
[node name="MarginContainer" type="MarginContainer" parent="HUD/GameControl"]
layout_mode = 0
offset_right = 40.0
offset_bottom = 40.0
[node name="PanelContainer" type="PanelContainer" parent="HUD/GameControl/MarginContainer"]
layout_mode = 2
[node name="VBoxContainer" type="HBoxContainer" parent="HUD/GameControl/MarginContainer/PanelContainer"]
layout_mode = 2
[node name="ResetLevel" type="Button" parent="HUD/GameControl/MarginContainer/PanelContainer/VBoxContainer"]
layout_mode = 2
text = "Reset Level"
[node name="ResetPlayer" type="Button" parent="HUD/GameControl/MarginContainer/PanelContainer/VBoxContainer"]
layout_mode = 2
text = "Reset Player"
[node name="Player" parent="." instance=ExtResource("9_hryqi")] [node name="Player" parent="." instance=ExtResource("9_hryqi")]
position = Vector2(8, 8) position = Vector2(150, 150)
[connection signal="accept_pressed" from="GameBoard/Cursor" to="." method="get_cell_information"] [connection signal="accept_pressed" from="GameBoard/Cursor" to="." method="get_cell_information"]
[connection signal="toggled" from="HUD/GridDisplay/MarginContainer/PanelContainer/ShowGrid" to="HUD/GridDisplay" method="toggle_grid_display"] [connection signal="toggled" from="HUD/GridDisplay/MarginContainer/PanelContainer/VBoxContainer/ShowGrid" to="HUD/GridDisplay" method="toggle_grid_display"]
[connection signal="MoveFinished" from="Player" to="HUD/GridDisplay" method="_on_player_move_finished"] [connection signal="toggled" from="HUD/GridDisplay/MarginContainer/PanelContainer/VBoxContainer/ShowMove" to="HUD/GridDisplay" method="toggle_move_range"]
[connection signal="pressed" from="HUD/GameControl/MarginContainer/PanelContainer/VBoxContainer/ResetLevel" to="." method="ResetLevel"]
[connection signal="pressed" from="HUD/GameControl/MarginContainer/PanelContainer/VBoxContainer/ResetPlayer" to="." method="ResetPlayer"]

View File

@@ -1,8 +1,9 @@
extends CharacterBody2D extends CharacterBody2D
@export var unitData = { @export var unitData: Dictionary[String, Variant] = {
"MoveRange": 5, "MoveRange": 5,
"MoveRangeV": Vector2i(5, 5), "MoveRangeVi": Vector2i(5, 5),
"MoveRangeV": Vector2(5, 5),
"Speed": 400.0 "Speed": 400.0
} }
@@ -10,9 +11,14 @@ var grid: AStarGrid2D
var currentCell: Vector2i var currentCell: Vector2i
var currentPoint: int var currentPoint: int
var moveGrid: AStarGrid2D
var selected: bool: var selected: bool:
set(v): set(v):
selected = v; $PathPrev.visible = selected; if selected: MapGlobal.SetRegion(calculateMovementRegion()) selected = v;
$PathPrev.visible = selected;
if selected: setMoveGrid();
# if selected: MapGlobal.SetRegion(calculateMovementRegion())
var moving: bool: var moving: bool:
set(v): set(v):
moving = v; $PathPrev.visible = not moving; set_physics_process(moving) moving = v; $PathPrev.visible = not moving; set_physics_process(moving)
@@ -31,6 +37,16 @@ func setup(_grid: AStarGrid2D):
grid = _grid grid = _grid
currentCell = pos_to_cell(global_position) currentCell = pos_to_cell(global_position)
targetCell = currentCell targetCell = currentCell
setMoveGrid()
func reset(pos: Vector2):
stopMove()
global_position = pos
currentCell = pos_to_cell(global_position)
targetCell = currentCell
movePts = []
$PathPrev.points = movePts
currentPoint = 0
func pos_to_cell(pos: Vector2): func pos_to_cell(pos: Vector2):
return pos / Vector2(MapGlobal.getData()["cellSize"]) return pos / Vector2(MapGlobal.getData()["cellSize"])
@@ -43,6 +59,7 @@ func _input(event: InputEvent) -> void:
func setTarget(target: Vector2i): func setTarget(target: Vector2i):
if !selected: return if !selected: return
if moving: return if moving: return
if currentCell.distance_to(target) > unitData["MoveRange"]: return
if target != targetCell: if target != targetCell:
print("Setting target: ") print("Setting target: ")
movePts = MapGlobal.GetGrid().get_point_path(currentCell, target) movePts = MapGlobal.GetGrid().get_point_path(currentCell, target)
@@ -55,6 +72,10 @@ func setTarget(target: Vector2i):
func startMove(): func startMove():
if movePts.is_empty(): return if movePts.is_empty(): return
currentPoint = 0; moving = true currentPoint = 0; moving = true
func stopMove():
if !moving: return
moving = false
func _physics_process(delta: float) -> void: func _physics_process(delta: float) -> void:
if currentPoint == movePts.size() - 1: if currentPoint == movePts.size() - 1:
@@ -63,7 +84,7 @@ func _physics_process(delta: float) -> void:
currentCell = pos_to_cell(global_position) currentCell = pos_to_cell(global_position)
$PathPrev.points = []; $PathPrev.points = [];
moving = false; MoveFinished.emit(); moving = false; MoveFinished.emit();
MapGlobal.SetRegion(calculateMovementRegion(), unitData["MoveRangeV"]) #MapGlobal.SetRegion(calculateMovementRegion(), -unitData["MoveRangeVi"])
else: else:
var direction = (movePts[currentPoint+1] - movePts[currentPoint]).normalized() var direction = (movePts[currentPoint+1] - movePts[currentPoint]).normalized()
@@ -79,7 +100,15 @@ func selectUnit(cell: Vector2i):
else: else:
selected = false selected = false
func calculateMovementRegion(): func setMoveGrid():
var region := Rect2i(pos_to_cell(global_position), Vector2i(unitData["MoveRange"], unitData["MoveRange"])) if not grid: return
region.set_center moveGrid = grid
return region
# [moveRange]-tiles in each direction + center
var gridSize = unitData["MoveRange"] * 2 + 1
# moveGrid.set_size(Vector2i(gridSize, gridSize))
moveGrid.region = (Rect2i(global_position, Vector2i(gridSize, gridSize)))
moveGrid.offset = unitData["MoveRangeV"]
func GetMoveGrid() -> AStarGrid2D:
return moveGrid

View File

@@ -9,8 +9,14 @@ size = Vector2(16, 16)
size = Vector2(32, 32) size = Vector2(32, 32)
[node name="BaseUnit" type="CharacterBody2D"] [node name="BaseUnit" type="CharacterBody2D"]
motion_mode = 1
script = ExtResource("1_bo1wp") script = ExtResource("1_bo1wp")
currentCell = Vector2i(20, 20) unitData = {
"MoveRange": 10,
"MoveRangeV": Vector2(5, 5),
"MoveRangeVi": Vector2i(5, 5),
"Speed": 400.0
}
[node name="PathPrev" type="Line2D" parent="."] [node name="PathPrev" type="Line2D" parent="."]
top_level = true top_level = true
@@ -21,4 +27,5 @@ default_color = Color(0, 0.3372549, 0.3372549, 1)
texture = SubResource("PlaceholderTexture2D_imbg2") texture = SubResource("PlaceholderTexture2D_imbg2")
[node name="CollisionShape2D" type="CollisionShape2D" parent="."] [node name="CollisionShape2D" type="CollisionShape2D" parent="."]
scale = Vector2(0.5, 0.5)
shape = SubResource("RectangleShape2D_hfg1r") shape = SubResource("RectangleShape2D_hfg1r")

View File

@@ -0,0 +1,21 @@
MIT License
Copyright (c) 2016-2023 The Godot Engine community
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,10 @@
[configuration]
entry_symbol = "git_plugin_init"
compatibility_minimum = "4.2.0"
[libraries]
linux.editor.x86_64 = "linux/libgit_plugin.linux.editor.x86_64.so"
macos.editor = "macos/libgit_plugin.macos.editor.universal.dylib"
windows.editor.x86_64 = "windows/libgit_plugin.windows.editor.x86_64.dll"

View File

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

View File

@@ -31,6 +31,11 @@ window/size/no_focus=true
project/assembly_name="TurnBasedStrategyGame" project/assembly_name="TurnBasedStrategyGame"
[editor]
version_control/plugin_name="GitPlugin"
version_control/autoload_on_startup=true
[editor_plugins] [editor_plugins]
enabled=PackedStringArray("res://addons/smartcamera2D/plugin.cfg") enabled=PackedStringArray("res://addons/smartcamera2D/plugin.cfg")