created fog of war

This commit is contained in:
Douwe Ravers
2024-10-05 13:12:42 +02:00
parent 71c3fad482
commit 4c0c57d5ba
40 changed files with 3396 additions and 0 deletions

View File

@@ -0,0 +1,11 @@
extends Faction
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
pass # Replace with function body.
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
pass

View File

@@ -0,0 +1,13 @@
class_name Faction extends Node
var buildings:Array[Node3D]:
get:
var buildings:Array[Node3D]
buildings.assign($Buildings.get_children())
return buildings
var units:Array[Node3D]:
get:
var buildings:Array[Node3D]
buildings.assign($Units.get_children())
return buildings

View File

@@ -0,0 +1,47 @@
class_name FogOfWar extends Node
@export var visual:bool = true
@export var width:int = 20
@export var height:int = 20
var size:int:
get: return width*height
var faction:Faction:
get: return get_parent() as Faction
var grid:Array[bool] = []
func _ready() -> void:
if not visual: $FogOfWarVisual.queue_free()
generate_new_grid()
visualize_grid()
func _on_tick() -> void:
update_fog_of_war()
visualize_grid()
func generate_new_grid()->void:
grid.resize(size)
for i in range(size):
grid[i] = true
func visualize_grid()->void:
if not has_node("FogOfWarVisual"): return
var fog_of_war_visual := $FogOfWarVisual as GridMap
for i in range(size):
var item := 0 if grid[i] else -1
fog_of_war_visual.set_cell_item(c2p(i2c(i)), item)
func update_fog_of_war()->void:
var entities := faction.buildings + faction.units
for i in range(size):
grid[i] = grid[i] and not entities.map(
func(entity:Node3D)->bool:
return entity.position.distance_squared_to(c2p(i2c(i))) < 10
).has(true)
# Converters from coordinates to index values
func c2i(coor:Vector2i)->int: return coor.x+coor.y*width
func i2c(index:int)->Vector2i: return Vector2i(index%width, index/width)
func c2p(coor:Vector2i)->Vector3i: return Vector3i(coor.x-width/2, 0, coor.y-height/2)

View File

@@ -0,0 +1,18 @@
[gd_scene load_steps=3 format=3 uid="uid://ch7ug2prgwyyn"]
[ext_resource type="Script" path="res://entities/faction/fog_of_war.gd" id="1_bm7vn"]
[ext_resource type="MeshLibrary" uid="uid://cesuajklj4ds4" path="res://resources/meshlib.tres" id="2_5docv"]
[node name="FogOfWar" type="Node"]
script = ExtResource("1_bm7vn")
[node name="TickTimer" type="Timer" parent="."]
wait_time = 0.5
autostart = true
[node name="FogOfWarVisual" type="GridMap" parent="."]
mesh_library = ExtResource("2_5docv")
cell_size = Vector3(1, 1, 1)
metadata/_editor_floor_ = Vector3(0, 0, 0)
[connection signal="timeout" from="TickTimer" to="." method="_on_tick"]

View File

@@ -0,0 +1,11 @@
extends Faction
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
pass # Replace with function body.
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
pass

View File

@@ -0,0 +1,33 @@
extends Camera3D
var move_speed : float = 5
var tilt_speed : float = 2
func _process(delta: float) -> void:
process_movement_speed()
process_movement(delta)
process_camera_tilt(delta)
func process_movement(delta:float) -> void:
var input_vector := Vector3.ZERO;
input_vector += Vector3.UP * Input.get_axis("move_down", "move_up")
input_vector += Vector3.FORWARD * Input.get_axis("move_back", "move_forward")
input_vector += Vector3.RIGHT * Input.get_axis("move_left", "move_right")
translate(input_vector * move_speed * delta)
position.y = clampf(position.y, 2, 20)
func process_movement_speed() -> void:
if Input.is_action_just_released("move_increase"):
move_speed = clampf(move_speed + 1, 1, 10)
if Input.is_action_just_released("move_decrease"):
move_speed = clampf(move_speed-1, 1, 10)
func process_camera_tilt(delta: float) -> void:
if Input.is_mouse_button_pressed(MOUSE_BUTTON_RIGHT):
Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
var mouse_move := -Input.get_last_mouse_velocity().normalized()
mouse_move *= tilt_speed * delta
rotation.y = rotation.y + mouse_move.x
rotation.x =clampf(rotation.x + mouse_move.y, -PI/2, 0)
else:
Input.mouse_mode = Input.MOUSE_MODE_VISIBLE