Skip to content

Commit

Permalink
Add an early combat test and physics layers.
Browse files Browse the repository at this point in the history
  • Loading branch information
Makosai committed Sep 22, 2023
1 parent 7123e48 commit 5c242b2
Show file tree
Hide file tree
Showing 10 changed files with 110 additions and 39 deletions.
11 changes: 9 additions & 2 deletions assets/characters/enemies/aqua_wizard.tscn
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
[gd_scene load_steps=3 format=3 uid="uid://op561o2k0d4o"]
[gd_scene load_steps=5 format=3 uid="uid://op561o2k0d4o"]

[ext_resource type="Script" path="res://scripts/mob/attackable/enemy/aqua_wizard.gd" id="1_2xua2"]
[ext_resource type="PackedScene" uid="uid://bqpe013mgarni" path="res://scripts/mob/attackable/attackable_health.tscn" id="1_051d3"]
[ext_resource type="PackedScene" uid="uid://b3p21o2ljivu2" path="res://assets/characters/npcs/aqua_wizard.glb" id="1_hf2bh"]

[sub_resource type="CapsuleShape3D" id="CapsuleShape3D_jxymx"]
radius = 0.573225
height = 2.59712

[node name="AquaWizard" type="CharacterBody3D"]
[node name="AquaWizard" type="CharacterBody3D" node_paths=PackedStringArray("health_manager")]
script = ExtResource("1_2xua2")
health_manager = NodePath("Visuals/Health")

[node name="CollisionShape3D" type="CollisionShape3D" parent="."]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.29, 0)
shape = SubResource("CapsuleShape3D_jxymx")

[node name="Visuals" type="Node3D" parent="."]

[node name="Health" parent="Visuals" instance=ExtResource("1_051d3")]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 2.59677, 0)

[node name="AquaWizard" parent="Visuals" instance=ExtResource("1_hf2bh")]
transform = Transform3D(1.5, 0, 0, 0, 1.5, 0, 0, 0, 1.5, 0, 0, 0)
33 changes: 6 additions & 27 deletions assets/characters/npcs/larry.tscn
Original file line number Diff line number Diff line change
@@ -1,44 +1,23 @@
[gd_scene load_steps=7 format=3 uid="uid://bmicgu6inl8e5"]
[gd_scene load_steps=5 format=3 uid="uid://bmicgu6inl8e5"]

[ext_resource type="Script" path="res://assets/characters/npcs/larry.gd" id="1_7b1nv"]
[ext_resource type="Script" path="res://scripts/mob/attackable/health_manager.gd" id="1_ile63"]
[ext_resource type="Script" path="res://scripts/mob/attackable/npc/larry.gd" id="1_mma82"]
[ext_resource type="PackedScene" uid="uid://b3p21o2ljivu2" path="res://assets/characters/npcs/aqua_wizard.glb" id="1_perw3"]
[ext_resource type="PackedScene" uid="uid://bb6pl2i42qjqf" path="res://scenes/ui/status_bars/health_bar.tscn" id="2_46ytx"]
[ext_resource type="PackedScene" uid="uid://bqpe013mgarni" path="res://scripts/mob/attackable/attackable_health.tscn" id="2_8ta6c"]

[sub_resource type="CapsuleShape3D" id="CapsuleShape3D_tslik"]
radius = 0.376215
height = 1.77435

[sub_resource type="ViewportTexture" id="ViewportTexture_vjeyi"]
viewport_path = NodePath("Visuals/Health/SubViewport")

[node name="Larry" type="CharacterBody3D" node_paths=PackedStringArray("health_manager")]
script = ExtResource("1_7b1nv")
health_manager = NodePath("Visuals/Health")
[node name="Larry" type="CharacterBody3D"]
script = ExtResource("1_mma82")

[node name="CollisionShape3D" type="CollisionShape3D" parent="."]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.88408, 0)
shape = SubResource("CapsuleShape3D_tslik")

[node name="Visuals" type="Node3D" parent="."]

[node name="Health" type="Node3D" parent="Visuals"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.72083, 0)
script = ExtResource("1_ile63")
metadata/_edit_lock_ = true

[node name="SubViewport" type="SubViewport" parent="Visuals/Health"]
transparent_bg = true
size = Vector2i(530, 65)

[node name="HealthBar" parent="Visuals/Health/SubViewport" instance=ExtResource("2_46ytx")]

[node name="Sprite" type="Sprite3D" parent="Visuals/Health"]
transform = Transform3D(0.15, 0, 0, 0, 0.15, 0, 0, 0, 0.15, 0, 0.0731125, 0)
texture = SubResource("ViewportTexture_vjeyi")
metadata/_edit_lock_ = true

[node name="HideTimer" type="Timer" parent="Visuals/Health"]
[node name="Health" parent="Visuals" instance=ExtResource("2_8ta6c")]

[node name="Larry" parent="Visuals" instance=ExtResource("1_perw3")]
metadata/_edit_lock_ = true
10 changes: 10 additions & 0 deletions project.godot
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,16 @@ quit={
]
}

[layer_names]

3d_physics/layer_1="walls"
3d_physics/layer_2="attackable"
3d_physics/layer_3="player"
3d_physics/layer_4="npc"
3d_physics/layer_5="enemy"
3d_physics/layer_6="item"
3d_physics/layer_9="ground"

[rendering]

textures/vram_compression/import_etc2_astc=true
Expand Down
7 changes: 7 additions & 0 deletions scenes/reia/reia.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,22 @@ light_data = ExtResource("1_6htgl")

[node name="MyPlayer" parent="Players" instance=ExtResource("3_m63h2")]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 1.347, -0.754201, 2.465)
collision_layer = 6
collision_mask = 287

[node name="NPCs" type="Node3D" parent="."]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 1.57933, -0.75015, 1.28274)

[node name="Larry" parent="NPCs" instance=ExtResource("2_w4lqh")]
collision_mask = 287

[node name="Enemies" type="Node3D" parent="."]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 1.57933, -0.75015, 1.28274)

[node name="AquaWizard" parent="Enemies" instance=ExtResource("3_jgoiv")]
transform = Transform3D(-0.351793, 0, 0.936078, 0, 1, 0, -0.936078, 0, -0.351793, -5.80119, 0, 2.76315)
collision_layer = 18
collision_mask = 287

[node name="Terrain" type="Node" parent="."]

Expand All @@ -58,6 +63,8 @@ transform = Transform3D(0.766045, 0, 0.642788, 0, 1, 0, -0.642788, 0, 0.766045,
[node name="Ground" type="CSGBox3D" parent="Terrain/Groundings"]
transform = Transform3D(3.86976, 0, 0, 0, 1.00056, 0, 0, 0, 8.83943, 0, -0.75856, -24.7402)
use_collision = true
collision_layer = 256
collision_mask = 0
size = Vector3(35, 0.002, 15)
material = SubResource("StandardMaterial3D_epo51")

Expand Down
10 changes: 5 additions & 5 deletions scripts/mob/attackable/attackable.gd
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ var armor_defense = 3

@export var health_manager: HealthManager

func _on_hit(attacker: Attackable, attackee: Attackable):
if attackee.health == 0: return
attackee.health -= (attacker.strength + attacker.weapon_damage) - (attackee.defense + attackee.armor_defense)
attackee.health_manager.set_health(attackee.health, attackee.max_health)
func _on_hit(attacker: Attackable):
if health == 0: return
health -= (attacker.strength + attacker.weapon_damage) - (defense + armor_defense)
health_manager.set_health(health, max_health)

func _attack(attackable: Attackable):
attackable._on_hit(attackable, self)
attackable._on_hit(self)
33 changes: 33 additions & 0 deletions scripts/mob/attackable/attackable_health.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
[gd_scene load_steps=4 format=3 uid="uid://bqpe013mgarni"]

[ext_resource type="Script" path="res://scripts/mob/attackable/health_manager.gd" id="1_tkq40"]
[ext_resource type="PackedScene" uid="uid://bb6pl2i42qjqf" path="res://scenes/ui/status_bars/health_bar.tscn" id="2_xssh5"]

[sub_resource type="ViewportTexture" id="ViewportTexture_pf4sx"]
viewport_path = NodePath("SubViewport")

[node name="Health" type="Node3D"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.72083, 0)
script = ExtResource("1_tkq40")

[node name="SubViewport" type="SubViewport" parent="."]
transparent_bg = true
size = Vector2i(520, 60)

[node name="HealthBar" parent="SubViewport" instance=ExtResource("2_xssh5")]
anchors_preset = 15
anchor_left = 0.0
anchor_top = 0.0
anchor_right = 1.0
anchor_bottom = 1.0
offset_left = 0.0
offset_top = 0.0
offset_right = 0.0
offset_bottom = 0.0
scale = Vector2(0.438, 0.438)

[node name="Sprite" type="Sprite3D" parent="."]
transform = Transform3D(0.15, 0, 0, 0, 0.15, 0, 0, 0, 0.15, 0, 0.0731125, 0)
texture = SubResource("ViewportTexture_pf4sx")

[node name="HideTimer" type="Timer" parent="."]
1 change: 1 addition & 0 deletions scripts/mob/attackable/enemy/aqua_wizard.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
class_name AquaWizard extends Attackable
File renamed without changes.
27 changes: 22 additions & 5 deletions scripts/mob/attackable/player/player.gd
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ var gravity = ProjectSettings.get_setting("physics/3d/default_gravity")
func _ready():
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)

func _process(_delta):
if position.y <= -10: # handle falling off the map for now
position.y = 1

func _input(_event: InputEvent):
if Input.is_action_just_pressed("quit"):
if Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED:
Expand All @@ -24,10 +28,6 @@ func _input(_event: InputEvent):
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
print("click")

if Input.is_action_just_pressed("attack"):
GameUI.instance.status_bars.set_health(72, 100)
print("attack")

func _unhandled_input(event: InputEvent):
if event is InputEventMouseButton:
print("Mouse Click/Unclick at: ", event.position)
Expand Down Expand Up @@ -63,14 +63,16 @@ func handle_camera(event: InputEvent):
camera.position.z = pos_z_formula

func handle_inputs():
handle_attack()

# Handle jump.
if Input.is_action_just_pressed("jump") and is_on_floor():
velocity.y = JUMP_VELOCITY

# Get the input direction and handle movement.

var input_dir = Input.get_vector("left", "right", "forward", "back")
print("input dir: ", input_dir)
if(input_dir): print("input dir: ", input_dir)
var direction = (camera_pivot.transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
if direction:
velocity.x = direction.x * SPEED
Expand All @@ -82,3 +84,18 @@ func handle_inputs():
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
velocity.z = move_toward(velocity.z, 0, SPEED)

func handle_attack():
if Input.is_action_just_pressed("attack"):
# GameUI.instance.status_bars.set_health(72, 100)
var space = get_world_3d().direct_space_state
var query = PhysicsRayQueryParameters3D.create(camera.global_position,
camera.global_position - camera.global_transform.basis.z * 100, PhysicsUtils.arr_to_collision_mask(
[ PhysicsUtils.ENEMY_MASK ]
))
var collision = space.intersect_ray(query)
if collision:
print("Attacked:", collision.collider.name)
_attack(collision.collider)
else:
print("Collide with:", "nuthin")
17 changes: 17 additions & 0 deletions scripts/utils/physics.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class_name PhysicsUtils

const WALL_MASK: int = 1
const ATTACKABLE_MASK: int = 2
const PLAYER_MASK: int = 3
const NPC_MASK: int = 4
static var ENEMY_MASK: int = 5
const ITEM_MASK: int = 6

const GROUND_MASK: int = 9

# Converts an array of layer numbers to a collision mask.
static func arr_to_collision_mask(arr: PackedByteArray):
var val = 0
for num in arr:
val += pow(2, num - 1)
return val

0 comments on commit 5c242b2

Please sign in to comment.