restructured folders
This commit is contained in:
parent
e3aec75404
commit
0f5045dc22
22 changed files with 32 additions and 21 deletions
54
entities/monster/monster.gd
Normal file
54
entities/monster/monster.gd
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
class_name Monster
|
||||
extends CharacterBody3D
|
||||
|
||||
signal level_up
|
||||
|
||||
@export var stats: MonsterData = MonsterData.new()
|
||||
|
||||
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 = stats.model.instantiate()
|
||||
frequency = randf_range(1.0, 4.0)
|
||||
amplitude = randf_range(1.0, 6.0)
|
||||
spawn_point = position
|
||||
add_child(model_instance)
|
||||
|
||||
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
|
||||
|
||||
collider.start_battle(self, new_player_position)
|
||||
#rotate_toward(rotation.z, collider.rotation.z, 1.0)
|
||||
22
entities/monster/monster.tscn
Normal file
22
entities/monster/monster.tscn
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
[gd_scene load_steps=5 format=3 uid="uid://cytaswycabcel"]
|
||||
|
||||
[ext_resource type="Script" path="res://entities/monster/monster.gd" id="1_5gfkg"]
|
||||
[ext_resource type="Material" uid="uid://ctul3e67rcq23" path="res://materials/CollisionDebugMaterial.tres" id="2_bf2kb"]
|
||||
|
||||
[sub_resource type="SphereShape3D" id="SphereShape3D_tjvna"]
|
||||
radius = 0.4
|
||||
|
||||
[sub_resource type="SphereMesh" id="SphereMesh_aou7a"]
|
||||
radius = 0.4
|
||||
height = 0.8
|
||||
|
||||
[node name="Monster" type="CharacterBody3D"]
|
||||
script = ExtResource("1_5gfkg")
|
||||
|
||||
[node name="CollisionShape3D" type="CollisionShape3D" parent="."]
|
||||
shape = SubResource("SphereShape3D_tjvna")
|
||||
|
||||
[node name="CollisionVisualizer" type="MeshInstance3D" parent="."]
|
||||
mesh = SubResource("SphereMesh_aou7a")
|
||||
skeleton = NodePath("")
|
||||
surface_material_override/0 = ExtResource("2_bf2kb")
|
||||
82
entities/monster_spawnpoint/monster_spawnpoint.gd
Normal file
82
entities/monster_spawnpoint/monster_spawnpoint.gd
Normal file
|
|
@ -0,0 +1,82 @@
|
|||
class_name MonsterSpawnpoint
|
||||
extends Node3D
|
||||
|
||||
@onready var monster_tscn = preload("res://entities/monster/monster.tscn")
|
||||
|
||||
# a spawnpoint that can spawn monsters
|
||||
# monsters are only spawned, if the player is not looking
|
||||
|
||||
# list that holds all spawnable monsters
|
||||
@export var spawnable_monsters: Array[MonsterData]
|
||||
|
||||
# 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 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] = []
|
||||
|
||||
# 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()
|
||||
|
||||
spawn_point = position
|
||||
|
||||
func spawn() -> void:
|
||||
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
|
||||
|
||||
# instantiate a monster .tscn scene
|
||||
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(position.x, position.y + 1, position.z)
|
||||
monster_to_spawn.position = monster_position
|
||||
|
||||
var debug_level_node = get_parent_node_3d()
|
||||
debug_level_node.add_child(monster_to_spawn)
|
||||
print("Spawning Mob \"%s\"" % monster_to_spawn.stats.display_name)
|
||||
|
||||
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
||||
func _process(delta: float) -> void:
|
||||
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
|
||||
30
entities/monster_spawnpoint/monster_spawnpoint.tscn
Normal file
30
entities/monster_spawnpoint/monster_spawnpoint.tscn
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
[gd_scene load_steps=4 format=3 uid="uid://dawpmu4vvv3rs"]
|
||||
|
||||
[ext_resource type="Script" path="res://entities/monster_spawnpoint/monster_spawnpoint.gd" id="1_p2vfw"]
|
||||
|
||||
[sub_resource type="SphereMesh" id="SphereMesh_phb77"]
|
||||
radius = 0.4
|
||||
radial_segments = 4
|
||||
rings = 1
|
||||
|
||||
[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_kvc2a"]
|
||||
albedo_color = Color(0.626709, 0.340723, 0.657327, 1)
|
||||
metallic = 1.0
|
||||
roughness = 0.25
|
||||
|
||||
[node name="MonsterSpawnpoint" type="Node3D"]
|
||||
script = ExtResource("1_p2vfw")
|
||||
|
||||
[node name="Diamond" type="MeshInstance3D" parent="."]
|
||||
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 2, 0)
|
||||
mesh = SubResource("SphereMesh_phb77")
|
||||
surface_material_override/0 = SubResource("StandardMaterial3D_kvc2a")
|
||||
|
||||
[node name="VisibleOnScreenNotifier3D" type="VisibleOnScreenNotifier3D" parent="."]
|
||||
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 2, 0)
|
||||
|
||||
[node name="Label3D" type="Label3D" parent="."]
|
||||
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 3, 0)
|
||||
visible = false
|
||||
text = "0 / 5"
|
||||
font_size = 64
|
||||
89
entities/player/player.gd
Normal file
89
entities/player/player.gd
Normal file
|
|
@ -0,0 +1,89 @@
|
|||
extends CharacterBody3D
|
||||
|
||||
@onready var animated_mesh = $GobotSkin
|
||||
|
||||
var in_battle: bool = false
|
||||
|
||||
const SPEED = 5.0
|
||||
const JUMP_VELOCITY = 4.5
|
||||
|
||||
func _physics_process(delta: float) -> void:
|
||||
if in_battle == true:
|
||||
return
|
||||
|
||||
animated_mesh.idle()
|
||||
|
||||
# Add the gravity.
|
||||
if not is_on_floor():
|
||||
velocity += get_gravity() * delta
|
||||
animated_mesh.fall()
|
||||
|
||||
# Handle jump.
|
||||
if Input.is_action_just_pressed("ui_accept"):
|
||||
print("jump")
|
||||
animated_mesh.run()
|
||||
|
||||
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
|
||||
|
||||
# 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")
|
||||
|
||||
if input_dir.length() > 0 and is_on_floor():
|
||||
animated_mesh.run()
|
||||
if input_dir.length() > 0 and not is_on_floor():
|
||||
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:
|
||||
$GobotSkin.rotation.y = atan2(direction.x,direction.z)
|
||||
|
||||
if direction:
|
||||
velocity.x = direction.x * SPEED
|
||||
velocity.z = direction.z * SPEED
|
||||
else:
|
||||
velocity.x = move_toward(velocity.x, 0, SPEED)
|
||||
velocity.z = move_toward(velocity.z, 0, SPEED)
|
||||
|
||||
move_and_slide()
|
||||
|
||||
if velocity.length() < 0.05:
|
||||
animated_mesh.idle()
|
||||
|
||||
# what happens when a battle starts?
|
||||
# the player is positioned (mostly somewhere else than the current position)
|
||||
# ui changes
|
||||
# music changes
|
||||
func start_battle(enemy: Node3D, player_position: Vector3) -> void:
|
||||
position = Vector3(player_position.x, position.y, player_position.z)
|
||||
in_battle = true
|
||||
velocity = Vector3.ZERO
|
||||
animated_mesh.idle()
|
||||
|
||||
await get_tree().create_timer(2.0).timeout
|
||||
print("timer over")
|
||||
in_battle = false
|
||||
|
||||
func save() -> Dictionary:
|
||||
var save_dict = {
|
||||
"player_transform": {
|
||||
"pos_x": global_position.x,
|
||||
"pos_y": global_position.y,
|
||||
"pos_z": global_position.z,
|
||||
"rot_x": global_rotation.x,
|
||||
"rot_y": global_rotation.y,
|
||||
"rot_z": global_rotation.z
|
||||
}
|
||||
}
|
||||
return save_dict
|
||||
28
entities/player/player.tscn
Normal file
28
entities/player/player.tscn
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
[gd_scene load_steps=4 format=3 uid="uid://be5bc5gjbo875"]
|
||||
|
||||
[ext_resource type="Script" path="res://entities/player/player.gd" id="1_mv2yl"]
|
||||
[ext_resource type="PackedScene" uid="uid://cy36aygvs66pn" path="res://assets/models/gobot/gobot_skin.tscn" id="2_a4t6w"]
|
||||
|
||||
[sub_resource type="CapsuleShape3D" id="CapsuleShape3D_xuba7"]
|
||||
height = 1.7
|
||||
|
||||
[node name="Player" type="CharacterBody3D"]
|
||||
axis_lock_angular_x = true
|
||||
axis_lock_angular_y = true
|
||||
axis_lock_angular_z = true
|
||||
script = ExtResource("1_mv2yl")
|
||||
|
||||
[node name="GobotSkin" parent="." instance=ExtResource("2_a4t6w")]
|
||||
transform = Transform3D(-1, 0, -8.74228e-08, 0, 1, 0, 8.74228e-08, 0, -1, 0, 0, 0)
|
||||
|
||||
[node name="CollisionShape3D" type="CollisionShape3D" parent="."]
|
||||
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.85, 0)
|
||||
shape = SubResource("CapsuleShape3D_xuba7")
|
||||
|
||||
[node name="CameraRoot" type="Node3D" parent="."]
|
||||
|
||||
[node name="SpringArm3D" type="SpringArm3D" parent="CameraRoot"]
|
||||
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 2.64528, 2.83335)
|
||||
|
||||
[node name="Camera3D" type="Camera3D" parent="CameraRoot/SpringArm3D"]
|
||||
transform = Transform3D(1, 0, 0, 0, 0.965926, 0.258819, 0, -0.258819, 0.965926, 0, 0.0210915, 0.372644)
|
||||
Loading…
Add table
Add a link
Reference in a new issue