monster spawner and moving to the camera direction

This commit is contained in:
Luca 2024-09-16 19:58:43 +02:00
parent 8c29b8bffd
commit b20f3b5124
821 changed files with 1611 additions and 1409 deletions

View file

@ -1,17 +1,35 @@
extends Panel
@onready var talk_a_texture = preload("res://resources/textures/Xbox Series/Vector/xbox_button_a.svg")
@onready var drag_texture = preload("res://resources/textures/Generic/Vector/generic_button_circle.svg")
var thumbstick_size_factor = 0.5
@onready var talk_a_texture = preload("res://assets/textures/Xbox Series/Vector/xbox_button_a.svg")
@onready var drag_texture = preload("res://assets/textures/Generic/Vector/generic_button_circle.svg")
@onready var thumbstick = $Thumbstick
var thumbstick_size_factor = 0.5
var middle: Vector2 = Vector2(100, 100)
var touch_start: Vector2 = Vector2(0, 0)
var jump_threshold: float = 10.0
var previous: Vector2 = Vector2(0, 0)
var delta: float = 0.0
var pressed: bool = false
# debug rotation
var rotation_delta: Vector2 = Vector2(0, 0)
func _ready() -> void:
thumbstick.custom_minimum_size = Vector2(size.x, size.x) * thumbstick_size_factor
thumbstick.position = middle - thumbstick.size / 2.0
print(thumbstick)
func _unhandled_input(event: InputEvent) -> void:
if event is not InputEventScreenDrag:
return
if event is InputEventScreenDrag:
var touch_position = event.position
rotation_delta = get_viewport_rect().size / 2 - touch_position
simulate_joystick_motion(JOY_AXIS_RIGHT_X, -rotation_delta.x * 0.4, 0)
print(rotation_delta)
func _on_gui_input(event: InputEvent) -> void:
if event is not InputEventScreenTouch and event is not InputEventScreenDrag:
return
@ -38,17 +56,10 @@ func _on_gui_input(event: InputEvent) -> void:
var touch_position = event.position
thumbstick.position = touch_position - thumbstick.size / 2.0
var delta = middle - touch_position
var direction = delta.normalized()
simulate_joystick_motion(0, -direction.x)
simulate_joystick_motion(1, -direction.y)
#if previous.y > thumbstick.position.y + jump_threshold:
# print("jump")
# Input.action_press("ui_accept")
#print(previous)
#previous = thumbstick.position
var _delta = middle - touch_position
var direction = _delta.normalized()
simulate_joystick_motion(JOY_AXIS_LEFT_X, -direction.x)
simulate_joystick_motion(JOY_AXIS_LEFT_Y, -direction.y)
func simulate_joystick_motion(axis: int, value: float, device_id: int = 0) -> void:
var joystick_event = InputEventJoypadMotion.new()
@ -61,6 +72,8 @@ func simulate_joystick_motion(axis: int, value: float, device_id: int = 0) -> vo
func _on_resized() -> void:
print("Size changed to %s" % size)
middle = size / 2.0
custom_minimum_size = Vector2(size.x, size.x)
thumbstick = get_node("Thumbstick")
#if thumbstick:
thumbstick.custom_minimum_size = Vector2(size.x, size.x) * thumbstick_size_factor
thumbstick.position = middle - thumbstick.size / 2.0

11
scripts/monster_list.gd Normal file
View file

@ -0,0 +1,11 @@
extends VBoxContainer
# 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,68 @@
class_name MonsterSpawnpoint
extends Node3D
# a spawnpoint that can spawn monsters around the center point
# with a given distance
# monsters are only spawned, if the player is not looking
@onready var spawn_timer: Timer = Timer.new()
# list that holds all spawnable monsters
@export var spawnable_monsters: Array[MonsterStats]
# the maximum amount of spawned monsters
# no monsters will spawn, when this number is reached
@export var max_monsters: int = 5
# the maximum distance a monster can spawn around the spawnpoint
@export var max_distance_spawn: float = 10.0
# the maximum lifetime a monster can be in the world
# a monster is then deleted if the following conditions are met:
# the monster has exceeded its maximum lifetime and it is not on screen
# TODO: figure out how to check for the exceeded lifetime and delete
@export var max_lifetime: float = 60.0 # seconds
@export var spawn_cooldown: float = 3.0 # seconds
# a spawnpoint should keep track of spawned entities
#var active_monsters: Array[Monster] = []
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
spawn_timer.wait_time = spawn_cooldown
spawn_timer.autostart = true
spawn_timer.timeout.connect(spawn)
add_child(spawn_timer)
spawn_timer.start()
func spawn() -> void:
if $VisibleOnScreenNotifier3D.is_on_screen():
print("Spawnpoint is visible on screen. Not spawning a monster.")
return
# TODO: Add a check to not spawn enemies X units near the player
# get a random position near the spawn point
var spawn_point_x = randf_range(-max_distance_spawn, max_distance_spawn)
var spawn_point_z = randf_range(-max_distance_spawn, max_distance_spawn)
# instantiate a monster .tscn scene
var monster_to_spawn = spawnable_monsters[0]
# TODO: Replace this with actual instantiation of a walking monster
var mesh_instance = MeshInstance3D.new()
mesh_instance.mesh = BoxMesh.new()
# calculate the position of the monster and add the offset of the spawnpoint itself
var monster_position = Vector3(spawn_point_x, 0.2, spawn_point_z)
mesh_instance.position = monster_position + position
var debug_level_node = get_node("/root/Main/CurrentLevel/DebugLevel")
debug_level_node.add_child(mesh_instance)
print("spawning mob")
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
var time = Time.get_ticks_msec()
$Diamond.rotation.y = sin(time / 800.0) * 2
$Diamond.position.y = 1.5 + sin(time / 800.0) * 0.3

View file

@ -7,6 +7,10 @@ const JUMP_VELOCITY = 4.5
func _ready() -> void:
pass
func _process(delta: float) -> void:
#$CameraRoot.rotation.y += 0.005
pass
func _physics_process(delta: float) -> void:
animated_mesh.idle()
@ -23,8 +27,12 @@ func _physics_process(delta: float) -> void:
if Input.is_action_just_pressed("ui_accept") and is_on_floor():
velocity.y = JUMP_VELOCITY
# Camera rotation
var camera_input_dir := Input.get_vector("joystick_right_left", "joystick_right_right", "joystick_right_left", "joystick_right_right")
$CameraRoot.rotation.y += camera_input_dir.x * 0.2
# Get the input direction and handle the movement/deceleration.
# Get the input direction and handle the movement / deceleration.
# As good practice, you should replace UI actions with custom gameplay actions.
var input_dir := Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down")
@ -34,6 +42,10 @@ func _physics_process(delta: float) -> void:
animated_mesh.fall()
var direction := (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
# Rotate the direction with the camera, so that the cameras forward direction
# is the joysticks and the players forward direction
direction = direction.rotated(Vector3.UP, $CameraRoot.rotation.y)
# Move character to the direction
if direction.length() != 0: