shove attack raycast, input maps, bullets object pooling, dynamic instantiation, grenades
This commit is contained in:
parent
20f76abae2
commit
426b0aa785
@ -20,3 +20,37 @@ config/icon="res://icon.svg"
|
||||
folder_colors={
|
||||
"res://scenes/": "orange"
|
||||
}
|
||||
|
||||
[input]
|
||||
|
||||
moveLeft={
|
||||
"deadzone": 0.2,
|
||||
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"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":0,"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":0,"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":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":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)
|
||||
]
|
||||
}
|
||||
|
14
scenes/bullet.tscn
Normal file
14
scenes/bullet.tscn
Normal file
@ -0,0 +1,14 @@
|
||||
[gd_scene load_steps=3 format=3 uid="uid://daa6idpu4gkat"]
|
||||
|
||||
[ext_resource type="Script" uid="uid://cbp3pm7i587mv" path="res://scripts/bullet.gd" id="1_mkf8s"]
|
||||
|
||||
[sub_resource type="RectangleShape2D" id="RectangleShape2D_h1aey"]
|
||||
size = Vector2(4, 2)
|
||||
|
||||
[node name="Bullet" type="Area2D"]
|
||||
scale = Vector2(3.2338707, 2.7645516)
|
||||
script = ExtResource("1_mkf8s")
|
||||
|
||||
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
|
||||
shape = SubResource("RectangleShape2D_h1aey")
|
||||
debug_color = Color(0.78294253, 0.1467745, 0.21462014, 0.41960785)
|
@ -1,7 +1,9 @@
|
||||
[gd_scene load_steps=8 format=3 uid="uid://cy1i6ucex6m0d"]
|
||||
[gd_scene load_steps=10 format=3 uid="uid://cy1i6ucex6m0d"]
|
||||
|
||||
[ext_resource type="Script" uid="uid://c546prectbgc7" 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://daa6idpu4gkat" path="res://scenes/bullet.tscn" id="2_iywne"]
|
||||
[ext_resource type="Script" uid="uid://barmq4qhp5cvl" 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://b1yy0sybg66hh" path="res://scenes/player.tscn" id="3_lnu2h"]
|
||||
|
||||
@ -15,6 +17,10 @@
|
||||
position = Vector2(6, 2)
|
||||
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="."]
|
||||
position = Vector2(120, 233)
|
||||
scale = Vector2(3.2000012, 1)
|
||||
@ -41,10 +47,10 @@ metadata/_edit_group_ = true
|
||||
[node name="CollisionShape2D" type="CollisionShape2D" parent="StaticBody2D3"]
|
||||
shape = SubResource("RectangleShape2D_8cj0n")
|
||||
|
||||
[node name="Crate" parent="." instance=ExtResource("1_uwrxv")]
|
||||
[node name="Crate" parent="." groups=["explodable", "pushable"] instance=ExtResource("1_uwrxv")]
|
||||
position = Vector2(112, 186)
|
||||
|
||||
[node name="Crate2" type="RigidBody2D" parent="."]
|
||||
[node name="Crate2" type="RigidBody2D" parent="." groups=["explodable", "pushable"]]
|
||||
position = Vector2(147.99998, 202.99994)
|
||||
rotation = 2.6595373
|
||||
metadata/_edit_group_ = true
|
||||
@ -75,5 +81,8 @@ offset_right = 40.0
|
||||
offset_bottom = 23.0
|
||||
text = "0000"
|
||||
|
||||
[node name="Bullet" parent="." instance=ExtResource("2_iywne")]
|
||||
position = Vector2(45, 205.99998)
|
||||
|
||||
[connection signal="triggerFiredSignal" from="Trigger" to="." method="_on_trigger_fired"]
|
||||
[connection signal="triggerFiredSignal" from="Trigger2" to="." method="_on_trigger_fired"]
|
||||
|
16
scenes/grenade.tscn
Normal file
16
scenes/grenade.tscn
Normal file
@ -0,0 +1,16 @@
|
||||
[gd_scene load_steps=3 format=3 uid="uid://b5n0lwes8qpu5"]
|
||||
|
||||
[ext_resource type="Script" uid="uid://c3sd8uvxer0wk" path="res://scripts/grenade.gd" id="1_cvyik"]
|
||||
|
||||
[sub_resource type="CircleShape2D" id="CircleShape2D_1xt3t"]
|
||||
|
||||
[node name="Grenade" type="RigidBody2D"]
|
||||
contact_monitor = true
|
||||
max_contacts_reported = 1
|
||||
script = ExtResource("1_cvyik")
|
||||
|
||||
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
|
||||
shape = SubResource("CircleShape2D_1xt3t")
|
||||
debug_color = Color(0.7058824, 0.49411765, 0.18431373, 0.5921569)
|
||||
|
||||
[connection signal="body_entered" from="." to="." method="_on_body_entered"]
|
@ -10,3 +10,15 @@ script = ExtResource("1_3vyb7")
|
||||
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
|
||||
shape = SubResource("CircleShape2D_3vyb7")
|
||||
debug_color = Color(0.46210045, 0.5883958, 0.21709043, 0.41960785)
|
||||
|
||||
[node name="RightCast" type="RayCast2D" parent="."]
|
||||
target_position = Vector2(15, 0)
|
||||
|
||||
[node name="LeftCast" type="RayCast2D" parent="."]
|
||||
target_position = Vector2(-14, 0)
|
||||
|
||||
[node name="RightSpawn" type="Marker2D" parent="."]
|
||||
position = Vector2(15, -10)
|
||||
|
||||
[node name="LeftSpawn" type="Marker2D" parent="."]
|
||||
position = Vector2(-14, -10)
|
||||
|
15
scripts/bullet.gd
Normal file
15
scripts/bullet.gd
Normal 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() -> void:
|
||||
pass # Replace with function body.
|
||||
|
||||
|
||||
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
||||
func _process(delta: float) -> void:
|
||||
position += transform.x * speed * delta
|
||||
|
||||
func setSpeed(value)->void:
|
||||
speed = value
|
1
scripts/bullet.gd.uid
Normal file
1
scripts/bullet.gd.uid
Normal file
@ -0,0 +1 @@
|
||||
uid://cbp3pm7i587mv
|
26
scripts/grenade.gd
Normal file
26
scripts/grenade.gd
Normal file
@ -0,0 +1,26 @@
|
||||
class_name Grenade extends RigidBody2D
|
||||
|
||||
var timer = Timer.new()
|
||||
|
||||
# Called when the node enters the scene tree for the first time.
|
||||
func _ready() -> void:
|
||||
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: float) -> void:
|
||||
pass
|
||||
|
||||
func explode()->void:
|
||||
print("ima splode")
|
||||
self.queue_free()
|
||||
|
||||
|
||||
func _on_body_entered(body: Node) -> void:
|
||||
print("Grenade hit something")
|
||||
if body.is_in_group("explodable"):
|
||||
body.queue_free()
|
||||
explode()
|
1
scripts/grenade.gd.uid
Normal file
1
scripts/grenade.gd.uid
Normal file
@ -0,0 +1 @@
|
||||
uid://c3sd8uvxer0wk
|
@ -3,33 +3,93 @@ extends CharacterBody2D
|
||||
|
||||
const SPEED = 300.0
|
||||
const JUMP_VELOCITY = -400.0
|
||||
var direction:float = 0
|
||||
|
||||
@export var BUMP_POWER = 100
|
||||
|
||||
enum FaceDirection{LEFT, RIGHT}
|
||||
var facing:FaceDirection = FaceDirection.RIGHT
|
||||
|
||||
@onready var right_cast: RayCast2D = $RightCast
|
||||
@onready var left_cast: RayCast2D = $LeftCast
|
||||
|
||||
var pushTarget:RigidBody2D
|
||||
var pushEnabled:bool = false
|
||||
|
||||
@onready var right_spawn: Marker2D = $RightSpawn
|
||||
@onready var left_spawn: Marker2D = $LeftSpawn
|
||||
|
||||
func _physics_process(delta: float) -> void:
|
||||
# Add the gravity.
|
||||
if not is_on_floor():
|
||||
velocity += get_gravity() * delta
|
||||
|
||||
handle_input()
|
||||
handle_movement(delta)
|
||||
move_and_slide()
|
||||
handle_collision()
|
||||
|
||||
func handle_input()->void:
|
||||
# Handle jump.
|
||||
if Input.is_action_just_pressed("ui_accept") and is_on_floor():
|
||||
if Input.is_action_just_pressed("jump") and is_on_floor():
|
||||
velocity.y = JUMP_VELOCITY
|
||||
|
||||
# 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")
|
||||
|
||||
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:
|
||||
velocity.x = direction * SPEED
|
||||
else:
|
||||
velocity.x = move_toward(velocity.x, 0, SPEED)
|
||||
|
||||
move_and_slide()
|
||||
|
||||
if not is_on_floor():
|
||||
velocity += get_gravity() * delta
|
||||
|
||||
func handle_collision()->void:
|
||||
# handle world reactions
|
||||
for i in get_slide_collision_count():
|
||||
var c = get_slide_collision(i)
|
||||
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()
|
||||
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
|
||||
|
||||
|
45
scripts/scene_manager.gd
Normal file
45
scripts/scene_manager.gd
Normal file
@ -0,0 +1,45 @@
|
||||
class_name SceneManager extends Node2D
|
||||
|
||||
var bullet = preload("res://scenes/bullet.tscn")
|
||||
|
||||
@onready var game: Node2D = $".."
|
||||
|
||||
var bulletArray = []
|
||||
var totalAllowedBullets = 7
|
||||
|
||||
var grenade = preload("res://scenes/grenade.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()->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
|
||||
|
1
scripts/scene_manager.gd.uid
Normal file
1
scripts/scene_manager.gd.uid
Normal file
@ -0,0 +1 @@
|
||||
uid://barmq4qhp5cvl
|
Loading…
Reference in New Issue
Block a user