Platformer with Level Editor
import QtQuick 2.0
import Felgo 3.0
EntityBaseDraggable {
id: entityBase
property var scene: gameScene
property alias image: sprite
property point lastPosition
Component.onCompleted: lastPosition = Qt.point(x, y)
width: sprite.width
height: sprite.height
gridSize: scene.gridSize
selectionMouseArea {
anchors.fill: sprite
onPressed: {
if(scene.editorOverlay.sidebar.activeTool == "hand") {
mouse.accepted = false
}
}
}
inLevelEditingMode: scene.state === "edit"
dragOffset: Qt.point(0, 0)
delayDragOffset: true
notAllowedRectangle.anchors.fill: sprite
poolingEnabled: true
ignoreBounds: true
onEntityClicked: scene.editorOverlay.clickEntity(entityBase)
onEntityStateChanged: {
if(entityState == "entityDragged")
audioManager.playSound("dragEntity")
}
onEntityReleased: {
if(lastPosition.x < 0) return
var currentPosition = scene.editorOverlay.snapToGrid(x, y)
if(lastPosition !== currentPosition) {
var undoObjectProperties = {"target": entityBase, "action": "move",
"otherPosition": lastPosition, "currentPosition": currentPosition}
var undoObject = scene.editorOverlay.undoHandler.createUndoObject(undoObjectProperties)
scene.editorOverlay.undoHandler.push([undoObject])
lastPosition = currentPosition
audioManager.playSound("createOrDropEntity")
}
}
onXChanged: positionChanged()
onYChanged: positionChanged()
onUsedFromPool: {
lastPosition = Qt.point(x, y)
}
MultiResolutionImage {
id: sprite
}
function positionChanged() {
if(entityBase.entityState == "entityDragged") {
var xScreen = entityBase.x * scene.container.scale + scene.container.x
var leftLimit = scene.editorOverlay.sidebar.width - 8 * scene.container.scale
var yScreen = entityBase.y * scene.container.scale + scene.container.y
var bottomLimit = scene.gameWindowAnchorItem.height - 17 * scene.container.scale
if(xScreen < leftLimit || yScreen > bottomLimit)
forbidBuild = true
else
forbidBuild = false