2022-03-21 00:19:05 -04:00
|
|
|
package funkin.play.character;
|
|
|
|
|
2022-03-23 01:18:23 -04:00
|
|
|
import flixel.math.FlxPoint;
|
2022-03-21 00:19:05 -04:00
|
|
|
import funkin.Note.NoteDir;
|
2022-04-29 11:56:08 -04:00
|
|
|
import funkin.modding.events.ScriptEvent;
|
|
|
|
import funkin.play.character.CharacterData.CharacterDataParser;
|
2022-03-21 00:19:05 -04:00
|
|
|
import funkin.play.stage.Bopper;
|
|
|
|
|
|
|
|
using StringTools;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A Character is a stage prop which bops to the music as well as controlled by the strumlines.
|
|
|
|
*
|
|
|
|
* Remember: The character's origin is at its FEET. (horizontal center, vertical bottom)
|
|
|
|
*/
|
|
|
|
class BaseCharacter extends Bopper
|
|
|
|
{
|
|
|
|
// Metadata about a character.
|
|
|
|
public var characterId(default, null):String;
|
|
|
|
public var characterName(default, null):String;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether the player is an active character (Boyfriend) or not.
|
|
|
|
*/
|
|
|
|
public var characterType:CharacterType = OTHER;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tracks how long, in seconds, the character has been playing the current `sing` animation.
|
|
|
|
* This is used to ensure that characters play the `sing` animations for at least one beat,
|
|
|
|
* preventing them from reverting to the `idle` animation between notes.
|
|
|
|
*/
|
|
|
|
public var holdTimer:Float = 0;
|
|
|
|
|
2022-03-23 01:18:23 -04:00
|
|
|
public var isDead:Bool = false;
|
|
|
|
public var debugMode:Bool = false;
|
|
|
|
|
|
|
|
final _data:CharacterData;
|
2022-03-21 00:19:05 -04:00
|
|
|
final singTimeCrochet:Float;
|
|
|
|
|
2022-03-23 01:18:23 -04:00
|
|
|
/**
|
2022-04-29 23:51:52 -04:00
|
|
|
* The offset between the corner of the sprite and the origin of the sprite (at the character's feet).
|
|
|
|
* cornerPosition = stageData - characterOrigin
|
2022-03-23 01:18:23 -04:00
|
|
|
*/
|
|
|
|
public var characterOrigin(get, null):FlxPoint;
|
|
|
|
|
|
|
|
function get_characterOrigin():FlxPoint
|
|
|
|
{
|
|
|
|
var xPos = (width / 2); // Horizontal center
|
|
|
|
var yPos = (height); // Vertical bottom
|
|
|
|
return new FlxPoint(xPos, yPos);
|
|
|
|
}
|
|
|
|
|
2022-04-29 23:51:52 -04:00
|
|
|
/**
|
|
|
|
* The absolute position of the top-left of the character.
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public var cornerPosition(get, null):FlxPoint;
|
|
|
|
|
|
|
|
function get_cornerPosition():FlxPoint
|
|
|
|
{
|
|
|
|
return new FlxPoint(x, y);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The absolute position of the character's feet, at the bottom-center of the sprite.
|
|
|
|
*/
|
|
|
|
public var feetPosition(get, null):FlxPoint;
|
|
|
|
|
|
|
|
function get_feetPosition():FlxPoint
|
|
|
|
{
|
|
|
|
return new FlxPoint(x + characterOrigin.x, y + characterOrigin.y);
|
|
|
|
}
|
|
|
|
|
2022-03-25 22:30:37 -04:00
|
|
|
/**
|
|
|
|
* Returns the point the camera should focus on.
|
|
|
|
* Should be approximately centered on the character, and should not move based on the current animation.
|
|
|
|
*
|
|
|
|
* Set the position of this rather than reassigning it, so that anything referencing it will not be affected.
|
|
|
|
*/
|
|
|
|
public var cameraFocusPoint(default, null):FlxPoint = new FlxPoint(0, 0);
|
|
|
|
|
2022-03-26 22:18:26 -04:00
|
|
|
override function set_animOffsets(value:Array<Float>)
|
2022-03-25 22:30:37 -04:00
|
|
|
{
|
2022-03-26 22:18:26 -04:00
|
|
|
if (animOffsets == null)
|
|
|
|
animOffsets = [0, 0];
|
|
|
|
if (animOffsets == value)
|
2022-03-25 22:30:37 -04:00
|
|
|
return value;
|
|
|
|
|
2022-03-26 22:18:26 -04:00
|
|
|
var xDiff = animOffsets[0] - value[0];
|
|
|
|
var yDiff = animOffsets[1] - value[1];
|
2022-03-25 22:30:37 -04:00
|
|
|
|
|
|
|
// Call the super function so that camera focus point is not affected.
|
|
|
|
super.set_x(this.x + xDiff);
|
|
|
|
super.set_y(this.y + yDiff);
|
|
|
|
|
2022-03-26 22:18:26 -04:00
|
|
|
return animOffsets = value;
|
2022-03-25 22:30:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If the x position changes, other than via changing the animation offset,
|
|
|
|
* then we need to update the camera focus point.
|
|
|
|
*/
|
2022-03-23 01:18:23 -04:00
|
|
|
override function set_x(value:Float):Float
|
|
|
|
{
|
2022-03-25 22:30:37 -04:00
|
|
|
if (value == this.x)
|
|
|
|
return value;
|
|
|
|
|
|
|
|
var xDiff = value - this.x;
|
|
|
|
this.cameraFocusPoint.x += xDiff;
|
|
|
|
|
2022-03-23 01:18:23 -04:00
|
|
|
return super.set_x(value);
|
|
|
|
}
|
2022-03-21 00:19:05 -04:00
|
|
|
|
2022-03-25 22:30:37 -04:00
|
|
|
/**
|
|
|
|
* If the y position changes, other than via changing the animation offset,
|
|
|
|
* then we need to update the camera focus point.
|
|
|
|
*/
|
|
|
|
override function set_y(value:Float):Float
|
|
|
|
{
|
|
|
|
if (value == this.y)
|
|
|
|
return value;
|
|
|
|
|
|
|
|
var yDiff = value - this.y;
|
|
|
|
this.cameraFocusPoint.y += yDiff;
|
|
|
|
|
|
|
|
return super.set_y(value);
|
|
|
|
}
|
|
|
|
|
2022-03-21 00:19:05 -04:00
|
|
|
public function new(id:String)
|
|
|
|
{
|
|
|
|
super();
|
|
|
|
this.characterId = id;
|
|
|
|
|
|
|
|
_data = CharacterDataParser.fetchCharacterData(this.characterId);
|
|
|
|
if (_data == null)
|
|
|
|
{
|
|
|
|
throw 'Could not find character data for characterId: $characterId';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.characterName = _data.name;
|
|
|
|
this.singTimeCrochet = _data.singTime;
|
2022-03-26 22:18:26 -04:00
|
|
|
this.globalOffsets = _data.offsets;
|
2022-03-21 00:19:05 -04:00
|
|
|
}
|
2022-04-29 11:56:08 -04:00
|
|
|
|
|
|
|
shouldBop = false;
|
2022-03-21 00:19:05 -04:00
|
|
|
}
|
|
|
|
|
2022-03-25 22:30:37 -04:00
|
|
|
/**
|
|
|
|
* Set the sprite scale to the appropriate value.
|
|
|
|
* @param scale
|
|
|
|
*/
|
|
|
|
function setScale(scale:Null<Float>):Void
|
|
|
|
{
|
|
|
|
if (scale == null)
|
|
|
|
scale = 1.0;
|
|
|
|
|
|
|
|
this.scale.x = scale;
|
|
|
|
this.scale.y = scale;
|
|
|
|
this.updateHitbox();
|
|
|
|
}
|
|
|
|
|
2022-03-26 22:18:26 -04:00
|
|
|
/**
|
|
|
|
* The per-character camera offset.
|
|
|
|
*/
|
|
|
|
var characterCameraOffsets(get, null):Array<Float>;
|
|
|
|
|
|
|
|
function get_characterCameraOffsets():Array<Float>
|
|
|
|
{
|
|
|
|
return _data.cameraOffsets;
|
|
|
|
}
|
|
|
|
|
2022-03-25 22:30:37 -04:00
|
|
|
override function onCreate(event:ScriptEvent):Void
|
|
|
|
{
|
2022-03-26 22:18:26 -04:00
|
|
|
// Camera focus point
|
|
|
|
var charCenterX = this.x + this.width / 2;
|
|
|
|
var charCenterY = this.y + this.height / 2;
|
|
|
|
this.cameraFocusPoint = new FlxPoint(charCenterX + _data.cameraOffsets[0], charCenterY + _data.cameraOffsets[1]);
|
2022-03-25 22:30:37 -04:00
|
|
|
super.onCreate(event);
|
|
|
|
}
|
|
|
|
|
2022-03-26 22:18:26 -04:00
|
|
|
public function initHealthIcon(isOpponent:Bool):Void
|
|
|
|
{
|
|
|
|
if (!isOpponent)
|
|
|
|
{
|
|
|
|
PlayState.instance.iconP1.characterId = _data.healthIcon.id;
|
|
|
|
PlayState.instance.iconP1.size.set(_data.healthIcon.scale, _data.healthIcon.scale);
|
|
|
|
PlayState.instance.iconP1.offset.x = _data.healthIcon.offsets[0];
|
|
|
|
PlayState.instance.iconP1.offset.y = _data.healthIcon.offsets[1];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
PlayState.instance.iconP2.characterId = _data.healthIcon.id;
|
|
|
|
PlayState.instance.iconP2.size.set(_data.healthIcon.scale, _data.healthIcon.scale);
|
|
|
|
PlayState.instance.iconP2.offset.x = _data.healthIcon.offsets[0];
|
|
|
|
PlayState.instance.iconP2.offset.y = _data.healthIcon.offsets[1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-21 00:19:05 -04:00
|
|
|
public override function onUpdate(event:UpdateScriptEvent):Void
|
|
|
|
{
|
|
|
|
super.onUpdate(event);
|
|
|
|
|
|
|
|
// Reset hold timer for each note pressed.
|
|
|
|
if (justPressedNote())
|
|
|
|
{
|
|
|
|
holdTimer = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isDead)
|
|
|
|
{
|
|
|
|
playDeathAnimation();
|
|
|
|
}
|
|
|
|
|
2022-03-23 01:18:23 -04:00
|
|
|
if (hasAnimation('idle-end') && getCurrentAnimation() == "idle" && isAnimationFinished())
|
|
|
|
playAnimation('idle-end');
|
|
|
|
if (hasAnimation('singLEFT-end') && getCurrentAnimation() == "singLEFT" && isAnimationFinished())
|
|
|
|
playAnimation('singLEFT-end');
|
|
|
|
if (hasAnimation('singDOWN-end') && getCurrentAnimation() == "singDOWN" && isAnimationFinished())
|
|
|
|
playAnimation('singDOWN-end');
|
|
|
|
if (hasAnimation('singUP-end') && getCurrentAnimation() == "singUP" && isAnimationFinished())
|
|
|
|
playAnimation('singUP-end');
|
|
|
|
if (hasAnimation('singRIGHT-end') && getCurrentAnimation() == "singRIGHT" && isAnimationFinished())
|
|
|
|
playAnimation('singRIGHT-end');
|
|
|
|
|
2022-03-21 00:19:05 -04:00
|
|
|
// Handle character note hold time.
|
|
|
|
if (getCurrentAnimation().startsWith("sing"))
|
|
|
|
{
|
|
|
|
holdTimer += event.elapsed;
|
|
|
|
var singTimeMs:Float = singTimeCrochet * (Conductor.crochet * 0.001); // x beats, to ms.
|
|
|
|
// Without this check here, the player character would only play the `sing` animation
|
|
|
|
// for one beat, as opposed to holding it as long as the player is holding the button.
|
|
|
|
var shouldStopSinging:Bool = (this.characterType == BF) ? !isHoldingNote() : true;
|
|
|
|
|
|
|
|
FlxG.watch.addQuick('singTimeMs-${characterId}', singTimeMs);
|
2022-04-29 11:56:08 -04:00
|
|
|
if (holdTimer > singTimeMs && shouldStopSinging && !getCurrentAnimation().endsWith("miss"))
|
2022-03-21 00:19:05 -04:00
|
|
|
{
|
2022-04-29 11:56:08 -04:00
|
|
|
trace(getCurrentAnimation());
|
2022-03-25 22:30:37 -04:00
|
|
|
// trace('holdTimer reached ${holdTimer}sec (> ${singTimeMs}), stopping sing animation');
|
2022-03-21 00:19:05 -04:00
|
|
|
holdTimer = 0;
|
|
|
|
dance(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
holdTimer = 0;
|
|
|
|
// super.onBeatHit handles the regular `dance()` calls.
|
|
|
|
}
|
|
|
|
FlxG.watch.addQuick('holdTimer-${characterId}', holdTimer);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Since no `onBeatHit` or `dance` calls happen in GameOverSubstate,
|
|
|
|
* this regularly gets called instead.
|
|
|
|
*/
|
|
|
|
public function playDeathAnimation(force:Bool = false):Void
|
|
|
|
{
|
|
|
|
if (force || (getCurrentAnimation().startsWith("firstDeath") && isAnimationFinished()))
|
|
|
|
{
|
|
|
|
playAnimation("deathLoop");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override function dance(force:Bool = false)
|
|
|
|
{
|
2022-03-23 01:18:23 -04:00
|
|
|
// Prevent default dancing behavior.
|
|
|
|
if (debugMode)
|
|
|
|
return;
|
|
|
|
|
2022-03-21 00:19:05 -04:00
|
|
|
if (!force)
|
|
|
|
{
|
|
|
|
if (getCurrentAnimation().startsWith("sing"))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (["hey", "cheer"].contains(getCurrentAnimation()) && !isAnimationFinished())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prevent dancing while another animation is playing.
|
|
|
|
if (!force && getCurrentAnimation().startsWith("sing"))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, fallback to the super dance() method, which handles playing the idle animation.
|
|
|
|
super.dance();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if the player just pressed a note.
|
|
|
|
* Used when determing whether a the player character should revert to the `idle` animation.
|
|
|
|
* On non-player characters, this should be ignored.
|
|
|
|
*/
|
|
|
|
function justPressedNote(player:Int = 1):Bool
|
|
|
|
{
|
|
|
|
// Returns true if at least one of LEFT, DOWN, UP, or RIGHT is being held.
|
|
|
|
switch (player)
|
|
|
|
{
|
|
|
|
case 1:
|
|
|
|
return [
|
|
|
|
PlayerSettings.player1.controls.NOTE_LEFT_P,
|
|
|
|
PlayerSettings.player1.controls.NOTE_DOWN_P,
|
|
|
|
PlayerSettings.player1.controls.NOTE_UP_P,
|
|
|
|
PlayerSettings.player1.controls.NOTE_RIGHT_P,
|
|
|
|
].contains(true);
|
|
|
|
case 2:
|
|
|
|
return [
|
|
|
|
PlayerSettings.player2.controls.NOTE_LEFT_P,
|
|
|
|
PlayerSettings.player2.controls.NOTE_DOWN_P,
|
|
|
|
PlayerSettings.player2.controls.NOTE_UP_P,
|
|
|
|
PlayerSettings.player2.controls.NOTE_RIGHT_P,
|
|
|
|
].contains(true);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if the player is holding a note.
|
|
|
|
* Used when determing whether a the player character should revert to the `idle` animation.
|
|
|
|
* On non-player characters, this should be ignored.
|
|
|
|
*/
|
|
|
|
function isHoldingNote(player:Int = 1):Bool
|
|
|
|
{
|
|
|
|
// Returns true if at least one of LEFT, DOWN, UP, or RIGHT is being held.
|
|
|
|
switch (player)
|
|
|
|
{
|
|
|
|
case 1:
|
|
|
|
return [
|
|
|
|
PlayerSettings.player1.controls.NOTE_LEFT,
|
|
|
|
PlayerSettings.player1.controls.NOTE_DOWN,
|
|
|
|
PlayerSettings.player1.controls.NOTE_UP,
|
|
|
|
PlayerSettings.player1.controls.NOTE_RIGHT,
|
|
|
|
].contains(true);
|
|
|
|
case 2:
|
|
|
|
return [
|
|
|
|
PlayerSettings.player2.controls.NOTE_LEFT,
|
|
|
|
PlayerSettings.player2.controls.NOTE_DOWN,
|
|
|
|
PlayerSettings.player2.controls.NOTE_UP,
|
|
|
|
PlayerSettings.player2.controls.NOTE_RIGHT,
|
|
|
|
].contains(true);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Every time a note is hit, check if the note is from the same strumline.
|
|
|
|
* If it is, then play the sing animation.
|
|
|
|
*/
|
|
|
|
public override function onNoteHit(event:NoteScriptEvent)
|
|
|
|
{
|
|
|
|
super.onNoteHit(event);
|
|
|
|
|
|
|
|
holdTimer = 0;
|
|
|
|
|
|
|
|
if (event.note.mustPress && characterType == BF)
|
|
|
|
{
|
|
|
|
// If the note is from the same strumline, play the sing animation.
|
2022-03-23 01:18:23 -04:00
|
|
|
this.playSingAnimation(event.note.data.dir, false, event.note.data.altNote);
|
2022-03-21 00:19:05 -04:00
|
|
|
}
|
|
|
|
else if (!event.note.mustPress && characterType == DAD)
|
|
|
|
{
|
|
|
|
// If the note is from the same strumline, play the sing animation.
|
2022-03-23 01:18:23 -04:00
|
|
|
this.playSingAnimation(event.note.data.dir, false, event.note.data.altNote);
|
2022-03-21 00:19:05 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Every time a note is missed, check if the note is from the same strumline.
|
|
|
|
* If it is, then play the sing animation.
|
|
|
|
*/
|
|
|
|
public override function onNoteMiss(event:NoteScriptEvent)
|
|
|
|
{
|
|
|
|
super.onNoteMiss(event);
|
|
|
|
|
|
|
|
if (event.note.mustPress && characterType == BF)
|
|
|
|
{
|
|
|
|
// If the note is from the same strumline, play the sing animation.
|
2022-03-23 01:18:23 -04:00
|
|
|
this.playSingAnimation(event.note.data.dir, true, event.note.data.altNote);
|
2022-03-21 00:19:05 -04:00
|
|
|
}
|
|
|
|
else if (!event.note.mustPress && characterType == DAD)
|
|
|
|
{
|
|
|
|
// If the note is from the same strumline, play the sing animation.
|
2022-03-23 01:18:23 -04:00
|
|
|
this.playSingAnimation(event.note.data.dir, true, event.note.data.altNote);
|
2022-03-21 00:19:05 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Every time a wrong key is pressed, play the miss animation if we are Boyfriend.
|
|
|
|
*/
|
|
|
|
public override function onNoteGhostMiss(event:GhostMissNoteScriptEvent)
|
|
|
|
{
|
|
|
|
super.onNoteGhostMiss(event);
|
|
|
|
|
|
|
|
if (event.eventCanceled || !event.playAnim)
|
|
|
|
{
|
|
|
|
// Skipping...
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (characterType == BF)
|
|
|
|
{
|
|
|
|
trace('Playing ghost miss animation...');
|
|
|
|
// If the note is from the same strumline, play the sing animation.
|
|
|
|
this.playSingAnimation(event.dir, true, null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public override function onDestroy(event:ScriptEvent):Void
|
|
|
|
{
|
|
|
|
this.characterType = OTHER;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Play the appropriate singing animation, for the given note direction.
|
|
|
|
* @param dir The direction of the note.
|
|
|
|
* @param miss If true, play the miss animation instead of the sing animation.
|
|
|
|
* @param suffix A suffix to append to the animation name, like `alt`.
|
|
|
|
*/
|
|
|
|
public function playSingAnimation(dir:NoteDir, miss:Bool = false, suffix:String = ""):Void
|
|
|
|
{
|
|
|
|
var anim:String = 'sing${dir.nameUpper}${miss ? 'miss' : ''}${suffix != "" ? '-${suffix}' : ''}';
|
|
|
|
|
|
|
|
// restart even if already playing, because the character might sing the same note twice.
|
|
|
|
playAnimation(anim, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
enum CharacterType
|
|
|
|
{
|
|
|
|
BF;
|
|
|
|
DAD;
|
|
|
|
GF;
|
|
|
|
OTHER;
|
|
|
|
}
|