From c7c4ace4ec10d71c850daa202492f5d80c28686d Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Sun, 3 Apr 2022 02:53:56 +0200 Subject: [PATCH] :broom: Clean up bullet shooting code --- project.godot | 2 +- src/entities/Player.tscn | 6 +++++- src/entities/behaviours/BulletMovement.gd | 3 ++- src/entities/behaviours/BulletSpawn.gd | 26 +++++++++++++---------- src/levels/Game.tscn | 6 +----- 5 files changed, 24 insertions(+), 19 deletions(-) diff --git a/project.godot b/project.godot index 9d50f67..8e7a2c7 100644 --- a/project.godot +++ b/project.godot @@ -61,7 +61,7 @@ player_move_right={ , Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":0,"physical_scancode":68,"unicode":0,"echo":false,"script":null) ] } -shoot={ +player_shoot={ "deadzone": 0.5, "events": [ Object(InputEventMouseButton,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"button_mask":0,"position":Vector2( 0, 0 ),"global_position":Vector2( 0, 0 ),"factor":1.0,"button_index":1,"pressed":false,"doubleclick":false,"script":null) ] diff --git a/src/entities/Player.tscn b/src/entities/Player.tscn index a3ca0e5..b80c31e 100644 --- a/src/entities/Player.tscn +++ b/src/entities/Player.tscn @@ -1,7 +1,8 @@ -[gd_scene load_steps=4 format=2] +[gd_scene load_steps=5 format=2] [ext_resource path="res://src/mechanics/White.png" type="Texture" id=1] [ext_resource path="res://src/entities/behaviours/PlayerMovement.tscn" type="PackedScene" id=2] +[ext_resource path="res://src/entities/behaviours/BulletSpawn.gd" type="Script" id=3] [sub_resource type="RectangleShape2D" id=1] extents = Vector2( 16, 16 ) @@ -15,3 +16,6 @@ texture = ExtResource( 1 ) [node name="Shape" type="CollisionShape2D" parent="."] shape = SubResource( 1 ) + +[node name="BulletSpawner" type="Node2D" parent="."] +script = ExtResource( 3 ) diff --git a/src/entities/behaviours/BulletMovement.gd b/src/entities/behaviours/BulletMovement.gd index 2fb4548..0817dbc 100644 --- a/src/entities/behaviours/BulletMovement.gd +++ b/src/entities/behaviours/BulletMovement.gd @@ -4,6 +4,7 @@ onready var target: KinematicBody2D = get_parent() export var movement_per_second: float = 400.0 -func _physics_process(delta): +func _physics_process(_delta): var direction = Vector2(cos(target.rotation), sin(target.rotation)) + # TODO: Maybe bullets shouldn't slide but collide var _motion: Vector2 = target.move_and_slide(direction * movement_per_second) diff --git a/src/entities/behaviours/BulletSpawn.gd b/src/entities/behaviours/BulletSpawn.gd index be97442..cb9569e 100644 --- a/src/entities/behaviours/BulletSpawn.gd +++ b/src/entities/behaviours/BulletSpawn.gd @@ -1,16 +1,20 @@ -extends Position2D +extends Node2D + + +export var bullet: PackedScene = preload("res://src/entities/Bullet.tscn") +export var bullet_container_node_path: String = "../.." +onready var bullet_container_node: Node = get_node(bullet_container_node_path) +onready var source: Node2D = get_parent() -# Items -onready var bullet = preload("res://src/entities/Bullet.tscn") -onready var spawner = get_node("/root/Game/PhaseOne/PhaseOneContainer/Player") func _process(_delta): - if Input.is_action_just_pressed("shoot"): - _on_Click() + if Input.is_action_just_pressed("player_shoot"): + shoot() -func _on_Click(): + +func shoot(): var new_bullet = bullet.instance() - new_bullet.set_position(spawner.position) - var bullet_rotation = new_bullet.get_angle_to(get_global_mouse_position()) - new_bullet.set_rotation(bullet_rotation) - add_child(new_bullet) + new_bullet.set_position(source.global_position) + bullet_container_node.add_child(new_bullet) + var rotation = new_bullet.get_angle_to(get_global_mouse_position()) + new_bullet.set_rotation(rotation) diff --git a/src/levels/Game.tscn b/src/levels/Game.tscn index f585c60..e7898b3 100644 --- a/src/levels/Game.tscn +++ b/src/levels/Game.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=10 format=2] +[gd_scene load_steps=9 format=2] [ext_resource path="res://src/mechanics/Background.tscn" type="PackedScene" id=1] [ext_resource path="res://src/mechanics/Darkness.tscn" type="PackedScene" id=2] @@ -7,7 +7,6 @@ [ext_resource path="res://src/entities/Enemy.tscn" type="PackedScene" id=5] [ext_resource path="res://src/levels/Game.gd" type="Script" id=6] [ext_resource path="res://src/ui/TimeSurvived.tscn" type="PackedScene" id=7] -[ext_resource path="res://src/entities/behaviours/BulletSpawn.gd" type="Script" id=8] [ext_resource path="res://src/levels/PhaseOneMusic.gd" type="Script" id=11] [node name="Game" type="Node2D"] @@ -50,9 +49,6 @@ position = Vector2( 755, 508 ) position = Vector2( 493, 305 ) intensity = 0.3 -[node name="Position2D" type="Position2D" parent="PhaseOne/PhaseOneContainer"] -script = ExtResource( 8 ) - [node name="UserInterface" type="CanvasLayer" parent="."] [node name="UserIntefaceContainer" type="Panel" parent="UserInterface"]