Squaby Demo
import QtQuick 2.0
import Felgo 4.0
import "../particles"
import "../gameScene"
EntityBase {
entityType: "squaby"
variationType: "squabyYellow"
id: squaby
property int score: 5
property int gold: 5
property alias squabySprite: squabySpriteElement.squabySprite
property alias healthbar: healthbar
property alias pathMovement: pathMovement
property alias healthComponent: healthComponent
property alias health: healthComponent.health
property real damageMultiplicatorNailgun: 1
property real damageMultiplicatorFlamethrower: 1
property real damageMultiplicatorTaser: 1
property real damageMultiplicatorTesla: 1
property alias pathMovementPixelsPerSecond: pathMovement.velocity
property bool movementAnimationNeedUpdate: true
poolingEnabled: true
onMovedToPool: {
console.debug("Squaby: onMovedToPool(), stopping all children components");
if(hitParticle.running)
hitParticle.stopLivingParticles()
if(deathParticle.running)
deathParticle.stopLivingParticles()
squabySprite.running = false
pathMovement.running = false
if(whirlRotationAnimation.running)
whirlRotationAnimation.stop()
if(dieAfterWhirlTimer.running)
dieAfterWhirlTimer.stop()
squaby.rotation = 0
if(collidersActive)
collidersActive=false
}
onUsedFromPool: {
console.debug("Squaby: onUsedFromPool(), re-initialize all children components");
squaby.state = ""
healthComponent.resetHealth();
pathMovement.rotationAnimationEnabled = true
updatePathPosition()
pathMovement.running = true;
if(squabySpriteElement.opacity !== 1)
squabySpriteElement.opacity = 1;
squabySprite.running = true
squabySprite.jumpTo("walk");
collidersActive = true;
}
function updatePathPosition() {
if(movementAnimationNeedUpdate) {
pathMovement.waypoints = level.pathEntity.waypoints
pathMovement.updateAnimationsFromWaypoints();
movementAnimationNeedUpdate = false
} else {
pathMovement.reset()
}
}
GameSoundEffect {
id: dieSound
source: Qt.resolvedUrl("../../assets/snd/squafurScream.wav")
}
GameSoundEffect {
id: dieSound2
source: Qt.resolvedUrl("../../assets/snd/squatanScream.wav")
}
GameParticle {
id: hitParticle
fileName: Qt.resolvedUrl("../particles/SplatterParticle.json")
x: -5
}
GameParticle {
id: deathParticle
fileName: Qt.resolvedUrl("../particles/DeathParticle.json")
}
SquabySprite {
id: squabySpriteElement
NumberAnimation on opacity {
id: hideAnimation
to: 0
duration: 1000
running: false
easing.type: Easing.InQuart
onStarted: {
console.debug("Squaby: start fadeout animation after dying")
}
onStopped: {
console.debug("Squaby: finished fadeout animation after dying, remove self")
squaby.removeEntity();
squabyCreator.squabyDied(variationType)
}
}
Timer {
id: waitTimer
interval: deathParticle.duration*1000 + 100
running: false
onTriggered: hideAnimation.start();
}
onAnimationFinished: {
console.debug("Squaby: GameSpriteSequence.onAnimationFinished was emitted, this means the died animation is over");
hideAnimation.start()
}
}
PathMovement {
id: pathMovement
velocity: 100
waypoints: level.pathEntity.waypoints
onPathCompleted: {
if(waypoints.length !== level.pathEntity.waypoints.length)
return;
console.debug("Squaby: pathCompleted, destroy squaby");
player.lives--;
squaby.removeEntity();
squabyCreator.squabyDied(variationType)
}
}
Healthbar {
id:healthbar
absoluteX: -width/2
absoluteY: -16
width: 32
height: 3
percent: healthComponent.healthInPercent
visible: percent<1 && squaby.state!="died"
useSpriteVersion: true
}
HealthComponent {
id: healthComponent
damageMultiplicators: {
"nailgun": damageMultiplicatorNailgun,
"flamethrower": damageMultiplicatorFlamethrower,
"taser": damageMultiplicatorTaser,
"tesla": damageMultiplicatorTesla
}
onDied: {
console.debug("onDied called for squaby");
squaby.state = "died"
squaby.collidersActive = false;
whirlRotationAnimation.stop()
dieAfterWhirlTimer.stop()
deathParticle.start();
die();
squabySprite.jumpTo("die");
pathMovement.running = false;
}
}
CircleCollider {
id: collider
radius: scene.gridSize
collisionTestingOnlyMode: true
categories: Box.Category1
collidesWith: Box.Category2
}
NumberAnimation on rotation {
id: whirlRotationAnimation
running: false
}
property string whirlingTurbineId
Timer {
id: dieAfterWhirlTimer
onTriggered: {
console.debug("Squaby: end of whirling towards turbine, kill squaby");
var whirlingTurbineEntity = entityManager.getEntityById(whirlingTurbineId);
if(whirlingTurbineEntity) {
whirlingTurbineEntity.whirlingOfSquabySuccessful(entityId);
} else {
console.debug("Squaby: no whirlingTurbine exists - it must have been sold during the whirling process!");
}
die();
squaby.removeEntity();
squabyCreator.squabyDied(variationType)
}
}
function hitWithAttackerIdAndType(attackerId, attackerType) {
if (attackerType === "nailgun" && currentParticles < maximumParticles)
hitParticle.start();
healthComponent.hitWithAttackerIdAndType(attackerId, attackerType);
}
function hitByValue(attackerId, attackerType, damageValue) {
healthComponent.hitByValue(attackerId, attackerType, damageValue);
}
function startWhirlingToTarget(towerTarget) {
console.debug("Squaby: startWhirlingToTarget()")
movementAnimationNeedUpdate = true
squabySprite.jumpTo("whirl");
pathMovement.rotationAnimationEnabled = false;
var whirlWaypoints = [ {x:squaby.x, y:squaby.y}, {x:towerTarget.x, y: towerTarget.y} ];
pathMovement.waypoints = whirlWaypoints;
whirlRotationAnimation.to = towerTarget.rotation;
whirlRotationAnimation.duration = pathMovement.completePathAnimationDuration*0.9;
whirlRotationAnimation.running = true;
whirlingTurbineId = towerTarget.entityId;
console.debug("whirlingTurbine set to", squaby.whirlingTurbineId);
console.debug("set dieAfterWhirlTimter to almost pathDuration:", pathMovement.completePathAnimationDuration);
dieAfterWhirlTimer.interval = pathMovement.completePathAnimationDuration*0.9;
dieAfterWhirlTimer.running = true;
}
function die() {
if(!system.debugBuild) {
var random = Math.random();
if(random>0.5)
dieSound.play();
else
dieSound2.play();
}
player.score += squaby.score
player.gold += squaby.gold
}