From 485de5791b9db23f0f070d4599f06395318b6b89 Mon Sep 17 00:00:00 2001 From: Hamcha Date: Tue, 7 Jul 2020 09:40:20 +0200 Subject: [PATCH] Add activation range and leave events --- Actors/Components/ActivationRange.gd | 21 ++++++++++++++++ Actors/Objects/Computer/Computer.gd | 23 ++++++++++++----- Actors/Objects/Computer/Computer.tscn | 14 ++++++++++- Actors/Objects/Door/Door.gd | 12 ++++++--- Actors/Objects/Door/Door.tscn | 14 ++++++++++- Actors/{ => Objects}/Lightbulb/Lightbulb.gd | 17 ++++++++----- Actors/{ => Objects}/Lightbulb/Lightbulb.tscn | 25 +++++++++++++------ Scenes/Maps/odyssey.tscn | 5 ++-- project.godot | 6 +++++ 9 files changed, 110 insertions(+), 27 deletions(-) create mode 100644 Actors/Components/ActivationRange.gd rename Actors/{ => Objects}/Lightbulb/Lightbulb.gd (78%) rename Actors/{ => Objects}/Lightbulb/Lightbulb.tscn (56%) diff --git a/Actors/Components/ActivationRange.gd b/Actors/Components/ActivationRange.gd new file mode 100644 index 0000000..147cef6 --- /dev/null +++ b/Actors/Components/ActivationRange.gd @@ -0,0 +1,21 @@ +extends Area2D + +class_name ActivationRange + +signal player_entered() +signal player_left() + +func _ready(): + connect("body_entered", self, "_body_entered") + connect("body_exited", self, "_body_left") + +func in_range(): + return overlaps_body($"/root/scene/world".player) + +func _body_entered(body: Node): + if body == $"/root/scene/world".player: + emit_signal("player_entered") + +func _body_left(body: Node): + if body == $"/root/scene/world".player: + emit_signal("player_left") diff --git a/Actors/Objects/Computer/Computer.gd b/Actors/Objects/Computer/Computer.gd index cffc599..d60d9fe 100644 --- a/Actors/Objects/Computer/Computer.gd +++ b/Actors/Objects/Computer/Computer.gd @@ -9,6 +9,9 @@ export(Direction) var direction = Direction.DOWN setget set_direction export(ComputerType) var computer_type = ComputerType.ShipCommand setget set_type var screen_region_offset = Vector2.ZERO +var open = false + +onready var activationRange = $ActivationRange as ActivationRange func set_type(val): computer_type = val @@ -52,22 +55,30 @@ func manage_controls(show: bool): $Control/ControlComp.visible = show func _input(event): - if event is InputEventMouseButton and event.pressed and not is_inside: + if event is InputEventMouseButton and event.pressed and not is_inside and open: + open = false $UIAnimation.play("fadeout") func _input_event(viewport, event, shape_idx): if Engine.editor_hint: return - if event is InputEventMouseButton and event.pressed: - manage_controls(true) - $UIAnimation.play("fadein") + if event is InputEventMouseButton and event.pressed and not open: + # Must be in range + if activationRange.in_range(): + open = true + manage_controls(true) + $UIAnimation.play("fadein") var is_inside = false func _ui_focus_changed(entered): - print(entered) is_inside = entered - func _ui_fade_completed(anim_name): if anim_name == "fadeout": manage_controls(false) + + +func _force_close_ui(): + if open: + open = false + $UIAnimation.play("fadeout") diff --git a/Actors/Objects/Computer/Computer.tscn b/Actors/Objects/Computer/Computer.tscn index 43a1c89..58227e0 100644 --- a/Actors/Objects/Computer/Computer.tscn +++ b/Actors/Objects/Computer/Computer.tscn @@ -1,10 +1,11 @@ -[gd_scene load_steps=12 format=2] +[gd_scene load_steps=14 format=2] [ext_resource path="res://Graphics/tgstation/computer-screens.png" type="Texture" id=1] [ext_resource path="res://Graphics/tgstation/computer.png" type="Texture" id=2] [ext_resource path="res://Actors/Objects/Computer/Computer.gd" type="Script" id=3] [ext_resource path="res://Actors/Objects/Computer/UI/ControlComp.tscn" type="PackedScene" id=4] [ext_resource path="res://Graphics/light_shadow_light.png" type="Texture" id=5] +[ext_resource path="res://Actors/Components/ActivationRange.gd" type="Script" id=6] [sub_resource type="CanvasItemMaterial" id=1] blend_mode = 1 @@ -94,6 +95,9 @@ tracks/1/keys = { "times": PoolRealArray( 0, 0.2 ) } +[sub_resource type="RectangleShape2D" id=7] +extents = Vector2( 48, 48 ) + [node name="Computer" type="StaticBody2D"] input_pickable = true script = ExtResource( 3 ) @@ -140,6 +144,14 @@ margin_top = -72.3908 margin_right = 166.574 margin_bottom = 59.6092 rect_scale = Vector2( 0.5, 0.5 ) + +[node name="ActivationRange" type="Area2D" parent="."] +script = ExtResource( 6 ) + +[node name="CollisionShape2D" type="CollisionShape2D" parent="ActivationRange"] +position = Vector2( 16, 16 ) +shape = SubResource( 7 ) [connection signal="animation_finished" from="UIAnimation" to="." method="_ui_fade_completed"] [connection signal="mouse_entered" from="Control/ControlComp" to="." method="_ui_focus_changed" binds= [ true ]] [connection signal="mouse_exited" from="Control/ControlComp" to="." method="_ui_focus_changed" binds= [ false ]] +[connection signal="player_left" from="ActivationRange" to="." method="_force_close_ui"] diff --git a/Actors/Objects/Door/Door.gd b/Actors/Objects/Door/Door.gd index c4a4b32..cdda257 100644 --- a/Actors/Objects/Door/Door.gd +++ b/Actors/Objects/Door/Door.gd @@ -1,7 +1,12 @@ extends StaticBody2D +onready var activationRange = $ActivationRange as ActivationRange + signal changed(open) +func _ready(): + activationRange.visible = true + func set_open(open: bool): if open: $Sprite.play("open") @@ -21,10 +26,11 @@ func _animation_finished(): collision_layer = 1 func _input_event(viewport, event, shape_idx): + # Check if we clicked the item if event is InputEventMouseButton and event.pressed: - set_open($Sprite.animation == "close") - - + # Must be in activation range + if activationRange.in_range(): + set_open($Sprite.animation == "close") func _close_timer_triggered(): $Sprite.play("close") diff --git a/Actors/Objects/Door/Door.tscn b/Actors/Objects/Door/Door.tscn index 251e72d..a1f39f2 100644 --- a/Actors/Objects/Door/Door.tscn +++ b/Actors/Objects/Door/Door.tscn @@ -1,7 +1,8 @@ -[gd_scene load_steps=10 format=2] +[gd_scene load_steps=12 format=2] [ext_resource path="res://Actors/Objects/Door/Door.gd" type="Script" id=1] [ext_resource path="res://Graphics/tgstation/opening-sheet.png" type="Texture" id=2] +[ext_resource path="res://Actors/Components/ActivationRange.gd" type="Script" id=3] [sub_resource type="AtlasTexture" id=1] atlas = ExtResource( 2 ) @@ -39,6 +40,9 @@ animations = [ { [sub_resource type="RectangleShape2D" id=7] extents = Vector2( 16, 16 ) +[sub_resource type="CircleShape2D" id=8] +radius = 64.0 + [node name="Door" type="StaticBody2D"] input_pickable = true script = ExtResource( 1 ) @@ -57,5 +61,13 @@ shape = SubResource( 7 ) [node name="Timer" type="Timer" parent="."] wait_time = 5.0 one_shot = true + +[node name="ActivationRange" type="Area2D" parent="."] +visible = false +script = ExtResource( 3 ) + +[node name="CollisionShape2D" type="CollisionShape2D" parent="ActivationRange"] +position = Vector2( 16, 16 ) +shape = SubResource( 8 ) [connection signal="animation_finished" from="Sprite" to="." method="_animation_finished"] [connection signal="timeout" from="Timer" to="." method="_close_timer_triggered"] diff --git a/Actors/Lightbulb/Lightbulb.gd b/Actors/Objects/Lightbulb/Lightbulb.gd similarity index 78% rename from Actors/Lightbulb/Lightbulb.gd rename to Actors/Objects/Lightbulb/Lightbulb.gd index 2e7b090..0702c99 100644 --- a/Actors/Lightbulb/Lightbulb.gd +++ b/Actors/Objects/Lightbulb/Lightbulb.gd @@ -5,9 +5,10 @@ extends Area2D enum Direction { LEFT, RIGHT, UP, DOWN } export(Direction) var direction = Direction.DOWN setget set_direction - export var lit = true setget set_lit +onready var activationRange = $ActivationRange as ActivationRange + func _ready(): $Light2D.set_meta("_edit_lock_", true) @@ -21,26 +22,30 @@ func set_lit(val): refresh_sprite() func refresh_sprite(): + var rot = 0 var lit_offset = 0 if not lit: lit_offset = 32 match direction: Direction.DOWN: $light.region_rect.position = Vector2(32, lit_offset) - $Light2D.rotation = 0 + rot =0 Direction.UP: $light.region_rect.position = Vector2(0, lit_offset) - $Light2D.rotation = PI + rot = PI Direction.LEFT: $light.region_rect.position = Vector2(96, lit_offset) - $Light2D.rotation = -PI/2 + rot = -PI/2 Direction.RIGHT: $light.region_rect.position = Vector2(64, lit_offset) - $Light2D.rotation = PI/2 + rot = PI/2 + $Light2D.rotation = rot + $ActivationRange.rotation = rot func _input_event(viewport, event, shape_idx): if Engine.editor_hint: return if event is InputEventMouseButton and event.pressed: - set_lit(!lit) + if activationRange.in_range(): + set_lit(!lit) diff --git a/Actors/Lightbulb/Lightbulb.tscn b/Actors/Objects/Lightbulb/Lightbulb.tscn similarity index 56% rename from Actors/Lightbulb/Lightbulb.tscn rename to Actors/Objects/Lightbulb/Lightbulb.tscn index f70f5dc..da111d1 100644 --- a/Actors/Lightbulb/Lightbulb.tscn +++ b/Actors/Objects/Lightbulb/Lightbulb.tscn @@ -1,22 +1,26 @@ -[gd_scene load_steps=5 format=2] +[gd_scene load_steps=7 format=2] -[ext_resource path="res://Graphics/tgstation/light.png" type="Texture" id=1] -[ext_resource path="res://Actors/Lightbulb/Lightbulb.gd" type="Script" id=2] -[ext_resource path="res://Graphics/light_shadow_light.png" type="Texture" id=3] +[ext_resource path="res://Graphics/light_shadow_light.png" type="Texture" id=1] +[ext_resource path="res://Graphics/tgstation/light.png" type="Texture" id=2] +[ext_resource path="res://Actors/Objects/Lightbulb/Lightbulb.gd" type="Script" id=3] +[ext_resource path="res://Actors/Components/ActivationRange.gd" type="Script" id=4] [sub_resource type="RectangleShape2D" id=1] extents = Vector2( 16, 16 ) +[sub_resource type="RectangleShape2D" id=2] +extents = Vector2( 48, 32 ) + [node name="Lighttube" type="Area2D"] -script = ExtResource( 2 ) +script = ExtResource( 3 ) [node name="light" type="Sprite" parent="."] -texture = ExtResource( 1 ) +texture = ExtResource( 2 ) region_enabled = true region_rect = Rect2( 32, 0, 32, 32 ) [node name="Light2D" type="Light2D" parent="."] -texture = ExtResource( 3 ) +texture = ExtResource( 1 ) offset = Vector2( 0, -10 ) texture_scale = 4.0 energy = 1.2 @@ -32,3 +36,10 @@ __meta__ = { [node name="CollisionShape2D" type="CollisionShape2D" parent="."] shape = SubResource( 1 ) + +[node name="ActivationRange" type="Area2D" parent="."] +script = ExtResource( 4 ) + +[node name="CollisionShape2D" type="CollisionShape2D" parent="ActivationRange"] +position = Vector2( 0, 16 ) +shape = SubResource( 2 ) diff --git a/Scenes/Maps/odyssey.tscn b/Scenes/Maps/odyssey.tscn index 557d231..ad49ded 100644 --- a/Scenes/Maps/odyssey.tscn +++ b/Scenes/Maps/odyssey.tscn @@ -7,7 +7,7 @@ [ext_resource path="res://Scenes/Map.gd" type="Script" id=5] [ext_resource path="res://Actors/Objects/Computer/Computer.tscn" type="PackedScene" id=6] [ext_resource path="res://Graphics/space.png" type="Texture" id=7] -[ext_resource path="res://Actors/Lightbulb/Lightbulb.tscn" type="PackedScene" id=8] +[ext_resource path="res://Actors/Objects/Lightbulb/Lightbulb.tscn" type="PackedScene" id=8] [sub_resource type="CanvasItemMaterial" id=1] light_mode = 1 @@ -17,7 +17,6 @@ script = ExtResource( 5 ) tilemap_path = NodePath("2x2") [node name="CanvasModulate" type="CanvasModulate" parent="."] -visible = false color = Color( 0, 0, 0, 1 ) __meta__ = { "_edit_group_": true, @@ -69,6 +68,7 @@ position = Vector2( 192, 384 ) [node name="ControlComp" parent="objects" instance=ExtResource( 6 )] position = Vector2( 576, 256 ) direction = 0 +computer_type = 0 [node name="CommsComp" parent="objects" instance=ExtResource( 6 )] position = Vector2( 576, 224 ) @@ -76,7 +76,6 @@ direction = 0 computer_type = 1 [node name="lights" type="Node2D" parent="."] -visible = false __meta__ = { "_edit_group_": true, "_edit_lock_": true diff --git a/project.godot b/project.godot index 02d42cf..588cdc5 100644 --- a/project.godot +++ b/project.godot @@ -9,6 +9,11 @@ config_version=4 _global_script_classes=[ { +"base": "Area2D", +"class": "ActivationRange", +"language": "GDScript", +"path": "res://Actors/Components/ActivationRange.gd" +}, { "base": "Node", "class": "GameInstance", "language": "GDScript", @@ -45,6 +50,7 @@ _global_script_classes=[ { "path": "res://Classes/UICommand.gd" } ] _global_script_class_icons={ +"ActivationRange": "", "GameInstance": "", "GameUI": "", "GameWorld": "",