Platformer with Level Editor
import QtQuick 2.0
import Felgo 3.0
Item {
id: undoObject
property var target
property var targetEntityType
property var targetVariationType
property string action
property point currentPosition
property point otherPosition
onTargetChanged: {
if(target !== null) {
targetEntityType = target.entityType
targetVariationType = target.variationType
}
}
function undo() {
if(action == "create") {
console.debug("undo create "+target+", at "+currentPosition)
removeTargetObject()
}
else if(action == "remove") {
console.debug("undo remove "+target+", at "+currentPosition)
createTargetObject()
}
else if(action == "move") {
console.debug("undo move "+target+", from "+otherPosition
+" to "+currentPosition)
moveTargetObject()
}
else {
console.debug("unknown action: "+action)
}
}
function redo() {
if(action == "create") {
console.debug("redo create "+target+", at "+currentPosition)
createTargetObject()
}
else if(action == "remove") {
console.debug("redo remove "+target+", at "+currentPosition)
removeTargetObject()
}
else if(action == "move") {
console.debug("redo move "+target+", from "+otherPosition
+" to "+currentPosition)
moveTargetObject()
}
else {
console.debug("unknown action: "+action)
}
}
function createTargetObject() {
var properties = {
entityType: targetEntityType,
variationType: targetVariationType,
x: currentPosition.x,
y: currentPosition.y
}
var newEntityId = entityManager.createEntityFromEntityTypeAndVariationType(properties)
target = entityManager.getEntityById(newEntityId)
}
function removeTargetObject() {
var bodyAtPos = Qt.point(currentPosition.x + 16, currentPosition.y + 16)
target = gameScene.physicsWorld.bodyAt(bodyAtPos).target
if(target) {
entityManager.removeEntityById(target.entityId)
}
}
function moveTargetObject() {
var bodyAtPos = Qt.point(currentPosition.x + 16, currentPosition.y + 16)
target = gameScene.physicsWorld.bodyAt(bodyAtPos).target
if(target) {
target.x = otherPosition.x
target.y = otherPosition.y
target.lastPosition = Qt.point(target.x, target.y)
var helper = Qt.point(currentPosition.x, currentPosition.y)
currentPosition = otherPosition
otherPosition = helper
}