shove attack raycast, input maps, bullets object pooling, dynamic instantiation, grenades
This commit is contained in:
1 parent
20f76abae2
commit
426b0aa785
12 files changed
+250
-16
No files matched your search
@@ -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
|
||||
@@ -0,0 +1 @@
|
||||
uid://cbp3pm7i587mv
|
||||
@@ -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()
|
||||
@@ -0,0 +1 @@
|
||||
uid://c3sd8uvxer0wk
|
||||
+73
-13
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
uid://barmq4qhp5cvl
|
||||
Reference in new issue
Block a user