extends KinematicBody2D var MAX_SPEED = 600 var ACCELERATION = 1800 var FRICTION = 2000 var MAX_LIFE = 30 const PICKUP_BONUS = 5 var vel = Vector2.ZERO var curLife = MAX_LIFE var coroutines = [] enum EInput{ UP, RIGHT, DOWN, LEFT } const baseInputs = ["ui_up", "ui_right", "ui_down", "ui_left"] var inputs = ["ui_up", "ui_right", "ui_down", "ui_left"] onready var animationPlayer = $AnimationPlayer onready var animationTree = $AnimationTree onready var animationState = animationTree.get("parameters/playback") var healthBar = null func _ready(): if self.name == "Player1": healthBar = get_tree().get_nodes_in_group("healthbar")[0] elif self.name == "Player2": healthBar = get_tree().get_nodes_in_group("healthbar")[1] healthBar.maxHealthUpdated(MAX_LIFE) func _process(delta): updatePaintEffects(delta) curLife -= delta if curLife <= 0: get_node("/root/World/GameManager").endGame() healthBar.healthUpdated(curLife) func _physics_process(delta): var input_vector = Vector2.ZERO if self.name == "Player1": input_vector.x = Input.get_action_strength(inputs[EInput.RIGHT]) - Input.get_action_strength(inputs[EInput.LEFT]) input_vector.y = Input.get_action_strength(inputs[EInput.DOWN]) - Input.get_action_strength(inputs[EInput.UP]) elif self.name == "Player2": input_vector.x = Input.get_action_strength(inputs[EInput.RIGHT] + "2") - Input.get_action_strength(inputs[EInput.LEFT] + "2") input_vector.y = Input.get_action_strength(inputs[EInput.DOWN] + "2") - Input.get_action_strength(inputs[EInput.UP] + "2") input_vector = input_vector.normalized() if input_vector != Vector2.ZERO: animationTree.set("parameters/Idle/blend_position", input_vector) animationTree.set("parameters/Run/blend_position", input_vector) animationState.travel("Run") vel = vel.move_toward(input_vector * MAX_SPEED, ACCELERATION * delta) else: animationState.travel("Idle") vel = vel.move_toward(Vector2.ZERO, FRICTION * delta) vel = move_and_slide(vel) func updatePaintEffects(delta): var sprite = get_node("Sprite") var updated = [] for rout in coroutines: if rout != null && rout.is_valid(): var new_rout = rout.resume(delta) if new_rout != null && new_rout.is_valid(): updated.append(new_rout) coroutines.remove(coroutines.find(rout)) for rout in updated: coroutines.append(rout) func addCoroutine(routine): curLife = MAX_LIFE if (curLife + PICKUP_BONUS > MAX_LIFE) else curLife + PICKUP_BONUS coroutines.append(routine)