Skip to content

Commit

Permalink
Lock system (#36)
Browse files Browse the repository at this point in the history
* Added lock system and fixed a few small spellings

* When locked, player will fall etc, but stop walking

* Also repathed the project.godot icon to prevent an error, and added a few debug functions to LockSystem

---------

Co-authored-by: Adam Courtney <adam@thefunnybrain.com>
  • Loading branch information
TheFunnyBrain and Adam Courtney authored Sep 17, 2023
1 parent bdc1243 commit ab13a8e
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 8 deletions.
29 changes: 27 additions & 2 deletions AMSG_Examples/Player/Player.tscn
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[gd_scene load_steps=44 format=3 uid="uid://c2l3xxr5mfm6w"]
[gd_scene load_steps=45 format=3 uid="uid://c2l3xxr5mfm6w"]

[ext_resource type="PackedScene" uid="uid://cth47yn1duvdh" path="res://AMSG_Examples/Character/mixamo_character.tscn" id="1_ihvpu"]
[ext_resource type="PackedScene" uid="uid://bfkejs88xm680" path="res://addons/PoseWarping/MotionWarping.tscn" id="2_0jifq"]
Expand All @@ -16,6 +16,7 @@
[ext_resource type="Texture2D" uid="uid://dpkisrpco54qj" path="res://AMSG_Examples/Player/UI/Debug/circle.png" id="7_n2d8i"]
[ext_resource type="Texture2D" uid="uid://ijv6wr22hov8" path="res://AMSG_Examples/Player/UI/Debug/direction.png" id="8_t071y"]
[ext_resource type="Texture2D" uid="uid://ck5riwbh478s4" path="res://AMSG_Examples/Player/UI/Debug/velocity.png" id="9_464xm"]
[ext_resource type="Script" path="res://addons/AMSG/Components/LockComponent/LockSystem.gd" id="11_06pi7"]

[sub_resource type="AnimationNodeAnimation" id="AnimationNodeAnimation_mb2kt"]
animation = &"TurnLeft"
Expand Down Expand Up @@ -238,12 +239,16 @@ targeting_component = NodePath("../TargetingComponent")
tilt_power = 0.0
deacceleration = 5.0

[node name="PlayerController" type="Node" parent="." index="6" node_paths=PackedStringArray("character_component", "networking")]
[node name="PlayerController" type="Node" parent="." index="6" node_paths=PackedStringArray("character_component", "networking", "lock_system")]
script = ExtResource("4_7eusw")
OnePressJump = true
UsingCrouchToggle = true
character_component = NodePath("../CharacterMovementComponent")
networking = NodePath("../Networking")
lock_system = NodePath("LockSystem")

[node name="LockSystem" type="Node" parent="PlayerController" index="0"]
script = ExtResource("11_06pi7")

[node name="Networking" parent="." index="7" instance=ExtResource("4_8jv5v")]
character_movement_component = NodePath("../CharacterMovementComponent")
Expand Down Expand Up @@ -457,3 +462,23 @@ offset_bottom = 34.0
theme_override_styles/background = SubResource("StyleBoxFlat_agyl7")
theme_override_styles/fill = SubResource("StyleBoxFlat_ueab0")
show_percentage = false

[node name="PauseText" type="Label" parent="Status" index="9"]
visible = false
layout_mode = 1
anchors_preset = 8
anchor_left = 0.5
anchor_top = 0.5
anchor_right = 0.5
anchor_bottom = 0.5
offset_left = -138.0
offset_top = -13.0
offset_right = 138.0
offset_bottom = 13.0
grow_horizontal = 2
grow_vertical = 2
text = "Character locked. Press P to unlock."
horizontal_alignment = 1
vertical_alignment = 1

[connection signal="Lock_Status_Changed" from="PlayerController/LockSystem" to="Status/PauseText" method="set_visible"]
22 changes: 18 additions & 4 deletions AMSG_Examples/Player/PlayerController.gd
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,13 @@ var h_rotation :float
var previous_rotation_mode
var direction := Vector3.ZERO

#####################################
#Locks System
@export var lock_system : LockSystem
#####################################

@export var mouse_sensitivity : float = 0.01

@export var mouse_sensitvity : float = 0.01



Expand All @@ -43,7 +48,11 @@ func possess_character(p_character_component:CharacterMovementComponent,control:
func _physics_process(delta):
if !networking.is_local_authority():
return


if lock_system != null && lock_system.is_locked:
direction = Vector3.ZERO
character_component.add_movement_input()
return

#------------------ Input Movement ------------------#
h_rotation = character_component.camera_root.HObject.transform.basis.get_euler().y
Expand Down Expand Up @@ -132,8 +141,8 @@ func _input(event):
if event is InputEventMouseMotion:
if !character_component or !controls_the_possessed_character:
return
character_component.camera_root.camera_h += -event.relative.x * mouse_sensitvity
character_component.camera_root.camera_v += -event.relative.y * mouse_sensitvity
character_component.camera_root.camera_h += -event.relative.x * mouse_sensitivity
character_component.camera_root.camera_v += -event.relative.y * mouse_sensitivity
#------------------ Motion Warping test------------------#
if event.is_action_pressed("fire"):
character_component.anim_ref.active = false
Expand Down Expand Up @@ -173,3 +182,8 @@ func _input(event):
if character_component.camera_root.view_mode == Global.view_mode.first_person:
character_component.camera_root.view_mode = Global.view_mode.third_person

if(Input.is_action_pressed("pause")):
if(lock_system.contains_lock("pauseGame")):
lock_system.remove_lock("pauseGame")
else:
lock_system.add_lock("pauseGame")
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ https://youtube.com/playlist?list=PLdcL5aF8ZcJvCyqWeCBYVGKbQgrQngen3) .

(C) One Press : Switch Camera Angle (Right Shoulder,Left Shoulder,Head(Center) )

(P) Pause : Toggles a lock on player movement, and shows a message on-screen

(Space) Jump

Expand Down
2 changes: 1 addition & 1 deletion addons/AMSG/Components/CharacterMovementComponent.gd
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class_name CharacterMovementComponent


#####################################
@export_category("Refrences")
@export_category("References")
#Refrences

## Refrence to character mesh, should be assigned to a [Node3D] that is a parent to the actual mesh (Skeleton3D)
Expand Down
70 changes: 70 additions & 0 deletions addons/AMSG/Components/LockComponent/LockSystem.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
extends Node
class_name LockSystem

#The array of locks that are presently applied. The player can perform an action if this array is empty.
#The locks can be used for any manner of things: player movement in cutscenes, restricting dialogue choices, door open conditions etc
var _locks = []

var lock_count : int :
get:
return _locks.size()

#An event to hook into - primarily for debugging.
signal Lock_Added(lockName:String)
signal Lock_Removed(lockName:String)

#This one should only emit if is_locked would have changed.
signal Lock_Status_Changed(newStatus:bool)

#A getter to see if any locks are being applied
@export var is_locked : bool :
get:
return _check_is_locked()

#If a lock called lock_name hasn't already been added, adds one.
func add_lock(lock_name:String):
#Don't add duplicate locks
if(contains_lock(lock_name)):
print_debug("Lock %lock is already added." % lock_name)
return
else:
#Add locks and emit events
_locks.append(lock_name)
emit_signal("Lock_Added", lock_name)
#if this is the first and only lock, the locked status has changed to true
if(_locks.size() == 1):
Lock_Status_Changed.emit(true)
return;

#Removes a lock with the name lock_name. Prints a message if it's not in there.
func remove_lock(lock_name:String):
if(contains_lock(lock_name)):
_locks.erase(lock_name)
#If there's now zero locks remaining, emit event
if(_locks.size() == 0):
Lock_Status_Changed.emit(false)
else:
print_debug("Lock %lock cannot be removed as it isn't there." % lock_name)

#Returns true if _locks has any entries added, false if no locks are being applied
func _check_is_locked():
return _locks.size() > 0;

#Returns true if a lock called lock_name is already added to _locks
func contains_lock(lock_name:String):
for lock in _locks:
if lock == lock_name:
return true;
return false;

#Prints all current locks - useful for tracking down issues when locks haven't been lifted
func debug_locks():
var log = "Printing all locks"
for lock in _locks:
print_debug("\n" + str(lock))

#To be used for debug - for when the locks need to be bypassed to test.
func debug_release_all_locks():
for lock in _locks:
Lock_Removed.emit(lock)
_locks.clear();
7 changes: 6 additions & 1 deletion project.godot
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ config_version=5
config/name="AMSG"
run/main_scene="res://AMSG_Examples/Maps/MovementTestMap.tscn"
config/features=PackedStringArray("4.1")
config/icon="res://icon.png"
config/icon="res://AMSG_Examples/icon.png"

[autoload]

Expand Down Expand Up @@ -122,6 +122,11 @@ exit={
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":4194305,"key_label":0,"unicode":0,"echo":false,"script":null)
]
}
pause={
"deadzone": 0.5,
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":80,"physical_keycode":0,"key_label":0,"unicode":112,"echo":false,"script":null)
]
}

[rendering]

Expand Down

0 comments on commit ab13a8e

Please sign in to comment.