ChickenOutbreak2 Demo
import QtQuick 2.0
import Felgo 4.0
EntityBase {
entityType: "player"
preventFromRemovalFromEntityManager: true
signal leftPressed(variant event)
signal rightPressed(variant event)
signal upPressed(variant event)
signal downPressed(variant event)
signal died
signal deathAnimationFinished()
property int score: 0
property int bonusScore: 0
property int intermediateScore: (score + (bonusScore * bonusScoreForCoin) + scoreFromItemEditor)
property int totalScore: (intermediateScore >= 0 ? intermediateScore : 0)
property int deaths: 0
onDeathsChanged: {
died()
}
property int bonusTextScore: 0
property bool parachute : false
property int doublePointsTime: 0
property int parachutes: 0
property int beams: 0
property int collectedGrains: 0
property int collectedBadGrains: 0
property bool __isLookingRight: true
property alias controller: twoAxisController
property alias collider : collider
property alias teleportParticle: teleportParticle
property alias teleportGlowParticle: teleportGlowParticle
property bool controlsEnabled: true
width: collider.radius * 2
height: collider.radius * 2
Text {
id: bonusText
text: bonusTextScore<0 ? "("+ bonusTextScore*-1 : bonusTextScore
visible: bonusTextScore!=0 && displayBonusTextTimer.running
font.family: fontHUD.name
font.pixelSize: 17
color: bonusTextScore<0 ? "#62f45f" : "white"
anchors.bottom: sprite.top
anchors.topMargin: 5
anchors.horizontalCenter: parent.horizontalCenter
}
Image {
z:4
source: Qt.resolvedUrl("../../assets/parachute.png")
width: 30
height: 30
x: {
if(twoAxisController.xAxis>0) return sprite.x+sprite.width/2-width/2-4
else if(twoAxisController.xAxis<0) return sprite.x+sprite.width/2-width/2+4
else return sprite.x+sprite.width/2-width/2
}
y: sprite.y-20
visible: parachute
rotation: {
if(twoAxisController.xAxis>0) return -12
else if(twoAxisController.xAxis<0) return 12
else return 0
}
}
Image {
id: sprite
source: store.blackHen.purchased ? "../../assets/chicken2-front.png" : "../../assets/chicken4-front.png"
z:5
anchors.centerIn: parent
width: 30
height: 30
visible: false
x:0
y:0
}
Image {
id: spriteMovement
source: store.blackHen.purchased ? "../../assets/chicken2-side.png" : "../../assets/chicken4-side.png"
z:5
anchors.centerIn: parent
width: 30
height: 30
visible: false
mirror: __isLookingRight
}
Image {
id: spriteFlying
source: store.blackHen.purchased ? "../../assets/chicken2-fly.png" : "../../assets/chicken4-fly.png"
z:5
anchors.centerIn: parent
width: 30
height: 30
visible: false
}
Image {
id: spriteDead
source: store.blackHen.purchased ? "../../assets/chicken2-dead.png" : "../../assets/chicken4-dead.png"
z:5
anchors.centerIn: parent
width: 30
height: 30
visible: false
}
property int blockCollisions: 0
onBlockCollisionsChanged: {
if(blockCollisions==0){
state = "fly";
} else {
if(lastState === "fly") {
state = "default"
}
}
}
CircleCollider {
id: collider
bodyType: Body.Dynamic
fixedRotation: true
linearDamping: henLinearDamping
friction: henFriction
restitution: 0
gravityScale: parachute ? parachuteGravityScale : 1
sleepingAllowed: false
radius: 14
fixture.onBeginContact: (other, contactNormal) => {
var fixture = other;
var body = fixture.getBody();
var collidedEntity = body.target;
var collidedEntityType = collidedEntity.entityType;
if(collidedEntityType === "coin") {
collidedEntity.removeEntity();
if(doublePointsTime>0) {
bonusScore+=2
bonusTextScore += bonusScoreForCoin*2
} else {
bonusScore++
bonusTextScore += bonusScoreForCoin
}
collectedGrains++
cornParticle.start()
displayBonusTextTimer.restart()
audioManager.play(audioManager.idCORN)
} if(collidedEntityType === "badCoin") {
collidedEntity.removeEntity();
if(doublePointsTime>0) {
bonusScore-=4
bonusTextScore -= bonusScoreForCoin*4
} else {
bonusScore-=2
bonusTextScore -= bonusScoreForCoin*2
}
collectedBadGrains++
skullParticle.start()
displayBonusTextTimer.restart()
audioManager.play(audioManager.idBADCORN)
} else if(collidedEntityType === "trippleCoin") {
collidedEntity.removeEntity();
if(doublePointsTime>0) {
bonusScore+=6
bonusTextScore += bonusScoreForCoin*6
} else {
bonusScore+=3
bonusTextScore += bonusScoreForCoin*3
}
collectedGrains++
cornParticle.start()
displayBonusTextTimer.restart()
audioManager.play(audioManager.idCORN)
} else if(collidedEntityType === "parachute") {
collidedEntity.removeEntity();
if(doublePointsTime>0) {
bonusScore+=2
bonusTextScore += bonusScoreForCoin*2
} else {
bonusScore++
bonusTextScore += bonusScoreForCoin
}
displayBonusTextTimer.restart()
parachuteCount++
audioManager.play(audioManager.idPOWERUP)
} else if(collidedEntityType === "doublePoints") {
collidedEntity.removeEntity();
if(doublePointsTime>0) {
bonusScore+=2
bonusTextScore += bonusScoreForCoin*2
} else {
bonusScore++
bonusTextScore += bonusScoreForCoin
}
displayBonusTextTimer.restart()
doublePointsTime = doublePointsDuration
if(level.state == "running") doublePointsTimer.start()
audioManager.play(audioManager.idPOWERUP)
} else if(collidedEntityType === "roost") {
blockCollisions++;
if(parachute === true) startOrStopParachute()
} else if(collidedEntityType === "wheel") {
if(parachute === true) startOrStopParachute()
}
}
fixture.onEndContact: (other, contactNormal) => {
var fixture = other;
var body = fixture.getBody();
var collidedEntity = body.target;
var collidedEntityType = collidedEntity.entityType;
if(collidedEntityType === "roost") {
blockCollisions--;
}
}
}
property bool updateAccelerometer: true
function updateWithAccData(reading) {
if(!updateAccelerometer)
return
if(state !== "fly" && Math.abs(reading.x) < accelerometerOffset)
twoAxisController.xAxis = 0
else twoAxisController.xAxis = reading.x * -accelerometerMultiplier;
}
TwoAxisController {
id: twoAxisController
inputActionsToKeyCode: {
"up": Qt.Key_Up,
"down": Qt.Key_Down,
"left": Qt.Key_Left,
"right": Qt.Key_Right,
"fire": Qt.Key_Space
}
onInputActionPressed: (actionName, isPressed) => {
if(!system.desktopPlatform)
return
if(actionName === "fire" && controlsEnabled) {
startOrStopParachute()
} else if(actionName === "up" && controlsEnabled) {
activateTeleport()
} else if(actionName === "down") {
deactivateTeleport()
}
}
}
Timer {
id: updateTimer
interval: 1000 / 60
running: true
repeat: true
onTriggered: {
if(level.state === "running") {
var xAxis = controller.xAxis;
if(xAxis < 0 || xAxis > 0) {
collider.body.linearVelocity.x = xAxis*rightValue;
if(blockCollisions!=0) {
state = "moveLeftRight";
}
if(xAxis>0)
__isLookingRight = true;
else if(xAxis<0)
__isLookingRight = false;
}
}
}
}
Timer {
id: doublePointsTimer
running: false
repeat: true
onTriggered: {
if(doublePointsTime>0) {
audioManager.play(audioManager.idTICK)
doublePointsTime--
} else {
stop()
}
}
}
Timer {
id: displayBonusTextTimer
interval: 500
running: false
repeat: false
onTriggered: {
bonusTextScore = 0
}
}
GameParticle {
id: cornParticle
z: 1
fileName: Qt.resolvedUrl("../particles/CornSplatter.json")
autoStart: false
anchors.centerIn: parent
}
GameParticle {
id: skullParticle
z: 1
fileName: Qt.resolvedUrl("../particles/SkullParticle.json")
autoStart: false
anchors.centerIn: parent
}
GameParticle {
id: teleportParticle
z: 1
fileName: Qt.resolvedUrl("../particles/TeleportParticle.json")
autoStart: false
anchors.centerIn: parent
}
GameParticle {
id: teleportGlowParticle
z: 1
fileName: Qt.resolvedUrl("../particles/TeleportGlowParticle.json")
autoStart: false
duration: ParticleBase.Infinite
anchors.centerIn: parent
}
onStateChanged: {
lastState = state
}
property string lastState: "default"
state: "default"
states: [
State {
name: "default"
PropertyChanges { target: sprite; visible: true }
},
State {
name: "fly"
PropertyChanges { target: spriteFlying; visible: true }
},
State {
name: "moveLeftRight"
PropertyChanges { target: spriteMovement; visible: true }
}
]
onParachuteChanged: {
if(parachute === true) parachuteCount--
}
function startOrStopParachute() {
if(parachute === true) parachute = false
else if(state==="fly" && parachuteCount > 0) {
player.parachutes++
audioManager.play(audioManager.idPARACHUTE)
parachute = true
}
}
function start() {
controlsEnabled = true
updateAccelerometer = true
collider.body.linearDamping = henLinearDamping
collider.gravityScale = Qt.binding(function() {return parachute ? parachuteGravityScale : 1})
spriteDead.visible = false
if(doublePointsTime>0) doublePointsTimer.start()
}
function stop() {
controlsEnabled = false
twoAxisController.xAxis = 0
collider.body.linearVelocity.x = 0
updateAccelerometer = false
collider.body.linearDamping = 10
collider.gravityScale = 0
doublePointsTimer.stop()
}
function prepareForDeath() {
spriteDead.visible = true
}
function saveIt() {
spriteDead.visible = false
}
function killIt() {
audioManager.play(audioManager.idGAMEOVER)
saveitDialog.setDissable()
lastScore = player.totalScore;
player.deaths++;
spriteDead.visible = false
deathAnimationFinished()