KieranGodot/august26godotcompkieran/Sripts/player.gd

110 lines
2.9 KiB
GDScript3
Raw Normal View History

2024-08-27 01:19:01 +00:00
extends CharacterBody2D
const SPEED = 300.0
const JUMP_VELOCITY = -400.0
2024-09-10 01:02:15 +00:00
const PUSH_FORCE = 700
var faceLeft = false
# can I push right or left
var pushLeftEnabled = false
var pushRightEnabled = false
@onready var right_ray: RayCast2D = $RightRay
@onready var left_ray: RayCast2D = $LeftRay
@onready var marker_right: Node2D = $MarkerRight
@onready var marker_left: Node2D = $MarkerLeft
@onready var playerSprite: AnimatedSprite2D = $AnimatedSprite2D
2024-09-10 01:02:15 +00:00
var pushTarget
var bullet = preload("res://Scenes/Bullet.tscn")
2024-08-27 01:19:01 +00:00
func killplayer():
print("dying")
#play death
playerSprite.play("Death")
2024-08-27 01:19:01 +00:00
func _physics_process(delta: float) -> void:
# Add the gravity.
if not is_on_floor():
velocity += get_gravity() * delta
# Handle jump.
if Input.is_action_just_pressed("ui_accept") and is_on_floor():
velocity.y = JUMP_VELOCITY
2024-09-10 01:02:15 +00:00
if Input.is_action_just_pressed("Shove") && pushLeftEnabled && faceLeft:
print("Shove left")
pushTarget.apply_central_impulse(Vector2(-1,0) * PUSH_FORCE * 1.5)
pushLeftEnabled = false
if Input.is_action_just_pressed("Shove") && pushRightEnabled && not faceLeft:
print("Shove right")
pushTarget.apply_central_impulse(Vector2(1,0) * PUSH_FORCE * 1.5)
pushRightEnabled = false
if Input.is_action_just_pressed("Shoot"):
2024-09-10 01:02:15 +00:00
if faceLeft:
print("shoot left")
#mybullet.setSpeed(-700)
#mybullet.transform = marker_left.global_transform
%SceneManager.placeBullet(-700, marker_left.global_transform)
2024-09-10 01:02:15 +00:00
else:
print("shoot right")
#mybullet.transform = marker_right.global_transform
%SceneManager.placeBullet(700, marker_right.global_transform)
2024-08-27 01:19:01 +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")
2024-09-10 01:02:15 +00:00
if direction <0:
faceLeft = true
playerSprite.flip_h = true
2024-09-10 01:02:15 +00:00
if direction >0:
faceLeft = false
playerSprite.flip_h = false
2024-09-10 01:02:15 +00:00
2024-08-27 01:19:01 +00:00
if direction:
velocity.x = direction * SPEED
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
if is_on_floor():
if direction == 0:
playerSprite.play("Idle")
else:
playerSprite.play("Run")
else:
playerSprite.play("Jump")
2024-08-27 01:19:01 +00:00
move_and_slide()
for i in get_slide_collision_count():
var c = get_slide_collision(i)
if c. get_collider() is RigidBody2D:
2024-09-10 01:02:15 +00:00
c.get_collider().apply_central_impulse(-c.get_normal()* 50)
if left_ray.is_colliding():
print("left ray collistion")
var collider = left_ray.get_collider()
if collider is Node:
if collider.is_in_group("Pushable"):
pushLeftEnabled = true
pushTarget = collider
else:
# do somthing else
pushLeftEnabled = false
if right_ray.is_colliding():
print("right ray collistion")
var collider = right_ray.get_collider()
if collider is Node:
if collider.is_in_group("Pushable"):
pushRightEnabled = true
pushTarget = collider
else:
pushRightEnabled = false