adds collision to spawned monsters, moves player position 3 units back

This commit is contained in:
Luca 2024-09-19 20:48:54 +02:00
parent f025f89401
commit e8f06e7b99
12 changed files with 251 additions and 121 deletions

13
scripts/inventory.gd Normal file
View file

@ -0,0 +1,13 @@
extends Control
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
Inventory.connect("inventory_changed", update_inventory)
pass # Replace with function body.
func update_inventory() -> void:
var m = Inventory.monsters[0]
var string = "%s Lvl %s, %s/%s" % [m.name, m.level, m.health, m.max_health]
$MonsterList/Monster1.text = string
print("Updating inventory")

View file

@ -1,13 +1,53 @@
class_name Monster
extends Node3D
extends CharacterBody3D
@export var monster_stats: MonsterStats
@export var stats: MonsterStats
var spawn_point: Vector3 = Vector3(0, 0, 0)
# DEBUG MOVEMENT
@onready var time: float = 16.0
@onready var frequency: float = 1.0
@onready var amplitude: float = 0.5
var in_battle: bool = false
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
var model_instance = monster_stats.model.instantiate()
var model_instance = stats.model.instantiate()
frequency = randf_range(1.0, 4.0)
amplitude = randf_range(1.0, 6.0)
spawn_point = position
add_child(model_instance)
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
pass
func _physics_process(delta: float) -> void:
if in_battle:
velocity = Vector3.ZERO
return
time += delta
var x_movement = sin(time * frequency) * amplitude
var z_movement = cos(time * frequency) * amplitude
velocity = Vector3(x_movement, 0, z_movement)
# if move_and_slide reports collisions
if move_and_slide():
var collision = get_slide_collision(0)
var collider = collision.get_collider()
# if the monster collided with the player...
if collider.name == "Player":
velocity = Vector3.ZERO
in_battle = true
# calculate players new position
# from the camera 3 units backwards
var camera = collider.find_child("Camera3D")
# local z axis of the camera
var local_z = camera.global_transform.basis.z
var new_player_position = camera.global_transform.origin + local_z * 3
print(camera.global_transform.origin, new_player_position)
collider.start_battle(new_player_position)

View file

@ -1,32 +1,40 @@
class_name MonsterSpawnpoint
extends Node3D
# a spawnpoint that can spawn monsters around the center point
# with a given distance
@onready var monster_tscn = preload("res://scenes/monster.tscn")
# a spawnpoint that can spawn monsters
# 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[PackedScene]
@export var spawnable_monsters: Array[MonsterStats]
# ignore spawning only if visible
@export var ignore_visibility: bool = false
# 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
# only spawn a monster every x seconds
@export var spawn_cooldown: float = 3.0 # seconds
@onready var spawn_timer: Timer = Timer.new()
@onready var time: float = 16.0
@onready var frequency: float = 1.0
@onready var amplitude: float = 0.5
@onready var spawn_point: Vector3 = Vector3(0, 0, 0)
@onready var label: Label3D = $Label3D
# a spawnpoint should keep track of spawned entities
#var active_monsters: Array[Monster] = []
var active_monsters: Array[Monster] = []
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
@ -35,31 +43,40 @@ func _ready() -> void:
spawn_timer.timeout.connect(spawn)
add_child(spawn_timer)
spawn_timer.start()
spawn_point = position
func spawn() -> void:
if $VisibleOnScreenNotifier3D.is_on_screen():
if len(active_monsters) >= max_monsters:
print("Spawnpoint reached maximum amount of monsters")
return
if ignore_visibility == false and $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].instantiate()
var monster_to_spawn = monster_tscn.instantiate()
monster_to_spawn.stats = spawnable_monsters[0]
# keep track of the current monster
active_monsters.append(monster_to_spawn)
label.text = "%s / %s" % [len(active_monsters), max_monsters]
# 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)
monster_to_spawn.position = monster_position + position
var monster_position = Vector3(position.x, position.y + 1, position.z)
monster_to_spawn.position = monster_position
var debug_level_node = get_node("/root/Main/CurrentLevel/DebugLevel")
var debug_level_node = get_parent_node_3d()
debug_level_node.add_child(monster_to_spawn)
print("spawning mob")
print("Spawning Mob \"%s\"" % monster_to_spawn.stats.name)
# 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
time += delta
var diamond_rotation: float = sin(time * frequency) * amplitude
var diamond_height: float = sin(time * frequency) * amplitude
$Diamond.rotation.y = diamond_rotation
$Diamond.position.y = 2.0 + diamond_height

View file

@ -4,13 +4,6 @@ extends CharacterBody3D
const SPEED = 5.0
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()
@ -62,3 +55,12 @@ func _physics_process(delta: float) -> void:
if velocity.length() < 0.01:
animated_mesh.idle()
# what happens when a battle starts?
# the player is positioned (mostly elsewhere than where he currently stands)
# ui changes
# music changes
func start_battle(player_position: Vector3) -> void:
position = Vector3(player_position.x, position.y, player_position.z)
print("düdelüdelüdelüdelü")
print("start battle")