Platformer Demo
import Felgo 4.0
import QtQuick 2.0
import "entities"
import "levels"
Scene {
id: gameScene
width: 480
height: 320
gridSize: 32
property int offsetBeforeScrollingStarts: 240
EntityManager {
id: entityManager
}
Rectangle {
anchors.fill: gameScene.gameWindowAnchorItem
color: "#74d6f7"
}
ParallaxScrollingBackground {
sourceImage: Qt.resolvedUrl("../assets/background/layer2.png")
anchors.bottom: gameScene.gameWindowAnchorItem.bottom
anchors.horizontalCenter: gameScene.gameWindowAnchorItem.horizontalCenter
movementVelocity: player.x > offsetBeforeScrollingStarts ? Qt.point(-player.horizontalVelocity,0) : Qt.point(0,0)
ratio: Qt.point(0.3,0)
}
ParallaxScrollingBackground {
sourceImage: Qt.resolvedUrl("../assets/background/layer1.png")
anchors.bottom: gameScene.gameWindowAnchorItem.bottom
anchors.horizontalCenter: gameScene.gameWindowAnchorItem.horizontalCenter
movementVelocity: player.x > offsetBeforeScrollingStarts ? Qt.point(-player.horizontalVelocity,0) : Qt.point(0,0)
ratio: Qt.point(0.6,0)
}
Item {
id: viewPort
height: level.height
width: level.width
anchors.bottom: gameScene.gameWindowAnchorItem.bottom
x: player.x > offsetBeforeScrollingStarts ? offsetBeforeScrollingStarts-player.x : 0
PhysicsWorld {
id: physicsWorld
gravity: Qt.point(0, 25)
debugDrawVisible: false
z: 1000
onPreSolve: contact => {
var entityA = contact.fixtureA.getBody().target
var entityB = contact.fixtureB.getBody().target
if(entityB.entityType === "platform" && entityA.entityType === "player" &&
entityA.y + entityA.height > entityB.y) {
contact.enabled = false
}
}
}
Level1 {
id: level
}
Player {
id: player
x: 20
y: 100
}
ResetSensor {
width: player.width
height: 10
x: player.x
anchors.bottom: viewPort.bottom
onContact: {
player.x = 20
player.y = 100
}
Rectangle {
anchors.fill: parent
color: "yellow"
opacity: 0.5
}
}
}
Rectangle {
anchors.right: parent.right
anchors.bottom: parent.bottom
height: 50
width: 150
color: "blue"
opacity: 0.4
Rectangle {
anchors.centerIn: parent
width: 1
height: parent.height
color: "white"
}
MultiPointTouchArea {
anchors.fill: parent
onPressed: touchPoints => {
if(touchPoints[0].x < width/2)
controller.xAxis = -1
else
controller.xAxis = 1
}
onUpdated: touchPoints => {
if(touchPoints[0].x < width/2)
controller.xAxis = -1
else
controller.xAxis = 1
}
onReleased: controller.xAxis = 0
}
}
Rectangle {
anchors.left: parent.left
anchors.bottom: parent.bottom
height: 100
width: 100
color: "green"
opacity: 0.4
Text {
anchors.centerIn: parent
text: "jump"
color: "white"
font.pixelSize: 9
}
MouseArea {
anchors.fill: parent
onPressed: player.jump()
}
}
Keys.forwardTo: controller
TwoAxisController {
id: controller
onInputActionPressed: (actionName, isPressed) => {
console.debug("key pressed actionName " + actionName)
if(actionName == "up") {
player.jump()
}