shove attack raycasts, input maps, bullets object pooling, dynamic instantiation, grenades

This commit is contained in:
OddlyTimbot 2025-10-06 20:56:47 -04:00
parent 39a2c3e097
commit b8e6b3f2ce
12 changed files with 251 additions and 17 deletions

View File

@ -20,3 +20,37 @@ config/icon="res://icon.svg"
folder_colors={ folder_colors={
"res://scenes/": "orange" "res://scenes/": "orange"
} }
[input]
moveLeft={
"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":4194319,"key_label":0,"unicode":0,"location":0,"echo":false,"script":null)
]
}
moveRight={
"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":4194321,"key_label":0,"unicode":0,"location":0,"echo":false,"script":null)
]
}
jump={
"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":32,"key_label":0,"unicode":32,"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":85,"key_label":0,"unicode":117,"location":0,"echo":false,"script":null)
]
}
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":90,"key_label":0,"unicode":122,"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":88,"key_label":0,"unicode":120,"location":0,"echo":false,"script":null)
]
}
chuck={
"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":67,"key_label":0,"unicode":99,"location":0,"echo":false,"script":null)
]
}

13
scenes/bullet.tscn Normal file
View File

@ -0,0 +1,13 @@
[gd_scene load_steps=3 format=3 uid="uid://daa6idpu4gkat"]
[ext_resource type="Script" uid="uid://ock81bqismk0" path="res://scripts/bullet.gd" id="1_mkf8s"]
[sub_resource type="RectangleShape2D" id="RectangleShape2D_h1aey"]
size = Vector2(8, 6)
[node name="Bullet" type="Area2D"]
script = ExtResource("1_mkf8s")
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
shape = SubResource("RectangleShape2D_h1aey")
debug_color = Color(0.95943683, 0.18113726, 0.24279165, 0.41960785)

View File

@ -1,9 +1,12 @@
[gd_scene load_steps=8 format=3 uid="uid://cy1i6ucex6m0d"] [gd_scene load_steps=11 format=3 uid="uid://cy1i6ucex6m0d"]
[ext_resource type="Script" uid="uid://crhflh7kooolf" path="res://scripts/gamecontroller.gd" id="1_lbhrr"] [ext_resource type="Script" uid="uid://crhflh7kooolf" path="res://scripts/gamecontroller.gd" id="1_lbhrr"]
[ext_resource type="PackedScene" uid="uid://qv8blu8wkqvq" path="res://scenes/crate.tscn" id="1_uwrxv"] [ext_resource type="PackedScene" uid="uid://qv8blu8wkqvq" path="res://scenes/crate.tscn" id="1_uwrxv"]
[ext_resource type="Script" uid="uid://co43e72devw6g" path="res://scripts/scene_manager.gd" id="2_p57ef"]
[ext_resource type="PackedScene" uid="uid://dmrrbiwqptrho" path="res://scenes/trigger.tscn" id="2_yqjtg"] [ext_resource type="PackedScene" uid="uid://dmrrbiwqptrho" path="res://scenes/trigger.tscn" id="2_yqjtg"]
[ext_resource type="PackedScene" uid="uid://b1yy0sybg66hh" path="res://scenes/player.tscn" id="3_lnu2h"] [ext_resource type="PackedScene" uid="uid://b1yy0sybg66hh" path="res://scenes/player.tscn" id="3_lnu2h"]
[ext_resource type="PackedScene" uid="uid://daa6idpu4gkat" path="res://scenes/bullet.tscn" id="5_iywne"]
[ext_resource type="PackedScene" uid="uid://b5n0lwes8qpu5" path="res://scenes/grenade.tscn" id="7_u5sy4"]
[sub_resource type="RectangleShape2D" id="RectangleShape2D_8cj0n"] [sub_resource type="RectangleShape2D" id="RectangleShape2D_8cj0n"]
size = Vector2(68, 35) size = Vector2(68, 35)
@ -15,6 +18,10 @@ size = Vector2(68, 35)
[node name="Game" type="Node2D"] [node name="Game" type="Node2D"]
script = ExtResource("1_lbhrr") script = ExtResource("1_lbhrr")
[node name="SceneManager" type="Node2D" parent="."]
unique_name_in_owner = true
script = ExtResource("2_p57ef")
[node name="StaticBody2D" type="StaticBody2D" parent="."] [node name="StaticBody2D" type="StaticBody2D" parent="."]
position = Vector2(394, 353) position = Vector2(394, 353)
metadata/_edit_group_ = true metadata/_edit_group_ = true
@ -40,10 +47,10 @@ metadata/_edit_group_ = true
position = Vector2(0, 7.5) position = Vector2(0, 7.5)
shape = SubResource("RectangleShape2D_8cj0n") shape = SubResource("RectangleShape2D_8cj0n")
[node name="Crate" parent="." instance=ExtResource("1_uwrxv")] [node name="Crate" parent="." groups=["pushable", "splodable"] instance=ExtResource("1_uwrxv")]
position = Vector2(406.00003, 327) position = Vector2(406.00003, 327)
[node name="RigidBody2D2" type="RigidBody2D" parent="."] [node name="RigidBody2D2" type="RigidBody2D" parent="." groups=["pushable", "splodable"]]
position = Vector2(243, 371) position = Vector2(243, 371)
rotation = -1.099222 rotation = -1.099222
metadata/_edit_group_ = true metadata/_edit_group_ = true
@ -52,7 +59,7 @@ metadata/_edit_group_ = true
shape = SubResource("RectangleShape2D_uwrxv") shape = SubResource("RectangleShape2D_uwrxv")
debug_color = Color(0.84557056, 0.3727497, 0.1753774, 0.41960785) debug_color = Color(0.84557056, 0.3727497, 0.1753774, 0.41960785)
[node name="RigidBody2D3" type="RigidBody2D" parent="."] [node name="RigidBody2D3" type="RigidBody2D" parent="." groups=["pushable", "splodable"]]
position = Vector2(502, 230) position = Vector2(502, 230)
rotation = -1.099222 rotation = -1.099222
metadata/_edit_group_ = true metadata/_edit_group_ = true
@ -79,5 +86,11 @@ intent = "powerup"
position = Vector2(379, 331) position = Vector2(379, 331)
BUMP_POWER = 50 BUMP_POWER = 50
[node name="Bullet" parent="." instance=ExtResource("5_iywne")]
position = Vector2(293, 329)
[node name="Grenade" parent="." instance=ExtResource("7_u5sy4")]
position = Vector2(380, 307)
[connection signal="triggerFiredSignal" from="Trigger" to="." method="_on_trigger_fired"] [connection signal="triggerFiredSignal" from="Trigger" to="." method="_on_trigger_fired"]
[connection signal="triggerFiredSignal" from="Trigger2" to="." method="_on_trigger_fired"] [connection signal="triggerFiredSignal" from="Trigger2" to="." method="_on_trigger_fired"]

17
scenes/grenade.tscn Normal file
View File

@ -0,0 +1,17 @@
[gd_scene load_steps=3 format=3 uid="uid://b5n0lwes8qpu5"]
[ext_resource type="Script" uid="uid://cev3cg5mdn3it" path="res://scripts/grenade.gd" id="1_cvyik"]
[sub_resource type="CircleShape2D" id="CircleShape2D_1xt3t"]
radius = 3.0
[node name="Grenade" type="RigidBody2D"]
contact_monitor = true
max_contacts_reported = 2
script = ExtResource("1_cvyik")
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
shape = SubResource("CircleShape2D_1xt3t")
debug_color = Color(0.6279718, 0.5294392, 0.11152003, 0.41960785)
[connection signal="body_entered" from="." to="." method="_on_body_entered"]

View File

@ -10,3 +10,15 @@ script = ExtResource("1_3vyb7")
[node name="CollisionShape2D" type="CollisionShape2D" parent="."] [node name="CollisionShape2D" type="CollisionShape2D" parent="."]
shape = SubResource("CircleShape2D_3vyb7") shape = SubResource("CircleShape2D_3vyb7")
debug_color = Color(0.46677285, 0.59072345, 0.09206382, 0.41960785) debug_color = Color(0.46677285, 0.59072345, 0.09206382, 0.41960785)
[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="Marker2D" parent="."]
position = Vector2(14, -8)
[node name="LeftSpawn" type="Marker2D" parent="."]
position = Vector2(-13, -8)

15
scripts/bullet.gd Normal file
View File

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

1
scripts/bullet.gd.uid Normal file
View File

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

27
scripts/grenade.gd Normal file
View File

@ -0,0 +1,27 @@
class_name Grenade extends RigidBody2D
var timer = Timer.new()
# Called when the node enters the scene tree for the first time.
func _ready():
add_child(timer)
timer.wait_time = 2
timer.one_shot = true
timer.connect("timeout", explode)
timer.start()
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
pass
func explode()->void:
print("ima splode")
self.queue_free()
func _on_body_entered(body):
print("grenade hit something")
if body.is_in_group("splodable"):
body.queue_free()
explode()

1
scripts/grenade.gd.uid Normal file
View File

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

View File

@ -3,31 +3,90 @@ extends CharacterBody2D
const SPEED = 300.0 const SPEED = 300.0
const JUMP_VELOCITY = -400.0 const JUMP_VELOCITY = -400.0
var direction:float = 0
@export var BUMP_POWER = 100 @export var BUMP_POWER = 100
enum FaceDirection{LEFT, RIGHT}
var facing:FaceDirection = FaceDirection.RIGHT
@onready var right_cast = $RightCast
@onready var left_cast = $LeftCast
var pushTarget:RigidBody2D
var pushEnabled:bool = false
@onready var right_spawn = $RightSpawn
@onready var left_spawn = $LeftSpawn
func _physics_process(delta): func _physics_process(delta):
# Add the gravity.
if not is_on_floor():
velocity += get_gravity() * delta
# Handle jump.
if Input.is_action_just_pressed("ui_accept") and is_on_floor():
velocity.y = JUMP_VELOCITY
# Get the input direction and handle the movement/deceleration. # Get the input direction and handle the movement/deceleration.
# As good practice, you should replace UI actions with custom gameplay actions. # As good practice, you should replace UI actions with custom gameplay actions.
var direction = Input.get_axis("ui_left", "ui_right") handle_input()
handle_movement(delta)
move_and_slide()
handle_collisions()
func handle_input()->void:
# Handle jump.
if Input.is_action_just_pressed("jump") and is_on_floor():
velocity.y = JUMP_VELOCITY
direction = Input.get_axis("moveLeft", "moveRight")
if direction <0:
facing=FaceDirection.LEFT
if direction >0:
facing=FaceDirection.RIGHT
if Input.is_action_just_pressed("shove") && pushEnabled:
print("I want to shove")
var shoveDirection:int
match facing:
FaceDirection.RIGHT:
shoveDirection = 1
FaceDirection.LEFT:
shoveDirection = -1
pushTarget.apply_central_impulse(Vector2(shoveDirection,0)*700)
if Input.is_action_just_pressed("shoot"):
print("Ima shoot")
match facing:
FaceDirection.RIGHT:
print("shoot right")
%SceneManager.makeBullet(right_spawn.global_transform, 700)
FaceDirection.LEFT:
print("shoot left")
%SceneManager.makeBullet(left_spawn.global_transform, -700)
if Input.is_action_just_pressed("chuck"):
print("ima chuck a grenade")
match facing:
FaceDirection.RIGHT:
%SceneManager.makeGrenade(right_spawn.global_transform, 1)
FaceDirection.LEFT:
%SceneManager.makeGrenade(left_spawn.global_transform, -1)
func handle_movement(_delta)->void:
if direction: if direction:
velocity.x = direction * SPEED velocity.x = direction * SPEED
else: else:
velocity.x = move_toward(velocity.x, 0, SPEED) velocity.x = move_toward(velocity.x, 0, SPEED)
move_and_slide() if not is_on_floor():
velocity += get_gravity() * _delta
func handle_collisions()->void:
#handle world reactions #handle world reactions
for i in get_slide_collision_count(): for i in get_slide_collision_count():
var c = get_slide_collision(i) var c = get_slide_collision(i)
if c.get_collider() is RigidBody2D: if c.get_collider() is RigidBody2D:
c.get_collider().apply_central_impulse(-c.get_normal() * BUMP_POWER) c.get_collider().apply_central_impulse(-c.get_normal() * BUMP_POWER)
if right_cast.is_colliding() && facing==FaceDirection.RIGHT:
var collider = right_cast.get_collider()
#check if this is okay
if collider is Node && collider is RigidBody2D && collider.is_in_group("pushable"):
pushTarget = collider
pushEnabled = true
if left_cast.is_colliding() && facing==FaceDirection.LEFT:
var collider = left_cast.get_collider()
if collider is Node && collider is RigidBody2D && collider.is_in_group("pushable"):
pushTarget = collider
pushEnabled = true
if not right_cast.is_colliding() && not left_cast.is_colliding():
pushEnabled = false

41
scripts/scene_manager.gd Normal file
View File

@ -0,0 +1,41 @@
class_name SceneManager extends Node2D
var bullet = preload("res://scenes/bullet.tscn")
var grenade = preload("res://scenes/grenade.tscn")
@onready var game = $".."
var bulletArray = []
var totalAllowedBullets = 7
# Called when the node enters the scene tree for the first time.
func _ready():
pass # Replace with function body.
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
pass
func bulletFactory()->Bullet:
var myBullet:Bullet
if bulletArray.size() < totalAllowedBullets:
#make a new bullet
myBullet = bullet.instantiate()
game.add_child(myBullet)
else:
myBullet = bulletArray.pop_back()
bulletArray.push_front(myBullet)
return myBullet
func makeBullet(_bulletPosition, _bulletSpeed)->void:
print("make a bullet, put it in the world")
var myBullet = bulletFactory()
myBullet.transform = _bulletPosition
myBullet.setSpeed(_bulletSpeed)
func makeGrenade(_grenadePosition, _grenadeDirection)->void:
print("SM make a grenade")
var myGrenade:Grenade = grenade.instantiate()
game.add_child(myGrenade)
myGrenade.apply_central_impulse(Vector2(_grenadeDirection, -1)*200)
myGrenade.transform = _grenadePosition

View File

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