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

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)