working bullet factory
This commit is contained in:
parent
80a771f3a9
commit
205e8d43b5
@ -26,11 +26,13 @@ folder_colors={
|
||||
right={
|
||||
"deadzone": 0.5,
|
||||
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":68,"key_label":0,"unicode":100,"location":0,"echo":false,"script":null)
|
||||
, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":4194321,"key_label":0,"unicode":0,"location":0,"echo":false,"script":null)
|
||||
]
|
||||
}
|
||||
left={
|
||||
"deadzone": 0.5,
|
||||
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":65,"key_label":0,"unicode":97,"location":0,"echo":false,"script":null)
|
||||
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":4194319,"key_label":0,"unicode":0,"location":0,"echo":false,"script":null)
|
||||
, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":65,"key_label":0,"unicode":97,"location":0,"echo":false,"script":null)
|
||||
]
|
||||
}
|
||||
jump={
|
||||
@ -43,3 +45,8 @@ shove={
|
||||
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":88,"key_label":0,"unicode":120,"location":0,"echo":false,"script":null)
|
||||
]
|
||||
}
|
||||
shoot={
|
||||
"deadzone": 0.5,
|
||||
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":90,"key_label":0,"unicode":122,"location":0,"echo":false,"script":null)
|
||||
]
|
||||
}
|
||||
|
15
dariusgodotproject/scenes/bullet.tscn
Normal file
15
dariusgodotproject/scenes/bullet.tscn
Normal file
@ -0,0 +1,15 @@
|
||||
[gd_scene load_steps=3 format=3 uid="uid://dwfife67i1fmr"]
|
||||
|
||||
[ext_resource type="Script" path="res://scripts/bullet.gd" id="1_qivxv"]
|
||||
|
||||
[sub_resource type="RectangleShape2D" id="RectangleShape2D_dp02a"]
|
||||
size = Vector2(16, 4)
|
||||
|
||||
[node name="Area2D" type="Area2D"]
|
||||
script = ExtResource("1_qivxv")
|
||||
|
||||
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
|
||||
shape = SubResource("RectangleShape2D_dp02a")
|
||||
debug_color = Color(1, 1, 0, 0.419608)
|
||||
|
||||
[connection signal="body_entered" from="." to="." method="_on_body_entered"]
|
@ -2,7 +2,7 @@
|
||||
|
||||
[sub_resource type="RectangleShape2D" id="RectangleShape2D_ixlbc"]
|
||||
|
||||
[node name="RigidBody2D" type="RigidBody2D"]
|
||||
[node name="RigidBody2D" type="RigidBody2D" groups=["destructable"]]
|
||||
metadata/_edit_group_ = true
|
||||
|
||||
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
|
||||
|
@ -1,9 +1,11 @@
|
||||
[gd_scene load_steps=8 format=3 uid="uid://dcmyx5g6dksl8"]
|
||||
[gd_scene load_steps=10 format=3 uid="uid://dcmyx5g6dksl8"]
|
||||
|
||||
[ext_resource type="PackedScene" uid="uid://d1ej2kiy7jcpv" path="res://scenes/crate.tscn" id="1_uivx3"]
|
||||
[ext_resource type="Script" path="res://scripts/gamecontroller.gd" id="1_vhl00"]
|
||||
[ext_resource type="Script" path="res://scripts/trigger.gd" id="3_2qbah"]
|
||||
[ext_resource type="PackedScene" uid="uid://b75mow511wmmb" path="res://scenes/player.tscn" id="3_8h5jv"]
|
||||
[ext_resource type="PackedScene" uid="uid://dwfife67i1fmr" path="res://scenes/bullet.tscn" id="5_7yfvg"]
|
||||
[ext_resource type="Script" path="res://scripts/scenemanager.gd" id="6_n2kp0"]
|
||||
|
||||
[sub_resource type="RectangleShape2D" id="RectangleShape2D_1cyeq"]
|
||||
size = Vector2(101, 20)
|
||||
@ -57,5 +59,12 @@ metadata/_edit_group_ = true
|
||||
shape = SubResource("CircleShape2D_mfh1j")
|
||||
debug_color = Color(0.825765, 0.00158581, 0.954354, 0.42)
|
||||
|
||||
[node name="Area2D2" parent="." instance=ExtResource("5_7yfvg")]
|
||||
position = Vector2(309, 222)
|
||||
|
||||
[node name="SceneManager" type="Node" parent="."]
|
||||
unique_name_in_owner = true
|
||||
script = ExtResource("6_n2kp0")
|
||||
|
||||
[connection signal="areaTrigger" from="Area2D" to="." method="_on_trigger"]
|
||||
[connection signal="body_entered" from="Area2D" to="Area2D" method="_on_body_entered"]
|
||||
|
@ -4,7 +4,7 @@
|
||||
|
||||
[sub_resource type="CircleShape2D" id="CircleShape2D_i8a0m"]
|
||||
|
||||
[node name="CharacterBody2D" type="CharacterBody2D"]
|
||||
[node name="CharacterBody2D" type="CharacterBody2D" groups=["player"]]
|
||||
script = ExtResource("1_racyk")
|
||||
metadata/_edit_group_ = true
|
||||
|
||||
@ -17,3 +17,9 @@ target_position = Vector2(25, 0)
|
||||
|
||||
[node name="LeftRay" type="RayCast2D" parent="."]
|
||||
target_position = Vector2(-25, 0)
|
||||
|
||||
[node name="RightTarget" type="Node2D" parent="."]
|
||||
position = Vector2(10, -5)
|
||||
|
||||
[node name="LeftTarget" type="Node2D" parent="."]
|
||||
position = Vector2(-10, -5)
|
||||
|
24
dariusgodotproject/scripts/bullet.gd
Normal file
24
dariusgodotproject/scripts/bullet.gd
Normal file
@ -0,0 +1,24 @@
|
||||
class_name Bullet extends Area2D
|
||||
|
||||
var speed = 700
|
||||
signal hit
|
||||
|
||||
# Called when the node enters the scene tree for the first time.
|
||||
func _ready() -> void:
|
||||
pass # Replace with function body.
|
||||
|
||||
|
||||
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
||||
func _process(delta: float) -> void:
|
||||
pass
|
||||
|
||||
func _physics_process(delta: float) -> void:
|
||||
position += transform.x * speed * delta
|
||||
func setSpeed(value):
|
||||
speed = value
|
||||
|
||||
|
||||
func _on_body_entered(body: Node2D) -> void:
|
||||
print("Bullet hits something")
|
||||
if not body.is_in_group("player"):
|
||||
hit.emit(body)
|
@ -14,6 +14,12 @@ var pushRightEnabled = false
|
||||
var pushLeftEnabled = false
|
||||
@export var PUSH_FORCE = 700
|
||||
|
||||
# Bullet Attack Variables
|
||||
@onready var right_target: Node2D = $RightTarget
|
||||
@onready var left_target: Node2D = $LeftTarget
|
||||
|
||||
|
||||
|
||||
func _physics_process(delta: float) -> void:
|
||||
# Add the gravity.
|
||||
if not is_on_floor():
|
||||
@ -29,6 +35,18 @@ func _physics_process(delta: float) -> void:
|
||||
pushTarget.apply_central_impulse(Vector2(1,0) * PUSH_FORCE * 2)
|
||||
if pushLeftEnabled && faceLeft == true:
|
||||
pushTarget.apply_central_impulse(Vector2(-1,0) * PUSH_FORCE * 2)
|
||||
# Handle Shooting
|
||||
if Input.is_action_just_pressed("shoot"):
|
||||
#print("I want to shoot")
|
||||
#make a new bullet
|
||||
var myBullet = %SceneManager.bulletFactory()
|
||||
|
||||
#var myBullet = bullet.instantiate()
|
||||
owner.add_child(myBullet)
|
||||
myBullet.transform = right_target.global_transform
|
||||
if faceLeft == true:
|
||||
myBullet.transform = left_target.global_transform
|
||||
myBullet.setSpeed(-700)
|
||||
|
||||
# Get the input direction and handle the movement/deceleration.
|
||||
# As good practice, you should replace UI actions with custom gameplay actions.
|
||||
|
@ -33,3 +33,8 @@ func _on_trigger(effect: Variant, body) -> void:
|
||||
if boxTotal <=0:
|
||||
print("YOU WON!!")
|
||||
get_tree().reload_current_scene()
|
||||
|
||||
func bulletHit(body):
|
||||
print("Game controller knows bullet hit something")
|
||||
if body.is_in_group("destructable"):
|
||||
body.queue_free()
|
||||
|
22
dariusgodotproject/scripts/scenemanager.gd
Normal file
22
dariusgodotproject/scripts/scenemanager.gd
Normal file
@ -0,0 +1,22 @@
|
||||
extends Node
|
||||
@onready var game: Node2D = $".."
|
||||
|
||||
var bullet = preload("res://scenes/bullet.tscn")
|
||||
# Called when the node enters the scene tree for the first time.
|
||||
func _ready() -> void:
|
||||
pass # Replace with function body.
|
||||
|
||||
|
||||
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
||||
func _process(delta: float) -> void:
|
||||
pass
|
||||
|
||||
func bulletFactory():
|
||||
print("Factory will make a bullet")
|
||||
var myBullet = bullet.instantiate()
|
||||
myBullet.connect("hit", onBulletHit)
|
||||
return myBullet
|
||||
|
||||
func onBulletHit(body):
|
||||
print("Scene manager knows bullet hit")
|
||||
game.bulletHit(body)
|
Loading…
Reference in New Issue
Block a user