241 lines
7.5 KiB
GDScript3
241 lines
7.5 KiB
GDScript3
|
######################
|
||
|
### Jessist Script ###
|
||
|
######################
|
||
|
# This script is part of Jessist
|
||
|
# Jessist is licensed under GNU GPLv3
|
||
|
#
|
||
|
# This script manages the player object
|
||
|
# It handles animations, physics and movement
|
||
|
extends KinematicBody2D
|
||
|
|
||
|
class_name Player
|
||
|
|
||
|
# Player information
|
||
|
export (int) var playerJump = 600
|
||
|
export (int) var playerSpeed = 175*1.5
|
||
|
export (int) var gravitySpeed = 600
|
||
|
export (int) var currentLevel = 0
|
||
|
export (int) var collectables = 0
|
||
|
var gravityVector = 0
|
||
|
export var moveVector = Vector2(0,0)
|
||
|
export (int) var noGravity = 0
|
||
|
var playerRunning = false
|
||
|
var handleMovementLeftRight = false
|
||
|
export (bool) var isAlive = true
|
||
|
var killAnimation = -10
|
||
|
export (bool) var eventDeathAllowed = true
|
||
|
export (String, "STANDING", "WALKING", "JUMPING") var playerAction = null
|
||
|
export (String, "LEFT", "RIGHT") var playerFacing = "RIGHT"
|
||
|
export (bool) var activateMovement = true
|
||
|
|
||
|
# Cheats
|
||
|
export var cheats_fly = false
|
||
|
export var cheats_moonjump = false
|
||
|
export var cheats_fastspeed = false
|
||
|
export var cheats_nokill = false
|
||
|
|
||
|
func _ready():
|
||
|
gameController.logCall("Player","_ready",null)
|
||
|
gameController.logInfo("Player","_ready","Initializing player object")
|
||
|
|
||
|
func _physics_process(delta):
|
||
|
gameController.logCall("Player","_physics_process",delta)
|
||
|
if not checkIsAlive():
|
||
|
return
|
||
|
if not gameController.gamePaused:
|
||
|
if activateMovement:
|
||
|
moveVector.x = 0
|
||
|
checkResetGravity()
|
||
|
animManager()
|
||
|
if activateMovement:
|
||
|
cheatManager()
|
||
|
inputHandler()
|
||
|
calculateGravity(delta)
|
||
|
gameController.logVelocity("Player","_physics_process",moveVector)
|
||
|
moveVector.y += gravityVector
|
||
|
move_and_slide(moveVector,Vector2(0,-1))
|
||
|
movementChecker()
|
||
|
else:
|
||
|
$PlayerSprite.playing = false
|
||
|
|
||
|
func animManager():
|
||
|
gameController.logCall("Player","animManager",null)
|
||
|
if playerAction == "WALKING":
|
||
|
$PlayerSprite.animation = "Walking"
|
||
|
$PlayerSprite.playing = true
|
||
|
#elif playerAction == "JUMPING":
|
||
|
# $PlayerSprite.animation = "Jumping"
|
||
|
# $PlayerSprite.playing = true
|
||
|
else:
|
||
|
$PlayerSprite.animation = "Walking"
|
||
|
$PlayerSprite.frame = 0
|
||
|
$PlayerSprite.playing = false
|
||
|
if playerFacing == "LEFT":
|
||
|
$PlayerSprite.flip_h = true
|
||
|
var shader = ShaderMaterial.new()
|
||
|
shader.shader = load("res://Shaders/offsetshadow.shader")
|
||
|
shader.set_shader_param("modulate",Color8(0,0,0,102))
|
||
|
shader.set_shader_param("offset",Vector2(-1,1))
|
||
|
$PlayerSprite.material = shader
|
||
|
elif playerFacing == "RIGHT":
|
||
|
$PlayerSprite.flip_h = false
|
||
|
var shader = ShaderMaterial.new()
|
||
|
shader.shader = load("res://Shaders/offsetshadow.shader")
|
||
|
shader.set_shader_param("modulate",Color8(0,0,0,102))
|
||
|
shader.set_shader_param("offset",Vector2(1,1))
|
||
|
$PlayerSprite.material = shader
|
||
|
|
||
|
func cheatManager():
|
||
|
gameController.logCall("Player","cheatManager",null)
|
||
|
if not gameController.allowCheats:
|
||
|
return
|
||
|
if cheats_nokill:
|
||
|
eventDeathAllowed = false
|
||
|
else:
|
||
|
eventDeathAllowed = true
|
||
|
if Input.is_action_just_pressed("cheats_fly"):
|
||
|
if cheats_fly:
|
||
|
cheats_fly = false
|
||
|
gameController.logInfo("Player","cheatManager","CHEATS: cheats_fly is off")
|
||
|
else:
|
||
|
cheats_fly = true
|
||
|
gameController.logInfo("Player","cheatManager","CHEATS: cheats_fly is on")
|
||
|
if Input.is_action_just_pressed("cheats_moonjump"):
|
||
|
if cheats_moonjump:
|
||
|
cheats_moonjump = false
|
||
|
gameController.logInfo("Player","cheatManager","CHEATS: cheats_moonjump is off")
|
||
|
else:
|
||
|
cheats_moonjump = true
|
||
|
gameController.logInfo("Player","cheatManager","CHEATS: cheats_moonjump is on")
|
||
|
if Input.is_action_just_pressed("cheats_fastspeed"):
|
||
|
if cheats_fastspeed:
|
||
|
cheats_fastspeed = false
|
||
|
gameController.logInfo("Player","cheatManager","CHEATS: cheats_fastspeed is off")
|
||
|
else:
|
||
|
cheats_fastspeed = true
|
||
|
gameController.logInfo("Player","cheatManager","CHEATS: cheats_fastspeed is on")
|
||
|
if Input.is_action_just_pressed("cheats_nokill"):
|
||
|
if cheats_nokill:
|
||
|
cheats_nokill = false
|
||
|
gameController.logInfo("Player","cheatManager","CHEATS: cheats_nokill is off")
|
||
|
else:
|
||
|
cheats_nokill = true
|
||
|
gameController.logInfo("Player","cheatManager","CHEATS: cheats_nokill is on")
|
||
|
|
||
|
func inputHandler():
|
||
|
gameController.logCall("Player","inputHandler",null)
|
||
|
if not Input.is_action_pressed("ui_left") and not Input.is_action_pressed("ui_right") or Input.is_action_pressed("ui_left") and Input.is_action_pressed("ui_right"):
|
||
|
playerAction = "STANDING"
|
||
|
handleMovementLeftRight = true
|
||
|
else:
|
||
|
handleMovementLeftRight = false
|
||
|
|
||
|
if Input.is_action_pressed("ui_left"):
|
||
|
gameController.logVerbose("Player","inputHandler","Left pressed")
|
||
|
if not handleMovementLeftRight:
|
||
|
playerFacing = "LEFT"
|
||
|
playerAction = "WALKING"
|
||
|
if cheats_fastspeed:
|
||
|
moveVector.x = moveVector.x-playerSpeed*5
|
||
|
elif playerRunning:
|
||
|
moveVector.x = moveVector.x-playerSpeed*1.5
|
||
|
else:
|
||
|
moveVector.x = moveVector.x-playerSpeed
|
||
|
if Input.is_action_pressed("ui_right"):
|
||
|
gameController.logVerbose("Player","inputHandler","Right pressed")
|
||
|
if not handleMovementLeftRight:
|
||
|
playerFacing = "RIGHT"
|
||
|
playerAction = "WALKING"
|
||
|
if cheats_fastspeed:
|
||
|
moveVector.x = moveVector.x+playerSpeed*5
|
||
|
elif playerRunning:
|
||
|
moveVector.x = moveVector.x+playerSpeed*1.5
|
||
|
else:
|
||
|
moveVector.x = moveVector.x+playerSpeed
|
||
|
if Input.is_action_pressed("ui_up"):
|
||
|
gameController.logVerbose("Player","inputHandler","Up pressed")
|
||
|
if cheats_moonjump:
|
||
|
playerAction = "JUMPING"
|
||
|
moveVector.y = -playerJump
|
||
|
noGravity = 1
|
||
|
else:
|
||
|
if is_on_floor():
|
||
|
playerAction = "JUMPING"
|
||
|
moveVector.y = -playerJump
|
||
|
noGravity = 1
|
||
|
if Input.is_action_pressed("run"):
|
||
|
gameController.logVerbose("Player","inputHandler","Run pressed")
|
||
|
playerRunning = true
|
||
|
else:
|
||
|
playerRunning = false
|
||
|
|
||
|
func checkResetGravity():
|
||
|
gameController.logCall("Player","checkResetGravity",null)
|
||
|
if is_on_ceiling() or is_on_floor():
|
||
|
gameController.logVelocity("Player","checkResetGravity","Player is on ceiling or floor, resetting y movement and gravity")
|
||
|
moveVector.y = 0
|
||
|
noGravity = 0
|
||
|
gravityVector = 0
|
||
|
|
||
|
func calculateGravity(delta):
|
||
|
gameController.logCall("Player","calculateGravity",delta)
|
||
|
if cheats_fly:
|
||
|
gravityVector = 0
|
||
|
noGravity = 0
|
||
|
else:
|
||
|
if not noGravity == 0:
|
||
|
gameController.logVerbose("Player","calculateGravity","noGravity is at " + String(noGravity))
|
||
|
noGravity -= 1
|
||
|
gravityVector = 0
|
||
|
else:
|
||
|
gravityVector += gravitySpeed * delta
|
||
|
if gravityVector > 1600:
|
||
|
gravityVector = 1600
|
||
|
gameController.logVelocity("Player","calculateGravity","gravityVector above 1600, throttling")
|
||
|
|
||
|
func movementChecker():
|
||
|
gameController.logCall("Player","movementChecker",null)
|
||
|
if position.x < 0+14:
|
||
|
position.x = 0+14
|
||
|
if position.y < 0+33:
|
||
|
position.y = 0+33
|
||
|
#if position.x > 1920-14:
|
||
|
# position.x = 1920-14
|
||
|
if position.y > 1080-33:
|
||
|
position.y = 1080-33
|
||
|
|
||
|
func event(event):
|
||
|
gameController.logCall("Player","event",event)
|
||
|
if event == "death":
|
||
|
if eventDeathAllowed:
|
||
|
isAlive = false
|
||
|
checkIsAlive()
|
||
|
else:
|
||
|
gameController.logWarn("Player","event","Invalid event specified. Requested event: " + event)
|
||
|
|
||
|
func checkIsAlive():
|
||
|
gameController.logCall("Player","isAlive",null)
|
||
|
if not isAlive:
|
||
|
if killAnimation == 0:
|
||
|
get_parent().get_node("Camera/GameOver").visible = true
|
||
|
elif killAnimation == -10:
|
||
|
gameController.gamePaused = true
|
||
|
$PlayerSprite.playing = false
|
||
|
$PlayerSprite.frame = 0
|
||
|
$PlayerSprite.animation = "Death"
|
||
|
gameController.gamePaused = true
|
||
|
killAnimation = 0
|
||
|
else:
|
||
|
killAnimation -= 1
|
||
|
return false
|
||
|
else:
|
||
|
return true
|
||
|
|
||
|
func enemyHitZoneEntered(body):
|
||
|
gameController.logCall("Player","enemyHitZoneEntered",body)
|
||
|
if body.name == "EnemySpricky":
|
||
|
eventDeathAllowed = false
|
||
|
noGravity = 1
|
||
|
gravityVector = 0
|
||
|
moveVector.y = -playerJump
|