NovemberGodotGame2024/novembergame/scripts/dudecontroller.gd

140 lines
3.5 KiB
GDScript3
Raw Normal View History

2024-11-19 01:50:07 +00:00
extends CharacterBody2D
const SPEED = 300.0
const JUMP_VELOCITY = -400.0
const BUMP_FORCE = 50
2024-11-26 01:52:14 +00:00
const PUSH_FORCE = 750
2024-12-03 01:49:35 +00:00
2024-11-26 01:52:14 +00:00
@onready var right_ray: RayCast2D = $RightRay
@onready var left_ray: RayCast2D = $LeftRay
2024-12-03 01:49:35 +00:00
@onready var playerSprite: AnimatedSprite2D = $AnimatedSprite2D
2024-11-26 01:52:14 +00:00
var faceLeft = false
var pushTarget
var pushLeftEnabled = false
var pushRightEnabled = false
2024-12-03 01:49:35 +00:00
var isJumping = false
var animPlaying = "idle"
var living = true
signal playerDead
2024-11-26 01:52:14 +00:00
@onready var right_target: Node2D = $RightTarget
@onready var left_target: Node2D = $LeftTarget
func hurtPlayer(amt):
print("Character knows it should hurt")
animPlaying = "hurt"
playerSprite.play("hurt")
func killPlayer():
print("Characters know it is the end")
if living:
living = false
animPlaying = "death"
playerSprite.play(animPlaying)
2024-11-19 01:50:07 +00:00
func _physics_process(delta: float) -> void:
if living and not animPlaying == "hurt":
# Add the gravity.
if not is_on_floor():
velocity += get_gravity() * delta
2024-11-19 01:50:07 +00:00
# Handle jump.
if Input.is_action_just_pressed("ui_accept") and is_on_floor():
velocity.y = JUMP_VELOCITY
2024-11-19 01:50:07 +00:00
# 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")
if Input.is_action_just_pressed("shove") && pushRightEnabled && faceLeft == false:
print("shove a box")
pushTarget.apply_central_impulse(Vector2(1,0) * PUSH_FORCE * 2)
pushRightEnabled = false
if Input.is_action_just_pressed("shove") && faceLeft == true && pushLeftEnabled == true:
pushTarget.apply_central_impulse(Vector2(-1,0) * PUSH_FORCE * 2)
pushLeftEnabled = false
2024-11-26 01:52:14 +00:00
if Input.is_action_just_pressed("shoot"):
print("shoot a bullet")
var mybullet = %SceneManager.bulletFactory()
if not faceLeft:
mybullet.transform = right_target.global_transform
mybullet.setSpeed(400)
if faceLeft:
mybullet.transform = left_target.global_transform
mybullet.setSpeed(-400)
2024-12-03 01:49:35 +00:00
if direction:
velocity.x = direction * SPEED
if direction > 0:
faceLeft = false
if direction < 0:
faceLeft = true
2024-12-03 01:49:35 +00:00
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
2024-12-03 01:49:35 +00:00
2024-11-26 01:52:14 +00:00
if faceLeft:
playerSprite.flip_h = true
else:
playerSprite.flip_h = false
if is_on_floor():
isJumping = false
if direction == 0:
playerSprite.play("idle")
else:
playerSprite.play("run")
else:
if not isJumping:
playerSprite.play("jump")
isJumping = true
2024-11-26 01:52:14 +00:00
move_and_slide()
if right_ray.is_colliding():
print("Something is on my right")
if not faceLeft:
var collider = right_ray.get_collider()
if collider is Node:
if collider.is_in_group("box"):
print("This is a pushable box")
pushTarget = collider
pushRightEnabled = true
else:
pushRightEnabled = false
if left_ray.is_colliding():
print("Something is on my left")
if faceLeft:
var collider = left_ray.get_collider()
if collider is Node:
if collider.is_in_group("box"):
print("This is a pushable box")
pushTarget = collider
pushLeftEnabled = true
else:
pushLeftEnabled = false
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_FORCE)
2024-11-19 01:50:07 +00:00
func _on_animation_finished() -> void:
if animPlaying == "hurt":
animPlaying = "idle"
if animPlaying == "death":
## emita custom signal
playerDead.emit()