From f27937e6e9b6a7146dd09fc711d2d293ecf5abbf Mon Sep 17 00:00:00 2001 From: Daniel Weipert Date: Sun, 14 Apr 2024 21:49:20 +0200 Subject: update --- Scenes/Entities/Enemies/Components/Collision.gd | 18 ++++++++ Scenes/Entities/Enemies/Components/Collision.tscn | 6 +++ Scenes/Entities/Enemies/Components/Movement.gd | 56 +++++++++++++++++++++++ Scenes/Entities/Enemies/Components/Movement.tscn | 6 +++ 4 files changed, 86 insertions(+) create mode 100644 Scenes/Entities/Enemies/Components/Collision.gd create mode 100644 Scenes/Entities/Enemies/Components/Collision.tscn create mode 100644 Scenes/Entities/Enemies/Components/Movement.gd create mode 100644 Scenes/Entities/Enemies/Components/Movement.tscn (limited to 'Scenes/Entities/Enemies/Components') diff --git a/Scenes/Entities/Enemies/Components/Collision.gd b/Scenes/Entities/Enemies/Components/Collision.gd new file mode 100644 index 0000000..b2e8d9b --- /dev/null +++ b/Scenes/Entities/Enemies/Components/Collision.gd @@ -0,0 +1,18 @@ +extends Node + +class_name Component_Collision + + +signal collided + +@export_node_path("CharacterBody2D") var entityPath: NodePath +@onready var entity: CharacterBody2D = get_node(entityPath) + +@export_node_path("CollisionShape2D") var collision_shape_path: NodePath +@onready var collision_shape: CollisionShape2D = get_node(collision_shape_path) + + +func init(): + var collision_area = Utilities.Collision.Area.new(entity, collision_shape) + collision_area.connect("collided", func(area): emit_signal("collided", area)) + entity.add_child(collision_area) diff --git a/Scenes/Entities/Enemies/Components/Collision.tscn b/Scenes/Entities/Enemies/Components/Collision.tscn new file mode 100644 index 0000000..d11f927 --- /dev/null +++ b/Scenes/Entities/Enemies/Components/Collision.tscn @@ -0,0 +1,6 @@ +[gd_scene load_steps=2 format=3 uid="uid://ce3vv2pod6auc"] + +[ext_resource type="Script" path="res://Scenes/Entities/Enemies/Components/Collision.gd" id="1_j60o5"] + +[node name="Collision" type="Node"] +script = ExtResource("1_j60o5") diff --git a/Scenes/Entities/Enemies/Components/Movement.gd b/Scenes/Entities/Enemies/Components/Movement.gd new file mode 100644 index 0000000..0b30820 --- /dev/null +++ b/Scenes/Entities/Enemies/Components/Movement.gd @@ -0,0 +1,56 @@ +extends Node + +signal direction_changed + +@export_node_path("CharacterBody2D") var entityPath: NodePath +@onready var entity: CharacterBody2D = get_node(entityPath) + +@export_node_path("AnimatedSprite2D") var spritePath: NodePath +@onready var sprite: AnimatedSprite2D = get_node(spritePath) + +@export var SPEED: int + +const DIRECTIONS = [Vector2.UP, Vector2.RIGHT, Vector2.DOWN, Vector2.LEFT] +var CURRENT_DIRECTION = Vector2.UP + +@onready var MovementTimer: Timer = Timer.new() + + +func _ready(): + MovementTimer.wait_time = 3.0 + MovementTimer.autostart = true + MovementTimer.connect("timeout", Callable(_on_movement_timer_timeout)) + add_child(MovementTimer) + + +func physics_process(delta): + if CURRENT_DIRECTION == Vector2.UP: + entity.velocity.y -= SPEED + sprite.play("up") + elif CURRENT_DIRECTION == Vector2.DOWN: + entity.velocity.y += SPEED + sprite.play("down") + elif CURRENT_DIRECTION == Vector2.LEFT: + entity.velocity.x -= SPEED + sprite.play("left") + elif CURRENT_DIRECTION == Vector2.RIGHT: + entity.velocity.x += SPEED + sprite.play("right") + + var collision = entity.move_and_collide(entity.velocity * delta) + entity.velocity = entity.velocity.lerp(Vector2(0, 0), 1) + + return collision + + +func _on_movement_timer_timeout(): + var directions = DIRECTIONS.duplicate() + + directions.remove_at(directions.find(CURRENT_DIRECTION)) + directions.shuffle() + + CURRENT_DIRECTION = directions[0] + + emit_signal("direction_changed", CURRENT_DIRECTION) + + MovementTimer.start() diff --git a/Scenes/Entities/Enemies/Components/Movement.tscn b/Scenes/Entities/Enemies/Components/Movement.tscn new file mode 100644 index 0000000..f3d5909 --- /dev/null +++ b/Scenes/Entities/Enemies/Components/Movement.tscn @@ -0,0 +1,6 @@ +[gd_scene load_steps=2 format=3 uid="uid://cq7yj2av01tqd"] + +[ext_resource type="Script" path="res://Scenes/Entities/Enemies/Components/Movement.gd" id="1_mtee1"] + +[node name="Movement" type="Node"] +script = ExtResource("1_mtee1") -- cgit v1.2.3