Simple Ninja Game Demo
import Felgo 4.0
import QtQuick 2.0
GameWindow {
screenWidth: 960
screenHeight: 640
property bool splashFinished: false
onSplashScreenFinished: { splashFinished = true}
property bool gameWon
property int monstersDestroyed
onMonstersDestroyedChanged: {
if(monstersDestroyed > 5) {
changeToGameOverScene(true)
}
}
EntityManager {
id: entityManager
entityContainer: scene
}
BackgroundMusic {
source: Qt.resolvedUrl("../assets/snd/background-music-aac.wav")
}
Rectangle {
anchors.fill: parent
color: "white"
}
Scene {
id: scene
width: 480
height: 320
PhysicsWorld {debugDrawVisible: false}
MultiResolutionImage {
id: player
anchors.verticalCenter: parent.verticalCenter
source: Qt.resolvedUrl("../assets/img/Player.png")
}
Component {
id: monster
EntityBase {
entityType: "monster"
MultiResolutionImage {
id: monsterImage
source: Qt.resolvedUrl("../assets/img/Target.png")
}
y: utils.generateRandomValueBetween(0, scene.height)
NumberAnimation on x {
from: scene.width
to: -monsterImage.width
duration: utils.generateRandomValueBetween(2000, 4000)
onStopped: {
console.debug("monster reached base - change to gameover scene because the player lost")
changeToGameOverScene(false)
}
}
BoxCollider {
anchors.fill: monsterImage
collisionTestingOnlyMode: true
fixture.onBeginContact: (other, contactNormal) => {
var collidedEntity = other.getBody().target
console.debug("collided with entity", collidedEntity.entityType)
if(collidedEntity.entityType === "projectile") {
monstersDestroyed++
collidedEntity.removeEntity()
removeEntity()
}
}
}
}
}
Component {
id: projectile
EntityBase {
entityType: "projectile"
MultiResolutionImage {
id: monsterImage
source: Qt.resolvedUrl("../assets/img/Projectile.png")
}
property point destination
property int moveDuration
PropertyAnimation on x {
from: player.x
to: destination.x
duration: moveDuration
}
PropertyAnimation on y {
from: player.y
to: destination.y
duration: moveDuration
}
BoxCollider {
anchors.fill: monsterImage
collisionTestingOnlyMode: true
}
}
}
GameSoundEffect {
id: projectileCreationSound
source: Qt.resolvedUrl("../assets/snd/pew-pew-lei.wav")
}
MouseArea {
anchors.fill: parent
onReleased: {
var offset = Qt.point(
mouseX - player.x,
mouseY - player.y
);
if(offset.x <= 0)
return;
var realX = scene.gameWindowAnchorItem.width
var ratio = offset.y / offset.x
var realY = (realX * ratio) + player.y
var destination = Qt.point(realX, realY)
var offReal = Qt.point(realX - player.x, realY - player.y)
var length = Math.sqrt(offReal.x*offReal.x + offReal.y*offReal.y)
var velocity = 480
var realMoveDuration = length / velocity * 1000
entityManager.createEntityFromComponentWithProperties(projectile, {"destination": destination, "moveDuration": realMoveDuration})
projectileCreationSound.play()
}
}
}
Scene {
id: gameOverScene
visible: false
Text {
anchors.centerIn: parent
text: gameWon ? "You won :)" : "You lost"
}
onVisibleChanged: {
if(visible) {
returnToGameSceneTimer.start()
}
}
Timer {
id: returnToGameSceneTimer
interval: 3000
onTriggered: {
scene.visible = true
gameOverScene.visible = false
}
}
}
Timer {
running: scene.visible == true && splashFinished
repeat: true
interval: 1000
onTriggered: addTarget()
}
function addTarget() {
console.debug("create a new monster")
entityManager.createEntityFromComponent(monster)
}
function changeToGameOverScene(won) {
gameWon = won
gameOverScene.visible = true
scene.visible = false
monstersDestroyed = 0