moved direction change logic to game-controller
This commit is contained in:
parent
25cad172e0
commit
a563c0c93d
@ -1,19 +1,12 @@
|
|||||||
extends RigidBody2D
|
class_name Enemy extends RigidBody2D
|
||||||
|
|
||||||
var direction = 1
|
# there is probably a better built-in to use here
|
||||||
|
@export var direction:int = 1
|
||||||
const SPEED = 1
|
const SPEED = 1
|
||||||
|
|
||||||
# signal triggerEdge(body, intentMessage)
|
|
||||||
|
|
||||||
|
|
||||||
# Called when the node enters the scene tree for the first time.
|
|
||||||
func _ready():
|
func _ready():
|
||||||
pass # Replace with function body.
|
pass # Replace with function body.
|
||||||
|
|
||||||
func _process(delta):
|
func _process(_delta):
|
||||||
var x = get_transform().get_origin().x
|
var x = get_transform().get_origin().x
|
||||||
# not sure why rigid body doesn't see wall hardcoded walls
|
|
||||||
if x < 0 or x > 300:
|
|
||||||
# triggerEdge.emit(body, )
|
|
||||||
direction *= -1 # switch direction
|
|
||||||
move_local_x(SPEED * direction)
|
move_local_x(SPEED * direction)
|
||||||
|
|||||||
@ -1,14 +1,23 @@
|
|||||||
extends Node2D
|
extends Node2D
|
||||||
|
|
||||||
|
var game_node = null
|
||||||
|
|
||||||
# Called when the node enters the scene tree for the first time.
|
# Called when the node enters the scene tree for the first time.
|
||||||
func _ready():
|
func _ready():
|
||||||
get_window().grab_focus() # Replace with function body.
|
get_window().grab_focus() # Replace with function body.
|
||||||
|
game_node = get_tree().root.get_child(0)
|
||||||
|
|
||||||
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
func _process(_delta):
|
||||||
func _process(delta):
|
# detect when one enemy goes too far
|
||||||
pass
|
var change_dir = false
|
||||||
|
for child in game_node.get_children():
|
||||||
func _on_area_2d_trigger_active_signal(body, intentMessage):
|
if child is RigidBody2D:
|
||||||
print("game controller received trigger")
|
var x = child.transform.get_origin().x
|
||||||
#if intentMessage == "destroy" and not body is Player:
|
if x < 0 or x > 300: # not using collision detection properly
|
||||||
#body.queue_free()
|
change_dir = true
|
||||||
|
break
|
||||||
|
# change directions of all enemies
|
||||||
|
if change_dir:
|
||||||
|
for child in game_node.get_children():
|
||||||
|
if child is RigidBody2D:
|
||||||
|
child.mydirection *= -1
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
[gd_scene format=3 uid="uid://bpno3xoqtrcl8"]
|
[gd_scene format=3 uid="uid://bpno3xoqtrcl8"]
|
||||||
|
|
||||||
[ext_resource type="Script" uid="uid://cehhkd2b24fhi" path="res://scripts/player.gd" id="1_5oyi1"]
|
[ext_resource type="Script" uid="uid://cehhkd2b24fhi" path="res://scripts/player.gd" id="1_5oyi1"]
|
||||||
|
[ext_resource type="Script" uid="uid://cvax0kvnw574u" path="res://scripts/game-controller.gd" id="1_kv2i2"]
|
||||||
[ext_resource type="Script" uid="uid://c6x4gdnsnusgk" path="res://scripts/enemy.gd" id="2_kxkvm"]
|
[ext_resource type="Script" uid="uid://c6x4gdnsnusgk" path="res://scripts/enemy.gd" id="2_kxkvm"]
|
||||||
|
|
||||||
[sub_resource type="RectangleShape2D" id="RectangleShape2D_qlrl5"]
|
[sub_resource type="RectangleShape2D" id="RectangleShape2D_qlrl5"]
|
||||||
@ -13,6 +14,7 @@ size = Vector2(60, 20)
|
|||||||
[sub_resource type="RectangleShape2D" id="RectangleShape2D_5oyi1"]
|
[sub_resource type="RectangleShape2D" id="RectangleShape2D_5oyi1"]
|
||||||
|
|
||||||
[node name="Game" type="Node2D" unique_id=1637093307]
|
[node name="Game" type="Node2D" unique_id=1637093307]
|
||||||
|
script = ExtResource("1_kv2i2")
|
||||||
|
|
||||||
[node name="Block" type="StaticBody2D" parent="." unique_id=346683741]
|
[node name="Block" type="StaticBody2D" parent="." unique_id=346683741]
|
||||||
position = Vector2(117, 416)
|
position = Vector2(117, 416)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user