bullets, bullet factory, shove attack, scenemanager

This commit is contained in:
AnicaCihla 2025-06-09 21:21:45 -04:00
parent a98540de07
commit 8a78da855c
9 changed files with 166 additions and 16 deletions

25
Scenes/Player.tscn Normal file
View File

@ -0,0 +1,25 @@
[gd_scene load_steps=3 format=3 uid="uid://bu8e4iyw8pc03"]
[ext_resource type="Script" uid="uid://dsryf6gxqcm1k" path="res://Scripts/character_body_2d.gd" id="1_0y7nr"]
[sub_resource type="CircleShape2D" id="CircleShape2D_2poj3"]
[node name="CharacterBody2D" type="CharacterBody2D"]
script = ExtResource("1_0y7nr")
metadata/_edit_group_ = true
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
shape = SubResource("CircleShape2D_2poj3")
debug_color = Color(0.613557, 0.363647, 0.991688, 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="RightSpawn" type="Node2D" parent="."]
position = Vector2(17, -8)
[node name="LeftSpawn" type="Node2D" parent="."]
position = Vector2(-14, -9)

14
Scenes/bullet.tscn Normal file
View File

@ -0,0 +1,14 @@
[gd_scene load_steps=3 format=3 uid="uid://cm8o5vpn4a00"]
[ext_resource type="Script" uid="uid://q1ok5ukbtibm" path="res://bullet.gd" id="1_xjght"]
[sub_resource type="RectangleShape2D" id="RectangleShape2D_oduel"]
size = Vector2(14, 4)
[node name="Bullet" type="Area2D"]
script = ExtResource("1_xjght")
metadata/_edit_group_ = true
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
shape = SubResource("RectangleShape2D_oduel")
debug_color = Color(0.995859, 0, 0.197667, 0.42)

View File

@ -1,35 +1,31 @@
[gd_scene load_steps=7 format=3 uid="uid://c6mxse0hqjro7"]
[gd_scene load_steps=8 format=3 uid="uid://c6mxse0hqjro7"]
[ext_resource type="Script" uid="uid://dsryf6gxqcm1k" path="res://Scripts/character_body_2d.gd" id="1_ebmjs"]
[ext_resource type="Script" uid="uid://tno7aku0yyke" path="res://Scripts/gamecontroller.gd" id="1_wrm1d"]
[ext_resource type="Script" uid="uid://bs4frobn6kxne" path="res://Scripts/scene_manager.gd" id="2_i6g32"]
[ext_resource type="PackedScene" uid="uid://cd7fv3pwgr880" path="res://Scenes/crate.tscn" id="3_3dryh"]
[ext_resource type="PackedScene" uid="uid://bu8e4iyw8pc03" path="res://Scenes/Player.tscn" id="4_3dryh"]
[ext_resource type="PackedScene" uid="uid://b5d8himq5sttt" path="res://Scenes/trigger.tscn" id="4_wowpa"]
[ext_resource type="PackedScene" uid="uid://cm8o5vpn4a00" path="res://Scenes/bullet.tscn" id="5_wowpa"]
[sub_resource type="RectangleShape2D" id="RectangleShape2D_2poj3"]
size = Vector2(50, 20)
[sub_resource type="CircleShape2D" id="CircleShape2D_2poj3"]
[node name="Game" type="Node2D"]
script = ExtResource("1_wrm1d")
[node name="SceneManager" type="Node2D" parent="."]
unique_name_in_owner = true
script = ExtResource("2_i6g32")
[node name="StaticBody2D" type="StaticBody2D" parent="."]
position = Vector2(595, 337)
scale = Vector2(1.50487, 1.09333)
scale = Vector2(4.146, 0.985995)
metadata/_edit_group_ = true
[node name="CollisionShape2D" type="CollisionShape2D" parent="StaticBody2D"]
scale = Vector2(1, 1)
shape = SubResource("RectangleShape2D_2poj3")
[node name="CharacterBody2D" type="CharacterBody2D" parent="."]
position = Vector2(576, 303)
script = ExtResource("1_ebmjs")
metadata/_edit_group_ = true
[node name="CollisionShape2D" type="CollisionShape2D" parent="CharacterBody2D"]
shape = SubResource("CircleShape2D_2poj3")
debug_color = Color(0.613557, 0.363647, 0.991688, 0.42)
[node name="RigidBody2D3" parent="." instance=ExtResource("3_3dryh")]
position = Vector2(596, 262)
@ -40,11 +36,17 @@ position = Vector2(616, 222)
position = Vector2(574, 222)
[node name="Area2D2" parent="." instance=ExtResource("4_wowpa")]
position = Vector2(511, 397)
position = Vector2(507, 414)
effect = "destroy"
[node name="Area2D3" parent="." instance=ExtResource("4_wowpa")]
position = Vector2(684, 400)
position = Vector2(679, 430)
[node name="CharacterBody2D" parent="." instance=ExtResource("4_3dryh")]
position = Vector2(614, 314)
[node name="Bullet" parent="." instance=ExtResource("5_wowpa")]
position = Vector2(520, 265)
[connection signal="areatrigger" from="Area2D2" to="." method="_on_areatrigger"]
[connection signal="areatrigger" from="Area2D3" to="." method="_on_areatrigger"]

View File

@ -4,6 +4,17 @@ extends CharacterBody2D
const SPEED = 300.0
const JUMP_VELOCITY = -400.0
@onready var right_cast: RayCast2D = $RightCast
@onready var left_cast: RayCast2D = $LeftCast
@onready var right_spawn: Node2D = $RightSpawn
@onready var left_spawn: Node2D = $LeftSpawn
enum FaceDirection{LEFT, RIGHT}
var facing:FaceDirection = FaceDirection.RIGHT
var pushTarget
var pushEnabled = false
func _physics_process(delta: float) -> void:
# Add the gravity.
@ -14,11 +25,31 @@ func _physics_process(delta: float) -> void:
if Input.is_action_just_pressed("ui_accept") and is_on_floor():
velocity.y = JUMP_VELOCITY
if Input.is_action_just_pressed("shove") && pushEnabled:
print("shove pressed")
if facing == FaceDirection.RIGHT:
pushTarget.apply_central_impulse(Vector2(1,0)*700)
pushEnabled = false
if facing == FaceDirection.LEFT:
pushTarget.apply_central_impulse(Vector2(-1,0)*700)
pushEnabled = false
if Input.is_action_just_pressed("shoot"):
print("shoot a bullet")
if facing == FaceDirection.RIGHT:
%SceneManager.makeBullet(right_spawn.global_transform, 700)
if facing == FaceDirection.LEFT:
%SceneManager.makeBullet(left_spawn.global_transform, -700)
# 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")
if direction:
velocity.x = direction * SPEED
if direction <0:
facing = FaceDirection.LEFT
if direction >0:
facing = FaceDirection.RIGHT
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
@ -27,3 +58,23 @@ func _physics_process(delta: float) -> void:
var c = get_slide_collision(i)
if c.get_collider() is RigidBody2D:
c.get_collider().apply_central_impulse(-c.get_normal() * 100)
if right_cast.is_colliding() && facing==FaceDirection.RIGHT:
#get the thing I am colliding with
var collider = right_cast.get_collider()
if collider is Node && collider is RigidBody2D:
print("I can shove this to the right")
pushTarget = collider
pushEnabled = true
if not right_cast.is_colliding() && not left_cast.is_colliding():
pushEnabled = false
if left_cast.is_colliding() && facing==FaceDirection.LEFT:
var collider = left_cast.get_collider()
if collider is Node && collider is RigidBody2D:
print("I can shove this to the left")
pushTarget = collider
pushEnabled = true
if not left_cast.is_colliding() && not right_cast.is_colliding():
pushEnabled = false

30
Scripts/scene_manager.gd Normal file
View File

@ -0,0 +1,30 @@
extends Node2D
var bullet = preload("res://Scenes/bullet.tscn")
var bulletArray =[]
var totalAllowedBullets = 7
#makes bullets
func bulletFactory():
var mybullet
if bulletArray.size() < totalAllowedBullets:
#make a new bullet
mybullet = bullet.instantiate()
owner.add_child(mybullet)
else:
#recycle bullet
mybullet = bulletArray.pop_back()
bulletArray.push_front(mybullet)
return mybullet
#order desk
func makeBullet(position, speed):
print("Scenemanger orders a bullet")
#ask the factory for a bullet
var mybullet = bulletFactory()
#set the speed for the bullet
mybullet.setSpeed(speed)
#position the bullet
mybullet.transform = position

View File

@ -0,0 +1 @@
uid://bs4frobn6kxne

13
bullet.gd Normal file
View File

@ -0,0 +1,13 @@
class_name Bullet extends Area2D
var speed = 700
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
pass # Replace with function body.func setSpeed(value):
func setSpeed(value):
speed = value
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _physics_process(delta: float) -> void:
position += transform.x * speed * delta

1
bullet.gd.uid Normal file
View File

@ -0,0 +1 @@
uid://q1ok5ukbtibm

View File

@ -21,3 +21,16 @@ folder_colors={
"res://Scenes/": "pink",
"res://Scripts/": "orange"
}
[input]
shove={
"deadzone": 0.2,
"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.2,
"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":89,"key_label":0,"unicode":121,"location":0,"echo":false,"script":null)
]
}