fixed all my bugs and did grenades and animations

This commit is contained in:
CormacRatledge committed 2026-05-04 21:00:45 -04:00
1 parent 5d34946622
commit 51803d0406
14 files changed
+370 -44

No files matched your search

+22
View File
@@ -0,0 +1,22 @@
class_name grenade extends RigidBody2D
var timer = Timer.new()
func _ready() -> void:
add_child(timer)
timer.wait_time = 2
timer.one_shot = true
timer.connect("timeout", explode)
timer.start()
func explode()->void:
print("boom boom")
self.queue_free()
func _on_body_entered(body: Node) -> void:
print("the grenade collision works")
if body.is_in_group("fragile"):
body.queue_free()
explode()
+1
View File
@@ -0,0 +1 @@
uid://wctwda5bor10
+61 -13
View File
@@ -1,51 +1,99 @@
class_name Player extends CharacterBody2D
@onready var rightcast: RayCast2D = $rightcast
@onready var leftcast: RayCast2D = $leftcast
const SPEED =200.0
const JUMP_VELOCITY = -300.0
const JUMP_VELOCITY = -500.0
var direction
enum FaceDirection{LEFT, RIGHT}
var facing:FaceDirection = FaceDirection
var facing:FaceDirection = FaceDirection.RIGHT
enum player_state{IDLE,RUNNING}
var current_player_state:player_state = player_state.IDLE
@onready var player_graphic: AnimatedSprite2D = $"player graphic"
@onready var rightspawn: Marker2D = $rightspawn
@onready var leftspawn: Marker2D = $leftspawn
@onready var right_cast: RayCast2D = $RightCast
@onready var left_cast: RayCast2D = $LeftCast
var pushTarget
var pushEnabled:bool=false
func _ready() -> void:
print(right_cast)
func _physics_process(delta: float) -> void:
handle_input()
handle_movement(delta)
handle_state()
handle_animation()
move_and_slide()
handle_collisions()
func handle_input()->void:
if Input.is_action_just_pressed("chuck"):
print("chuck grenade")
%SceneManager.makeGrenade(rightspawn.global_transform, 1)
if Input.is_action_just_pressed("ui_accept"):
velocity.y = JUMP_VELOCITY
if Input.is_action_just_pressed("magic push"):
print ("may the force be with you")
if Input.is_action_just_pressed("magic push") and 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)
# Get the input direction and handle the movement/deceleration.
# As good practice, you should replace UI actions with custom gameplay actions.
direction = Input.get_axis("ui_left", "ui_right")
if direction<0:
facing = FaceDirection.LEFT
player_graphic.flip_h = true
if direction>0:
facing = FaceDirection.RIGHT
player_graphic.flip_h = false
func handle_movement(delta)->void:
if not is_on_floor():
velocity += get_gravity() * delta
# Handle jump.
if direction:
velocity.x = direction * SPEED
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
func handle_collisions()->void:
if rightcast.is_colliding() and facing==FaceDirection.RIGHT:
print("yay")
move_and_slide()
if right_cast.is_colliding() && facing==FaceDirection.RIGHT:
var collider = right_cast.get_collider()
if collider is Node && collider is RigidBody2D:
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:
pushTarget = collider
pushEnabled = true
for i in get_slide_collision_count():
var c = get_slide_collision(i)
if c.get_collider() is RigidBody2D:
#deliver the impact
c.get_collider().apply_central_impulse(-c.get_normal() * 100)
func handle_state()->void:
match current_player_state:
player_state.IDLE when velocity.x !=0:
print("runnnnnn")
current_player_state = player_state.RUNNING
player_state.RUNNING when velocity.x ==0:
current_player_state = player_state.IDLE
func handle_animation()->void:
match current_player_state:
player_state.IDLE:
player_graphic.play("idle")
player_state.RUNNING:
player_graphic.play("run")
+8
View File
@@ -1,4 +1,6 @@
class_name SM extends Node2D
var grenade = preload("res://grenade.tscn")
@onready var game: Node2D = $".."
@onready var cube_container: Node2D = $"../cube container"
@@ -23,3 +25,9 @@ func updateCube()->void:
_CubeTotal +=1
print ("Number of cubes: "+str(_CubeTotal))
func makeGrenade(_grenadeposition, _grenadedirection)->void:
print("grenade time")
var myGrenade:grenade = grenade.instantiate()
owner.add_child(myGrenade)
myGrenade.transform = _grenadeposition
myGrenade.apply_central_impulse(Vector2(_grenadedirection, -1)*400)