added melee, shooting, throwing attacks

This commit is contained in:
torontocaper committed 2025-10-06 20:57:11 -04:00
1 parent 0f76a2ccf1
commit f39c9fba95
14 files changed
+262 -36

No files matched your search

+15
View File
@@ -0,0 +1,15 @@
class_name Bullet
extends Area2D
@export 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 set_speed(value) -> void:
speed = value
+1
View File
@@ -0,0 +1 @@
uid://cb874g2gkrukb
-10
View File
@@ -1,16 +1,6 @@
extends Node2D
# 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 _on_trigger_fired(intent: Variant, body: PhysicsBody2D) -> void:
print("Game controller knows %s trigger fired " % intent)
match intent:
+21
View File
@@ -0,0 +1,21 @@
class_name Grenade
extends RigidBody2D
var timer = Timer.new()
func _ready() -> void:
add_child(timer)
timer.wait_time = 2.0
timer.one_shot = true
timer.connect("timeout", explode)
timer.start()
func explode() -> void:
print("kaboom!")
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
View File
@@ -0,0 +1 @@
uid://cevjj6wpr5f8f
+82 -12
View File
@@ -1,31 +1,101 @@
extends CharacterBody2D
const SPEED = 300.0
const JUMP_VELOCITY = -600.0
@export var BUMP_POWER = 100
enum FaceDirection{LEFT, RIGHT}
@export var BUMP_POWER = 50
@export var SHOVE_POWER = 200
var facing : FaceDirection = FaceDirection.RIGHT
var direction : float = 0.0
var push_target : RigidBody2D
var push_enabled : bool = false
@onready var right_cast: RayCast2D = $RightCast
@onready var left_cast: RayCast2D = $LeftCast
@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_collisions()
# Handle jump.
if Input.is_action_just_pressed("ui_accept") and is_on_floor():
func handle_input() -> void:
# Handle jumping.
if Input.is_action_just_pressed("jump") and is_on_floor():
velocity.y = JUMP_VELOCITY
direction = Input.get_axis("move_left", "move_right")
if direction < 0:
facing = FaceDirection.LEFT
if direction > 0:
facing = FaceDirection.RIGHT
# Handle shoving.
if Input.is_action_just_pressed("shove") and push_enabled == true:
var shove_direction : int
match facing:
FaceDirection.RIGHT:
shove_direction = 1
FaceDirection.LEFT:
shove_direction = -1
push_target.apply_central_impulse(Vector2(shove_direction, 0) * SHOVE_POWER)
# Handle shooting.eee
if Input.is_action_just_pressed("shoot"):
print("pew-pew")
match facing:
FaceDirection.RIGHT:
%SceneManager.make_bullet(right_spawn.global_transform, 700)
print("shoot right")
FaceDirection.LEFT:
%SceneManager.make_bullet(left_spawn.global_transform, -700)
print("shoot left")
if Input.is_action_just_pressed("throw"):
print("grenade!")
match facing:
FaceDirection.RIGHT:
%SceneManager.make_grenade(right_spawn.global_transform, 1.0)
print("throwing right")
FaceDirection.LEFT:
%SceneManager.make_grenade(left_spawn.global_transform, -1.0)
print("throwing left")
# 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")
func handle_movement(delta) -> void:
# Left-right movement.
if direction:
velocity.x = direction * SPEED
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
# Add gravity.
if not is_on_floor():
velocity += get_gravity() * delta
move_and_slide()
# Handle world reactions
func handle_collisions() -> void:
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)
if right_cast.is_colliding() and facing == FaceDirection.RIGHT:
var collider = right_cast.get_collider()
# check if this is OK
if collider is Node and collider is RigidBody2D and collider.is_in_group("pushable"):
push_target = collider
push_enabled = true
if left_cast.is_colliding() and facing == FaceDirection.LEFT:
var collider = left_cast.get_collider()
if collider is Node and collider is RigidBody2D and collider.is_in_group("pushable"):
push_target = collider
push_enabled = true
if not right_cast.is_colliding() and not left_cast.is_colliding():
push_enabled = false
+34
View File
@@ -0,0 +1,34 @@
class_name SceneManager
extends Node2D
var bullet = preload("res://scenes/bullet.tscn")
var grenade = preload("res://scenes/grenade.tscn")
var bullet_array = []
var total_allowed_bullets : int = 7
@onready var game: Node2D = $".."
func make_bullet(_bullet_position, _bullet_speed) -> void:
print("make a bullet; put it in the world")
var my_bullet = bullet_factory()
game.add_child(my_bullet)
my_bullet.transform = _bullet_position
my_bullet.set_speed(_bullet_speed)
func make_grenade(_grenade_position, _grenade_direction) -> void:
print("SM make a grenade")
var my_grenade : Grenade = grenade.instantiate()
game.add_child(my_grenade)
my_grenade.transform = _grenade_position
my_grenade.apply_central_impulse(Vector2(_grenade_direction, -1) * 200)
func bullet_factory() -> Bullet:
var my_bullet : Bullet
if bullet_array.size() < total_allowed_bullets:
# make a new bullet
my_bullet = bullet.instantiate()
game.add_child(my_bullet)
else:
my_bullet = bullet_array.pop_back()
bullet_array.push_front(my_bullet)
return my_bullet
+1
View File
@@ -0,0 +1 @@
uid://b84pdw14l6gb6