2024-08-27 01:18:57 +00:00
|
|
|
extends CharacterBody2D
|
|
|
|
|
|
|
|
|
|
|
|
const SPEED = 300.0
|
|
|
|
const JUMP_VELOCITY = -400.0
|
2024-09-10 01:08:24 +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 #wait for one full game tick to happen before you boot this up
|
|
|
|
@onready var left_ray: RayCast2D = $LeftRay
|
|
|
|
@onready var marker_right: Node2D = $MarkerRight
|
|
|
|
@onready var marker_left: Node2D = $MarkerLeft
|
2024-09-17 01:06:38 +00:00
|
|
|
@onready var playerSprite: AnimatedSprite2D = $AnimatedSprite2D
|
|
|
|
|
2024-09-10 01:08:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
var pushTarget
|
|
|
|
#what we are leveraging our pushforce against
|
|
|
|
var bullet = preload("res://scenes/bullets.tscn") #how we make with code any of the scenes we created
|
|
|
|
#goes into resources and goes to bullet and puts on standby. ready to make the bullet..
|
2024-08-27 01:18:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
func _physics_process(delta: float) -> void:
|
|
|
|
# Add the gravity. where main game loop is gonna go
|
|
|
|
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:08:24 +00:00
|
|
|
|
|
|
|
if Input.is_action_just_pressed("Shove") && pushLeftEnabled && faceLeft:
|
|
|
|
print("shove a box on the left")
|
|
|
|
pushTarget.apply_central_impulse(Vector2(-1,0) * PUSH_FORCE * 1.5) #-1 in game grid is left direction
|
|
|
|
pushLeftEnabled = false
|
|
|
|
|
|
|
|
|
|
|
|
if Input.is_action_just_pressed("Shove") && pushRightEnabled && not faceLeft:
|
|
|
|
print("shove a box on the right")
|
|
|
|
pushTarget.apply_central_impulse(Vector2(1,0) * PUSH_FORCE * 1.5) #1 is right direction
|
|
|
|
pushRightEnabled = false
|
|
|
|
|
|
|
|
if Input.is_action_just_pressed("shoot"):
|
2024-09-17 01:06:38 +00:00
|
|
|
|
2024-09-10 01:08:24 +00:00
|
|
|
var mybullet = bullet.instantiate() #will make bullet but won't position or put bullet on screen
|
2024-08-27 01:18:57 +00:00
|
|
|
|
2024-09-10 01:08:24 +00:00
|
|
|
if faceLeft:
|
|
|
|
print("shoot left")
|
2024-09-17 01:06:38 +00:00
|
|
|
#mybullet.setSpeed(-700)
|
|
|
|
#mybullet.transform = marker_left.global_transform
|
|
|
|
%SceneManager.placeBullet(-700, marker_left.global_transform)
|
2024-09-10 01:08:24 +00:00
|
|
|
else:
|
|
|
|
print("shoot right")
|
|
|
|
#position bullet
|
2024-09-17 01:06:38 +00:00
|
|
|
#mybullet.transform = marker_right.global_transform #saying give me global coordinates for ..
|
2024-09-10 01:08:24 +00:00
|
|
|
#put bullet on screen
|
2024-09-17 01:06:38 +00:00
|
|
|
#owner.add_child(mybullet) #adds bullet when facing both directions
|
|
|
|
#saying place bullet in global coordinates not local
|
|
|
|
%SceneManager.placeBullet(700, marker_right.global_transform)
|
2024-09-10 01:08:24 +00:00
|
|
|
|
2024-08-27 01:18:57 +00:00
|
|
|
# Get the input direction and handle the movement/deceleration.
|
|
|
|
# As good practice, you should replace UI actions with custom gameplay actions.
|
2024-09-10 01:08:24 +00:00
|
|
|
var direction := Input.get_axis("ui_left", "ui_right")
|
|
|
|
#code tells us whether we are facing left or right
|
|
|
|
if direction <0:
|
|
|
|
faceLeft = true
|
2024-09-17 01:06:38 +00:00
|
|
|
playerSprite.flip_h = true
|
2024-09-10 01:08:24 +00:00
|
|
|
if direction >0:
|
|
|
|
faceLeft = false
|
2024-09-17 01:06:38 +00:00
|
|
|
playerSprite.flip_h = false
|
|
|
|
|
2024-08-27 01:18:57 +00:00
|
|
|
if direction:
|
|
|
|
velocity.x = direction * SPEED
|
|
|
|
else:
|
|
|
|
velocity.x = move_toward(velocity.x, 0, SPEED)
|
2024-09-17 01:06:38 +00:00
|
|
|
|
|
|
|
if is_on_floor():
|
|
|
|
#looks at angle to see if flat surface or if surface is wall... 45 deg angle is floor
|
|
|
|
if direction == 0:
|
|
|
|
playerSprite.play("idle")
|
|
|
|
else:
|
|
|
|
playerSprite.play("run")
|
|
|
|
else:
|
|
|
|
playerSprite.play("jump")
|
2024-09-10 01:08:24 +00:00
|
|
|
#blue text is part of the engine!!!!
|
|
|
|
move_and_slide() #this is where all the collisions are recorded
|
2024-08-27 01:18:57 +00:00
|
|
|
for i in get_slide_collision_count():
|
|
|
|
var c = get_slide_collision(i)
|
|
|
|
if c.get_collider() is RigidBody2D: #checking if its a crate
|
|
|
|
c.get_collider().apply_central_impulse(-c.get_normal()*100)
|
|
|
|
|
|
|
|
#i is gonna be increased by one by every colision as I move
|
2024-09-10 01:08:24 +00:00
|
|
|
if left_ray.is_colliding(): #tells if leftray has something hitting it
|
|
|
|
print("left ray collision")
|
|
|
|
var collider = left_ray.get_collider() #get me what it is colliding with on left
|
|
|
|
if collider is Node: #might not be game object like light, camera...
|
|
|
|
if collider.is_in_group("pushables"):
|
|
|
|
pushLeftEnabled = true
|
|
|
|
pushTarget = collider
|
|
|
|
else:
|
|
|
|
#do something else
|
|
|
|
pushLeftEnabled = false
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if right_ray.is_colliding():
|
|
|
|
print("right ray collision")
|
|
|
|
var collider = right_ray.get_collider()
|
|
|
|
if collider is Node:
|
|
|
|
if collider.is_in_group("pushables"):
|
|
|
|
pushRightEnabled = true
|
|
|
|
pushTarget = collider
|
|
|
|
|
|
|
|
else:
|
|
|
|
pushRightEnabled = false
|
|
|
|
|
|
|
|
|