Skip to content

Commit

Permalink
Refactor and apply fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
artism90 committed Sep 16, 2023
1 parent 844ace7 commit dca2966
Show file tree
Hide file tree
Showing 42 changed files with 226 additions and 284 deletions.
6 changes: 4 additions & 2 deletions Assets/Player/InteractionContext.gd
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,13 @@ class_name InteractionContext
signal switch_context
signal context_aborted

@onready var _player_camera := owner as Node3D

@export var _context_name: String = "Basic Interaction Context"
@export var valid_actions: PackedStringArray = ["main_command", "alt_command"]

var last_target: Node3D

@onready var _player_camera := owner as PlayerCamera

func interact(event: InputEvent, target: Node, position: Vector2) -> void:
if event.is_action_type():
for action in valid_actions:
Expand Down
22 changes: 12 additions & 10 deletions Assets/Player/InteractionContexts/MovementContext.gd
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
extends SelectionContext
class_name MovementContext

func move_selected_units(m_pos: Vector2) -> void:
var result = _player_camera.raycast_from_mouse()
if result:
print_debug("Command: Move selected units {0} ".format([result]))
for unit in _player_camera.selected_entities:
if unit.faction == _player_camera.player.faction:
unit.move_to(Utils.map_3_to_2(result.position))
func move_selected_units_to(position) -> void:
#print_debug("Command: Move selected units {0} ".format([result]))
print("Command: Move selected units to ", position)
for unit in _player_camera.selected_entities:
if unit.faction == _player_camera.player.faction:
#unit.move_to(round(Vector2(position.x + 0.5, position.y + 0.5)))
#unit.move_to(Vector2(position.x + 1, position.y + 1))
unit.move_to(Vector2(position))

func _on_ia_main_command_pressed(target: Node, position: Vector2) -> void:
#print_debug("Move action")

var m_pos = get_viewport().get_mouse_position()
move_selected_units(m_pos)
if not target is AStarMap:
print(target) # TODO: Interact with different entity (e.g. attack)
else:
move_selected_units_to(position)
16 changes: 14 additions & 2 deletions Assets/Player/InteractionContexts/SelectionContext.gd
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ enum SelectionType {
BUILDING,
}

@onready var _parent := get_parent()

@export var selection_tolerance: int = 10

@onready var _parent := get_parent()

var sel_pos_start: Vector2
var sel_pos_end: Vector2

Expand All @@ -35,6 +35,18 @@ func get_selected_units(top_left: Vector2, bottom_right: Vector2) -> Array:
selected_units.append(unit)
return selected_units

func _on_mouse_motion(target: Node, position: Vector2) -> void:
# Hover new object
if target != last_target:
if target is WorldThing:
target.set_hover(true)

# Unhover old object
if last_target is WorldThing:
last_target.set_hover(false)

last_target = target

func _on_ia_alt_command_pressed(target: Node, position: Vector2) -> void:
# Start selection
print_debug("Start selection")
Expand Down
4 changes: 2 additions & 2 deletions Assets/Player/InteractionContexts/TileContext.gd
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ func _ready() -> void:
set_process(false)

func show_phantom_tile(tile_pos: Vector2) -> void:
phantom_tile.position = streets.map_to_local(Vector3i(tile_pos.x,0,tile_pos.y))
phantom_tile.position = streets.map_to_local(Utils.map_2_to_3(Vector2(tile_pos)))

func handle_tiles(raycast_position: Vector2) -> void:
var tile_pos = streets.world_to_tilemap(raycast_position)
var tile_pos = streets.local_to_tile_map(raycast_position)
#var tile_item = streets.get_tile_item(tile_pos)
#var mesh_library = streets.mesh_library
#var item_name = mesh_library.get_item_name(tile_item)
Expand Down
37 changes: 6 additions & 31 deletions Assets/Player/PlayerCamera.gd
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
extends Node3D
class_name PlayerCamera

signal hovered
signal unhovered
signal selected(selected_entities)
signal unselected

Expand All @@ -14,25 +12,15 @@ var player: Player
var active_context: InteractionContext
var selected_entities: Array

var hovered_object: WorldThing : set = set_hovered_object

@onready var hud := $PlayerHUD

@onready var _camera := $RotationY/Camera3D as Camera3D

func set_hovered_object(new_hovered_object: WorldThing) -> void:
if new_hovered_object != hovered_object:
var hovered_object: WorldThing:
set(new_hovered_object):
prints("set_hovered_object:", new_hovered_object)
hovered_object = new_hovered_object
emit_signal("hovered")

func _on_WorldThing_mouse_entered(object: WorldThing) -> void:
hovered_object = object
emit_signal("hovered")
@onready var hud := $PlayerHUD

func _on_WorldThing_mouse_exited(object: WorldThing) -> void:
hovered_object = null
emit_signal("unhovered")
@onready var _camera := $RotationY/Camera3D as Camera3D

func _ready() -> void:
abort_context()
Expand All @@ -59,13 +47,7 @@ func _unhandled_input(event: InputEvent) -> void:
target_object = (target["collider"] as CollisionObject3D).get_parent()
target_pos = (Utils.map_3_to_2(target["position"]) as Vector2)

#if target_object is WorldThing:
# print(target_object)

#if hovered_object != null:
# target_object = hovered_object

active_context.interact(event, target_object, target_pos)
active_context.interact(event, target_object, ceil(target_pos))

func assign_to_player() -> Player:
return Global.World.player if is_instance_valid(Global.World) and is_instance_valid(Global.World.player) else null
Expand Down Expand Up @@ -128,20 +110,13 @@ func unset_selection() -> void:
emit_signal("unselected")

func raycast_from_mouse(collision_mask: int = -1) -> Dictionary:
#var m_pos: Vector2 = get_viewport().get_mouse_position()
#var ray_start := _camera.project_ray_origin(m_pos)
#var ray_end := ray_start + _camera.project_ray_normal(m_pos) * RAY_LENGTH
#var space_state := get_world_3d().direct_space_state
#var dict := space_state.intersect_ray(ray_start, ray_end, [], collision_mask, true, true)
#return dict

var mouse_pos := get_viewport().get_mouse_position()
var space_state := get_world_3d().direct_space_state
var query := PhysicsRayQueryParameters3D.new()
query.collide_with_areas = true
query.from = _camera.project_ray_origin(mouse_pos)
query.to = query.from + _camera.project_ray_normal(mouse_pos) * RAY_LENGTH
#query.exclude = [collision_mask] # gridmap.get_static_bodies_rids() TODO
query.collision_mask = collision_mask

var result := space_state.intersect_ray(query)
return result
Expand Down
14 changes: 6 additions & 8 deletions Assets/UI/Cursor.gd
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,12 @@ var cursors = {
CursorType.CURSOR_TEAR: CURSOR_TEAR
}

var cursor: int = CursorType.CURSOR_DEFAULT : get = get_cursor, set = set_cursor
var cursor: int = CursorType.CURSOR_DEFAULT:
get:
return cursor
set(new_cursor):
cursor = new_cursor
Input.set_custom_mouse_cursor(cursors[cursor])

func _ready() -> void:
self.cursor = CursorType.CURSOR_DEFAULT

func set_cursor(new_cursor: int) -> void:
cursor = new_cursor
Input.set_custom_mouse_cursor(cursors[cursor])

func get_cursor() -> int:
return cursor
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
[sub_resource type="Animation" id="1"]
resource_name = "cycle"
length = 0.6
loop_mode = 1
tracks/0/type = "value"
tracks/0/imported = false
tracks/0/enabled = true
Expand Down Expand Up @@ -68,7 +69,6 @@ offset_bottom = 26.0
grow_horizontal = 2
grow_vertical = 2
mouse_filter = 2
texture = ExtResource("4")

[node name="AnimationPlayer" parent="AnimatedTexture" index="0"]
libraries = {
Expand Down
79 changes: 32 additions & 47 deletions Assets/World/AStarMap.gd
Original file line number Diff line number Diff line change
@@ -1,44 +1,38 @@
@tool
extends Node3D
class_name AStarMap

signal map_size_changed(new_map_size: int)

var all_points := {}
var as_node: AStar2D = null

@export var map_size := 100 : set = set_map_size
@export_node_path("TileMap3D") var grid_map_path
@export var map_size := 100:
set(new_map_size):
if not is_inside_tree():
await self.ready

@onready var grid_map := get_node(grid_map_path) as TileMap3D
@onready var static_body: StaticBody3D = $StaticBody3D as StaticBody3D
@onready var collision_shape: CollisionShape3D = static_body.get_node("CollisionShape3D") as CollisionShape3D
map_size = clampi(new_map_size, 0, 256)

##var ray_plane := MeshInstance3D.new() as MeshInstance3D
static_body.position = Utils.map_2i_to_3i(Vector2(map_size, map_size))
collision_shape.shape.extents = Utils.map_2i_to_3i(Vector2(map_size, map_size))

func _ready() -> void:
##ray_plane.mesh = PlaneMesh.new()
##ray_plane.visible = true

##ray_plane.scale = Vector3.ONE * 128 # TODO: Adapt size dynamically to GridMap's outer bounds

#ray_plane.create_convex_collision() # crashes for GLES2 in the export build
emit_signal("map_size_changed", map_size)

#var static_body = StaticBody3D.new()
#var collision_shape = CollisionShape3D.new()
#collision_shape.shape = WorldBoundaryShape3D.new()
@export var tile_map: TileMap3D

##ray_plane.set("material/0", StandardMaterial3D.new())
##ray_plane.get("material/0").set("flags_no_depth_test", true)
##ray_plane.get("material/0").set("albedo_color", Color("1863d3"))
##add_child(ray_plane)
@onready var static_body := $StaticBody3D as StaticBody3D
@onready var collision_shape := static_body.get_node("CollisionShape3D") as CollisionShape3D

func _ready() -> void:
as_node = AStar2D.new()

var tiles = grid_map.get_used_tiles()
var tiles = tile_map.get_used_tiles()

for tile in tiles:
if _get_tile_item_name(tile) in ["deep", "shallow_curve_in"]:
var index = as_node.get_available_point_id()
as_node.add_point(index, grid_map.tilemap_to_local(tile))
as_node.add_point(index, tile_map.tile_map_to_local(tile))
all_points[v2_to_index(tile)] = index

for tile in tiles:
Expand All @@ -50,42 +44,33 @@ func _ready() -> void:
if v2 == Vector2i.ZERO:
continue

#prints("v2:", v2, "tile:", tile)
if v2_to_index(v2 + tile) in all_points:
var ind1 = all_points[v2_to_index(tile)]
var ind2 = all_points[v2_to_index(tile + v2)]
if !as_node.are_points_connected(ind1, ind2):
as_node.connect_points(ind1, ind2, true)
var i1 = all_points[v2_to_index(tile)]
var i2 = all_points[v2_to_index(tile + v2)]
if !as_node.are_points_connected(i1, i2):
as_node.connect_points(i1, i2, true)

func _get_tile_item_name(tile: Vector2i) -> String:
var tile_item_index = grid_map.get_tile_item(tile)
return grid_map.get_item_name(tile_item_index)
var tile_item_index = tile_map.get_tile_item(tile)
return tile_map.get_item_name(tile_item_index)

func v2_to_index(v2: Vector2i) -> String:
return str(int(v2.x)) + "," + str(int(v2.y))
#print("v2_to_index(", v2, ")")
return str(v2.x) + "," + str(v2.y)

func get_tilemap_path(start: Vector2i, end: Vector2i) -> PackedVector2Array:
func get_tile_map_path(start: Vector2i, end: Vector2i) -> PackedVector2Array:
#print_debug(start, end)
var gm_start := grid_map.world_to_tilemap(start)
var gm_end := grid_map.world_to_tilemap(end)
var tile_map_start: Vector2i = tile_map.local_to_tile_map(start)
var tile_map_end: Vector2i = tile_map.local_to_tile_map(end)
var start_id := 0
var end_id := 0
if gm_start in all_points:
start_id = all_points[gm_start]
if tile_map_start in all_points:
start_id = all_points[tile_map_start]
else:
start_id = as_node.get_closest_point(start)
if gm_end in all_points:
end_id = all_points[gm_end]
if tile_map_end in all_points:
end_id = all_points[tile_map_end]
else:
end_id = as_node.get_closest_point(end)
return as_node.get_point_path(start_id, end_id)

func set_map_size(new_map_size: int) -> void:
if not is_inside_tree():
await self.ready

map_size = new_map_size if new_map_size > 0 else 0

static_body.position = Vector3(map_size, 0, map_size)
collision_shape.shape.extents = Vector3(map_size, 0, map_size)

emit_signal("map_size_changed", map_size)
6 changes: 4 additions & 2 deletions Assets/World/Buildings/Lumberjack/Lumberjack.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ emission_enabled = true
emission = Color(1, 1, 1, 1)
billboard_mode = 1

[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_ixlho"]
[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_olghc"]
transparency = 2
alpha_scissor_threshold = 0.05
alpha_antialiasing_mode = 0
Expand Down Expand Up @@ -42,6 +42,7 @@ offset = Vector2(0, 32)
pixel_size = 0.025
billboard = 1
double_sided = false
no_depth_test = true
hframes = 2
vframes = 2

Expand All @@ -51,12 +52,13 @@ offset = Vector2(0, 32)
pixel_size = 0.025
billboard = 1
double_sided = false
no_depth_test = true
hframes = 2
vframes = 2

[node name="Outline" parent="Billboard" index="2"]
material_override = SubResource("1")
material_overlay = SubResource("StandardMaterial3D_ixlho")
material_overlay = SubResource("StandardMaterial3D_olghc")
texture = ExtResource("1")
region_enabled = true
region_rect = Rect2(0, 0, 256, 256)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ compress/lossy_quality=0.7
compress/hdr_compression=1
compress/normal_map=0
compress/channel_pack=0
mipmaps/generate=false
mipmaps/generate=true
mipmaps/limit=-1
roughness/mode=0
roughness/src_normal=""
Expand All @@ -32,4 +32,4 @@ process/normal_map_invert_y=false
process/hdr_as_srgb=false
process/hdr_clamp_exposure=false
process/size_limit=0
detect_3d/compress_to=1
detect_3d/compress_to=0
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ compress/lossy_quality=0.7
compress/hdr_compression=1
compress/normal_map=0
compress/channel_pack=0
mipmaps/generate=false
mipmaps/generate=true
mipmaps/limit=-1
roughness/mode=0
roughness/src_normal=""
Expand All @@ -32,4 +32,4 @@ process/normal_map_invert_y=false
process/hdr_as_srgb=false
process/hdr_clamp_exposure=false
process/size_limit=0
detect_3d/compress_to=1
detect_3d/compress_to=0
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ compress/lossy_quality=0.7
compress/hdr_compression=1
compress/normal_map=0
compress/channel_pack=0
mipmaps/generate=false
mipmaps/generate=true
mipmaps/limit=-1
roughness/mode=0
roughness/src_normal=""
Expand All @@ -32,4 +32,4 @@ process/normal_map_invert_y=false
process/hdr_as_srgb=false
process/hdr_clamp_exposure=false
process/size_limit=0
detect_3d/compress_to=1
detect_3d/compress_to=0
Loading

0 comments on commit dca2966

Please sign in to comment.