bullet factory, push attack

This commit is contained in:
Darius 2025-03-03 20:40:27 -05:00
parent 86a099e02f
commit adf15b6014
7 changed files with 228 additions and 66 deletions

View File

@ -20,3 +20,26 @@ config/icon="res://icon.svg"
folder_colors={
"res://scenes/": "red"
}
[input]
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)
]
}
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)
]
}
shove={
"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":76,"key_label":0,"unicode":108,"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":77,"key_label":0,"unicode":109,"location":0,"echo":false,"script":null)
]
}

View File

@ -0,0 +1,15 @@
[gd_scene load_steps=3 format=3 uid="uid://dwy0c50ndnu8k"]
[ext_resource type="Script" path="res://scripts/bullet.gd" id="1_v85g0"]
[sub_resource type="RectangleShape2D" id="RectangleShape2D_pwf7j"]
size = Vector2(20, 4)
[node name="Area2D" type="Area2D"]
script = ExtResource("1_v85g0")
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
shape = SubResource("RectangleShape2D_pwf7j")
debug_color = Color(0.932915, 0.177129, 0.479323, 0.42)
[connection signal="body_entered" from="." to="." method="_on_body_entered"]

View File

@ -1,72 +1,25 @@
[gd_scene load_steps=9 format=3 uid="uid://dhopuj38ic6dp"]
[gd_scene load_steps=10 format=3 uid="uid://dhopuj38ic6dp"]
[ext_resource type="Script" path="res://scripts/trigger.gd" id="1_7cb3s"]
[ext_resource type="Script" path="res://scripts/gamecontroller.gd" id="1_dasmg"]
[ext_resource type="Script" path="res://scripts/player.gd" id="2_7k2el"]
[sub_resource type="RectangleShape2D" id="RectangleShape2D_uopew"]
size = Vector2(70, 26)
[sub_resource type="RectangleShape2D" id="RectangleShape2D_oospb"]
size = Vector2(43.1503, 26)
[sub_resource type="WorldBoundaryShape2D" id="WorldBoundaryShape2D_igau3"]
[ext_resource type="PackedScene" uid="uid://du8aftivw4k25" path="res://scenes/player.tscn" id="3_glxkt"]
[ext_resource type="PackedScene" uid="uid://dwy0c50ndnu8k" path="res://scenes/bullet.tscn" id="4_2bgno"]
[ext_resource type="Script" path="res://scripts/scene_manager.gd" id="5_6wvkt"]
[sub_resource type="CircleShape2D" id="CircleShape2D_ox8o7"]
radius = 31.0161
[sub_resource type="CircleShape2D" id="CircleShape2D_a75m4"]
[sub_resource type="RectangleShape2D" id="RectangleShape2D_uopew"]
size = Vector2(70, 26)
[sub_resource type="WorldBoundaryShape2D" id="WorldBoundaryShape2D_igau3"]
[sub_resource type="RectangleShape2D" id="RectangleShape2D_oospb"]
size = Vector2(43.1503, 26)
[node name="Game" type="Node2D"]
script = ExtResource("1_dasmg")
[node name="StaticBody2D" type="StaticBody2D" parent="."]
position = Vector2(525, 412)
metadata/_edit_group_ = true
[node name="CollisionShape2D" type="CollisionShape2D" parent="StaticBody2D"]
position = Vector2(-26, 3)
shape = SubResource("RectangleShape2D_uopew")
[node name="StaticBody2D3" type="StaticBody2D" parent="."]
position = Vector2(525, 412)
rotation = 2.16668
metadata/_edit_group_ = true
[node name="CollisionShape2D" type="CollisionShape2D" parent="StaticBody2D3"]
position = Vector2(-26, 3)
shape = SubResource("RectangleShape2D_uopew")
[node name="crate1" type="RigidBody2D" parent="."]
position = Vector2(193, 264)
rotation = 0.807772
metadata/_edit_group_ = true
[node name="CollisionShape2D" type="CollisionShape2D" parent="crate1"]
position = Vector2(23.712, 8.64544)
rotation = -1.01686
scale = Vector2(2.89246, 1)
shape = SubResource("RectangleShape2D_oospb")
debug_color = Color(0.907205, 0.191988, 0.583153, 0.42)
[node name="crate2" type="RigidBody2D" parent="."]
position = Vector2(616, 580)
rotation = -0.385715
metadata/_edit_group_ = true
[node name="CollisionShape2D" type="CollisionShape2D" parent="crate2"]
position = Vector2(107.026, -85.1608)
shape = SubResource("RectangleShape2D_oospb")
debug_color = Color(0.907205, 0.191988, 0.583153, 0.42)
[node name="StaticBody2D2" type="StaticBody2D" parent="."]
position = Vector2(11, 464)
metadata/_edit_group_ = true
[node name="CollisionShape2D" type="CollisionShape2D" parent="StaticBody2D2"]
position = Vector2(518, 20)
shape = SubResource("WorldBoundaryShape2D_igau3")
[node name="Trigger" type="Area2D" parent="."]
position = Vector2(570, 452)
script = ExtResource("1_7cb3s")
@ -76,14 +29,66 @@ metadata/_edit_group_ = true
shape = SubResource("CircleShape2D_ox8o7")
debug_color = Color(0.448323, 0.465614, 0.961841, 0.42)
[node name="CharacterBody2D" type="CharacterBody2D" parent="."]
position = Vector2(468, 388)
script = ExtResource("2_7k2el")
[node name="Level" type="Node2D" parent="."]
[node name="StaticBody2D" type="StaticBody2D" parent="Level"]
position = Vector2(525, 412)
metadata/_edit_group_ = true
[node name="CollisionShape2D" type="CollisionShape2D" parent="CharacterBody2D"]
shape = SubResource("CircleShape2D_a75m4")
debug_color = Color(1.05888e-06, 0.616635, 0.591704, 0.42)
[node name="CollisionShape2D" type="CollisionShape2D" parent="Level/StaticBody2D"]
position = Vector2(-26, 3)
shape = SubResource("RectangleShape2D_uopew")
[node name="StaticBody2D3" type="StaticBody2D" parent="Level"]
position = Vector2(525, 412)
rotation = 2.16668
metadata/_edit_group_ = true
[node name="CollisionShape2D" type="CollisionShape2D" parent="Level/StaticBody2D3"]
position = Vector2(-26, 3)
shape = SubResource("RectangleShape2D_uopew")
[node name="StaticBody2D2" type="StaticBody2D" parent="Level"]
position = Vector2(11, 464)
metadata/_edit_group_ = true
[node name="CollisionShape2D" type="CollisionShape2D" parent="Level/StaticBody2D2"]
position = Vector2(518, 20)
shape = SubResource("WorldBoundaryShape2D_igau3")
[node name="crates" type="Node2D" parent="."]
[node name="crate2" type="RigidBody2D" parent="crates"]
position = Vector2(616, 580)
rotation = -0.385715
metadata/_edit_group_ = true
[node name="CollisionShape2D" type="CollisionShape2D" parent="crates/crate2"]
position = Vector2(107.026, -85.1608)
shape = SubResource("RectangleShape2D_oospb")
debug_color = Color(0.907205, 0.191988, 0.583153, 0.42)
[node name="crate1" type="RigidBody2D" parent="crates"]
position = Vector2(193, 264)
rotation = 0.807772
metadata/_edit_group_ = true
[node name="CollisionShape2D" type="CollisionShape2D" parent="crates/crate1"]
position = Vector2(23.712, 8.64544)
rotation = -1.01686
scale = Vector2(2.89246, 1)
shape = SubResource("RectangleShape2D_oospb")
debug_color = Color(0.907205, 0.191988, 0.583153, 0.42)
[node name="CharacterBody2D" parent="." instance=ExtResource("3_glxkt")]
position = Vector2(338, 226)
[node name="Area2D" parent="." instance=ExtResource("4_2bgno")]
position = Vector2(330, 271)
[node name="SceneManager" type="Node" parent="."]
unique_name_in_owner = true
script = ExtResource("5_6wvkt")
[connection signal="areaTrigger" from="Trigger" to="." method="_on_trigger_area_trigger"]
[connection signal="body_entered" from="Trigger" to="Trigger" method="_on_body_entered"]

View File

@ -0,0 +1,25 @@
[gd_scene load_steps=3 format=3 uid="uid://du8aftivw4k25"]
[ext_resource type="Script" path="res://scripts/player.gd" id="1_p8gev"]
[sub_resource type="CircleShape2D" id="CircleShape2D_a75m4"]
[node name="CharacterBody2D" type="CharacterBody2D"]
script = ExtResource("1_p8gev")
metadata/_edit_group_ = true
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
shape = SubResource("CircleShape2D_a75m4")
debug_color = Color(1.05888e-06, 0.616635, 0.591704, 0.42)
[node name="rightcast" type="RayCast2D" parent="."]
target_position = Vector2(16, 0)
[node name="leftcast" type="RayCast2D" parent="."]
target_position = Vector2(-16, 0)
[node name="rightTarget" type="Node2D" parent="."]
position = Vector2(11, -10)
[node name="leftTarget" type="Node2D" parent="."]
position = Vector2(-15, -10)

View File

@ -0,0 +1,17 @@
class_name Bullet extends Area2D
var speed = 750
signal hit(bullet, body)
func setSpeed(value):
speed = value
#animation
func _physics_process(delta: float) -> void:
position += transform.x * speed * delta
func _on_body_entered(body: Node2D) -> void:
print ("bullet collision")
hit.emit(self, body)

View File

@ -3,6 +3,17 @@ extends CharacterBody2D
const SPEED = 300.0
const JUMP_VELOCITY = -400.0
const PUSH_FORCE = 500
var faceLeft = false
var pushRightEnabled = false
var pushLeftEnabled = false
var pushTarget
@onready var rightcast: RayCast2D = $rightcast
@onready var leftcast: RayCast2D = $leftcast
@onready var right_target: Node2D = $rightTarget
@onready var left_target: Node2D = $leftTarget
func _physics_process(delta: float) -> void:
@ -16,13 +27,49 @@ func _physics_process(delta: float) -> void:
# Get the input direction and handle the movement/deceleration.
# As good practice, you should replace UI actions with custom gameplay actions.
var direction := Input.get_axis("ui_left", "ui_right")
var direction := Input.get_axis("left", "right")
if Input.is_action_just_pressed("shove") && pushRightEnabled && faceLeft == false:
pushTarget.apply_central_impulse(Vector2(1,0) * PUSH_FORCE)
pushRightEnabled = false
if Input.is_action_just_pressed("shove") && pushLeftEnabled && faceLeft == true:
pushTarget.apply_central_impulse(Vector2(-1,0) * PUSH_FORCE)
pushLeftEnabled = false
# Shoot attack
if Input.is_action_just_pressed("shoot"):
if faceLeft == false:
%SceneManager.makeBullet(right_target.global_transform, 700)
if faceLeft == true:
%SceneManager.makeBullet(left_target.global_transform, -700)
if direction:
velocity.x = direction * SPEED
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
if direction <0:
faceLeft = true
if direction >0:
faceLeft = false
move_and_slide()
if rightcast.is_colliding():
print("something on my right")
var collider = rightcast.get_collider()
if collider is Node:
if collider is RigidBody2D:
print ("shove this crave")
#record that we can shove right
pushRightEnabled = true
#record what objet to shove
pushTarget = collider
if leftcast.is_colliding():
var collider = leftcast.get_collider()
if collider is Node:
if collider is RigidBody2D:
pushLeftEnabled = true
pushTarget = collider
for i in get_slide_collision_count():
var c = get_slide_collision(i)
if c.get_collider() is RigidBody2D:

View File

@ -0,0 +1,30 @@
extends Node
var bulletsFiredTotal = 0
var bulletsMadeTotal = 0
var bulletArray = []
var bullet = preload("res://scenes/bullet.tscn")
#bullet factory - makes bullets
func bulletFactory():
var mybullet
if bulletArray.size() < 7:
print("new bullet on the factory")
mybullet = bullet.instantiate()
mybullet.connect("hit", bulletHit)
else:
print("recycle bullet")
mybullet = bulletArray.pop_back()
bulletArray.push_front(mybullet)
return mybullet
#order desk for bullets
func makeBullet(position,speed):
print("make a bullet")
var newBullet = bulletFactory()
owner.add_child(newBullet)
newBullet.setSpeed(speed)
newBullet.transform = position
func bulletHit(bullet, body):
print("Tell the game controller a bullet hit something")