1
Fork 0
mirror of https://github.com/RYGhub/the-cold-night.git synced 2024-11-25 14:14:19 +00:00

🧹 Clean up bullet shooting code

This commit is contained in:
Steffo 2022-04-03 02:53:56 +02:00
parent 1546d40b53
commit c7c4ace4ec
Signed by: steffo
GPG key ID: 6965406171929D01
5 changed files with 24 additions and 19 deletions

View file

@ -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) , 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, "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) "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)
] ]

View file

@ -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/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/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] [sub_resource type="RectangleShape2D" id=1]
extents = Vector2( 16, 16 ) extents = Vector2( 16, 16 )
@ -15,3 +16,6 @@ texture = ExtResource( 1 )
[node name="Shape" type="CollisionShape2D" parent="."] [node name="Shape" type="CollisionShape2D" parent="."]
shape = SubResource( 1 ) shape = SubResource( 1 )
[node name="BulletSpawner" type="Node2D" parent="."]
script = ExtResource( 3 )

View file

@ -4,6 +4,7 @@ onready var target: KinematicBody2D = get_parent()
export var movement_per_second: float = 400.0 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)) 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) var _motion: Vector2 = target.move_and_slide(direction * movement_per_second)

View file

@ -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): func _process(_delta):
if Input.is_action_just_pressed("shoot"): if Input.is_action_just_pressed("player_shoot"):
_on_Click() shoot()
func _on_Click():
func shoot():
var new_bullet = bullet.instance() var new_bullet = bullet.instance()
new_bullet.set_position(spawner.position) new_bullet.set_position(source.global_position)
var bullet_rotation = new_bullet.get_angle_to(get_global_mouse_position()) bullet_container_node.add_child(new_bullet)
new_bullet.set_rotation(bullet_rotation) var rotation = new_bullet.get_angle_to(get_global_mouse_position())
add_child(new_bullet) new_bullet.set_rotation(rotation)

View file

@ -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/Background.tscn" type="PackedScene" id=1]
[ext_resource path="res://src/mechanics/Darkness.tscn" type="PackedScene" id=2] [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/entities/Enemy.tscn" type="PackedScene" id=5]
[ext_resource path="res://src/levels/Game.gd" type="Script" id=6] [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/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] [ext_resource path="res://src/levels/PhaseOneMusic.gd" type="Script" id=11]
[node name="Game" type="Node2D"] [node name="Game" type="Node2D"]
@ -50,9 +49,6 @@ position = Vector2( 755, 508 )
position = Vector2( 493, 305 ) position = Vector2( 493, 305 )
intensity = 0.3 intensity = 0.3
[node name="Position2D" type="Position2D" parent="PhaseOne/PhaseOneContainer"]
script = ExtResource( 8 )
[node name="UserInterface" type="CanvasLayer" parent="."] [node name="UserInterface" type="CanvasLayer" parent="."]
[node name="UserIntefaceContainer" type="Panel" parent="UserInterface"] [node name="UserIntefaceContainer" type="Panel" parent="UserInterface"]