add bug mesh - colorize flowfield - very simple enemy - and first test with multimesh
This commit is contained in:
24
swarm_engine/bug.gd
Normal file
24
swarm_engine/bug.gd
Normal file
@@ -0,0 +1,24 @@
|
||||
extends Node3D
|
||||
|
||||
@onready var flow_field_navigation: FlowFieldNav = %FlowFieldNavigation
|
||||
|
||||
@export var move_speed = 3
|
||||
|
||||
# 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:
|
||||
DebugDraw3D.draw_sphere(position, .5)
|
||||
var cell = flow_field_navigation.get_cell_from_pos(position)
|
||||
|
||||
if cell.flow_direction.length() > 0.0:
|
||||
# Normalisierte Bewegung in Flow-Richtung
|
||||
var movement = cell.flow_direction.normalized() * move_speed * delta
|
||||
global_transform.origin += movement # Position aktualisierennormalized() * speed
|
||||
|
||||
# Rotation: Gegner schaut in Bewegungsrichtung (XZ-Ebene)
|
||||
var target_rotation = Quaternion(Vector3.UP, atan2(-cell.flow_direction.x, -cell.flow_direction.z))
|
||||
global_transform.basis = Basis(target_rotation)
|
||||
@@ -1,11 +1,31 @@
|
||||
extends Resource
|
||||
class_name FlowFieldCell
|
||||
|
||||
var position:Vector3 = Vector3(0.0, 0.0, 0.0)
|
||||
var flow_direction:Vector3 = Vector3(randf(), 0.0, randf())
|
||||
@export var cell_size: float = 1.0
|
||||
@export var position: Vector3 = Vector3.ZERO
|
||||
var flow_direction: Vector3 = Vector3.ZERO
|
||||
var is_target:bool = false
|
||||
|
||||
|
||||
func update(targetPos: Vector3):
|
||||
if is_target:
|
||||
flow_direction = Vector3.ZERO
|
||||
return
|
||||
|
||||
if targetPos != position:
|
||||
flow_direction = (targetPos - position).normalized() * (cell_size/2)
|
||||
else:
|
||||
flow_direction = Vector3.ZERO
|
||||
|
||||
func debug_process() -> void:
|
||||
if flow_direction.length() > 0.0:
|
||||
DebugDraw3D.draw_arrow(position, position + flow_direction, Color(1,1,1,1), 0.1)
|
||||
var arrow_color = get_direction_color(flow_direction.normalized())
|
||||
DebugDraw3D.draw_arrow(position, position + flow_direction, arrow_color, 0.1)
|
||||
else:
|
||||
DebugDraw3D.draw_sphere(position, .05)
|
||||
DebugDraw3D.draw_sphere(position, 0.05)
|
||||
|
||||
func get_direction_color(direction: Vector3) -> Color:
|
||||
var angle = atan2(direction.z, direction.x)
|
||||
var hue = (angle / (2.0 * PI)) + 0.5
|
||||
|
||||
return Color.from_hsv(hue, 1.0, 1.0)
|
||||
|
||||
@@ -1,4 +1,8 @@
|
||||
extends Node3D
|
||||
class_name FlowFieldNav
|
||||
|
||||
@onready var player: Player = %player
|
||||
|
||||
|
||||
@export_group("Debug")
|
||||
@export var debugVisu: bool = true
|
||||
@@ -11,10 +15,27 @@ extends Node3D
|
||||
var _cells: Dictionary
|
||||
var grid_offset: Vector3
|
||||
|
||||
var target_position: Vector3
|
||||
var target_cell: FlowFieldCell
|
||||
|
||||
var lines: PackedVector3Array
|
||||
|
||||
func _ready() -> void:
|
||||
var half_size = (grid_size * cell_size) * 0.5
|
||||
grid_offset = Vector3(-half_size + (cell_size * 0.5), 0, -half_size + (cell_size * 0.5))
|
||||
create_grid()
|
||||
|
||||
# Vertikale Linien
|
||||
for x in range(grid_size + 1):
|
||||
var pos_x = x * cell_size + grid_offset.x - (cell_size * 0.5)
|
||||
lines.append(Vector3(pos_x, 0, grid_offset.z - (cell_size * 0.5)))
|
||||
lines.append(Vector3(pos_x, 0, grid_offset.z + grid_size * cell_size - (cell_size * 0.5)))
|
||||
|
||||
# Horizontale Linien
|
||||
for z in range(grid_size + 1):
|
||||
var pos_z = z * cell_size + grid_offset.z - (cell_size * 0.5)
|
||||
lines.append(Vector3(grid_offset.x - (cell_size * 0.5), 0, pos_z))
|
||||
lines.append(Vector3(grid_offset.x + grid_size * cell_size - (cell_size * 0.5), 0, pos_z))
|
||||
|
||||
func create_grid() -> void:
|
||||
_cells.clear()
|
||||
@@ -24,48 +45,36 @@ func create_grid() -> void:
|
||||
var world_x = x * cell_size + grid_offset.x
|
||||
var world_z = z * cell_size + grid_offset.z
|
||||
cell.position = Vector3(world_x, 0, world_z)
|
||||
cell.cell_size = cell_size
|
||||
_cells[Vector3(x, 0, z)] = cell # Dictionary mit Grid-Koordinaten als Key
|
||||
|
||||
|
||||
func _process(delta: float) -> void:
|
||||
target_position = Vector3(player.position.x, 0, player.position.z)
|
||||
var new_target_cell = get_cell_from_pos(target_position)
|
||||
if target_cell != null and target_cell != new_target_cell:
|
||||
target_cell.is_target = false
|
||||
new_target_cell.is_target = true
|
||||
target_cell = new_target_cell
|
||||
else:
|
||||
target_cell = new_target_cell
|
||||
target_cell.is_target = true
|
||||
|
||||
if debugVisu:
|
||||
_debug_draw_grid()
|
||||
for cell in _cells.values():
|
||||
cell.debug_process()
|
||||
|
||||
for cell in _cells.values():
|
||||
cell.update(target_position)
|
||||
|
||||
func _debug_draw_grid() -> void:
|
||||
for x in range(grid_size + 1):
|
||||
var pos_x = x * cell_size + grid_offset.x - (cell_size * 0.5)
|
||||
DebugDraw3D.draw_line(Vector3(pos_x, 0, grid_offset.z - (cell_size * 0.5)), Vector3(pos_x, 0, grid_offset.z + grid_size * cell_size - (cell_size * 0.5)), line_color)
|
||||
|
||||
for z in range(grid_size + 1):
|
||||
var pos_z = z * cell_size + grid_offset.z - (cell_size * 0.5)
|
||||
DebugDraw3D.draw_line(Vector3(grid_offset.x - (cell_size * 0.5), 0, pos_z), Vector3(grid_offset.x + grid_size * cell_size - (cell_size * 0.5), 0, pos_z), line_color)
|
||||
DebugDraw3D.draw_lines(lines, line_color)
|
||||
|
||||
|
||||
# Gibt die Zelle basierend auf einer Weltposition zurück
|
||||
func get_cell_from_pos(world_pos: Vector3) -> FlowFieldCell:
|
||||
var grid_x = int((world_pos.x - grid_offset.x) / cell_size)
|
||||
var grid_z = int((world_pos.z - grid_offset.z) / cell_size)
|
||||
var cell_pos = Vector3(grid_x, 0, grid_z)
|
||||
|
||||
return _cells.get(cell_pos, null)
|
||||
|
||||
|
||||
# Raycast zur Bestimmung der Zelle unter dem Mauszeiger
|
||||
func _input(event):
|
||||
if event is InputEventMouseButton and event.pressed:
|
||||
var camera = get_viewport().get_camera_3d()
|
||||
var from = camera.project_ray_origin(get_viewport().get_mouse_position())
|
||||
var to = from + camera.project_ray_normal(get_viewport().get_mouse_position()) * 1000
|
||||
|
||||
var space_state = get_world_3d().direct_space_state
|
||||
var query = PhysicsRayQueryParameters3D.create(from, to)
|
||||
var result = space_state.intersect_ray(query)
|
||||
|
||||
if result.has("position"):
|
||||
var cell = get_cell_from_pos(result["position"])
|
||||
if cell:
|
||||
print("Zelle an", cell.position, ":", cell.flow_direction)
|
||||
else:
|
||||
print("Keine Zelle gefunden!")
|
||||
|
||||
23
swarm_engine/multi_mesh_instance_3d.gd
Normal file
23
swarm_engine/multi_mesh_instance_3d.gd
Normal file
@@ -0,0 +1,23 @@
|
||||
extends MultiMeshInstance3D
|
||||
|
||||
@export var speed = 5;
|
||||
@export var number: int = 100
|
||||
|
||||
|
||||
func _ready() -> void:
|
||||
for x in range(number):
|
||||
for z in range(number):
|
||||
var index = z * number + x
|
||||
self.multimesh.set_instance_transform(index, Transform3D(Basis(), Vector3(x, 0.0, -z)))
|
||||
|
||||
func _process(delta: float) -> void:
|
||||
for x in range(number):
|
||||
for z in range(number):
|
||||
var index = z * number + x
|
||||
var current_trans = self.multimesh.get_instance_transform(index)
|
||||
|
||||
# Rotation um die Y-Achse (delta für gleichmäßige Drehung)
|
||||
var rotation_amount = deg_to_rad(10) * delta # 10 Grad pro Sekunde
|
||||
current_trans.basis = current_trans.basis.rotated(Vector3.UP, rotation_amount)
|
||||
|
||||
self.multimesh.set_instance_transform(index, current_trans)
|
||||
@@ -1,5 +1,6 @@
|
||||
extends Node3D
|
||||
|
||||
@onready var camera_3d: Camera3D = %Camera3D
|
||||
|
||||
func _process(delta: float) -> void:
|
||||
DebugDraw2D.set_text("Frames drawn", Engine.get_frames_drawn())
|
||||
|
||||
File diff suppressed because one or more lines are too long
11
swarm_engine/swarm_spawner/bug.tscn
Normal file
11
swarm_engine/swarm_spawner/bug.tscn
Normal file
@@ -0,0 +1,11 @@
|
||||
[gd_scene load_steps=3 format=3 uid="uid://ck0ymixmnj446"]
|
||||
|
||||
[ext_resource type="Script" path="res://swarm_engine/bug.gd" id="1_ql2rg"]
|
||||
[ext_resource type="PackedScene" uid="uid://be1j1x6skha50" path="res://assets/bug/scene.gltf" id="2_0pswm"]
|
||||
|
||||
[node name="Bug" type="Node3D"]
|
||||
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 2.85631, 13.4907)
|
||||
script = ExtResource("1_ql2rg")
|
||||
|
||||
[node name="Sketchfab_Scene" parent="." instance=ExtResource("2_0pswm")]
|
||||
transform = Transform3D(-4.37114e-08, 0, 1, 0, 1, 0, -1, 0, -4.37114e-08, -1.5, 0, -1.5)
|
||||
Reference in New Issue
Block a user