Squaby Demo
import QtQuick 2.0
import Felgo 4.0
Item {
id: pathEntity
property real pathSize: scene.gridSize*2
property variant waypoints: []
Component.onCompleted: {
initializeFromWaypoints(waypoints, true);
}
function appendSingleWaypoint(newWaypoint) {
var tempWaypoints = waypoints;
if(!tempWaypoints)
tempWaypoints = new Array;
tempWaypoints.push(newWaypoint);
waypoints = tempWaypoints;
if(waypoints.length < 2) {
return;
}
var lastElementIndex = waypoints.length-1;
var wp = waypoints[lastElementIndex];
var previosWaypoint = waypoints[lastElementIndex-1];
var prevPrevWaypoint;
if(lastElementIndex>=2)
prevPrevWaypoint = waypoints[lastElementIndex-2]
createEntitiesForSingleWaypoint(wp, previosWaypoint, prevPrevWaypoint)
}
function initializeFromWaypoints(initialWaypoints, calledFromOnCompleted) {
console.debug("PathEntity: initializeFromWaypoints:", JSON.stringify(initialWaypoints))
var now = Date.now();
var waypointsStayedEqual = true
if(calledFromOnCompleted ||
!initialWaypoints || !waypoints ||
!waypoints["length"] || !initialWaypoints["length"] ||
waypoints.length !== initialWaypoints.length) {
waypointsStayedEqual = false
} else {
for(var i=0; i<initialWaypoints.length && waypointsStayedEqual; i++) {
console.debug("comparing waypoint index", i)
if(waypoints[i].x !== initialWaypoints[i].x || initialWaypoints[i].y !== initialWaypoints[i].y) {
console.debug("PathEntity: there was a change in the waypoints, at waypoint with index", i)
waypointsStayedEqual = false
break;
}
}
if(waypointsStayedEqual) {
var activePathSections = entityManager.getEntityArrayByType("pathSection")
waypointsStayedEqual = activePathSections.length === initialWaypoints.length-1
}
}
if(waypointsStayedEqual) {
console.debug("PathEntity: waypoints are the same, thus skip removing the old ones and loading the new ones")
return;
}
waypoints = initialWaypoints;
var toRemoveEntityTypes = ["pathSection", "waypoint"];
entityManager.removeEntitiesByFilter(toRemoveEntityTypes);
if(!initialWaypoints) {
console.debug("PathEnttiy: no waypoints were defined - this occurs when a new level is created")
return;
}
for(var i=1; i<initialWaypoints.length; i++) {
var prevPrevWaypoint;
if(i>=2)
prevPrevWaypoint = initialWaypoints[i-2];
var lastWaypoint = initialWaypoints[i-1];
var waypoint = initialWaypoints[i];
createEntitiesForSingleWaypoint(waypoint, lastWaypoint, prevPrevWaypoint);
}
var dt = Date.now() - now;
console.debug("PathEntity: dt for path creation:", dt)
}
function createEntitiesForSingleWaypoint(waypoint, prevWaypoint, prevPrevWaypoint) {
var sectionX;
var sectionY;
var sectionWidth;
var sectionHeight;
sectionWidth = waypoint.x-prevWaypoint.x;
sectionHeight = waypoint.y-prevWaypoint.y;
sectionX = prevWaypoint.x + sectionWidth*0.5;
sectionY = prevWaypoint.y + sectionHeight*0.5;
sectionWidth = Math.abs(sectionWidth);
sectionHeight = Math.abs(sectionHeight);
if(sectionWidth === 0) {
sectionWidth = pathSize;
sectionHeight+=pathSize;
} else if (sectionHeight === 0) {
sectionHeight = pathSize;
sectionWidth+=pathSize;
} else {
console.debug("WARNING: the path is non-90 degree! this is not supported! problematic between last waypoint at pos", prevWaypoint.x, prevWaypoint.y ," and current wp", waypoint.x, waypoint.y, "sectionWidth:", sectionWidth, "sectionHeight:", sectionHeight);
return;
}
if(sectionHeight > sectionWidth) {
var tempForChange = sectionWidth;
sectionWidth = sectionHeight;
sectionHeight = tempForChange;
}
entityManager.createEntityFromUrlWithProperties(Qt.resolvedUrl("PathSection.qml"),
{"x": sectionX, "y":sectionY,
"width": sectionWidth, "height": sectionHeight,
"first": prevWaypoint,
"second": waypoint,
"z": -1
});
if(prevPrevWaypoint) {
entityManager.createEntityFromUrlWithProperties(Qt.resolvedUrl("Waypoint.qml"),
{"x": prevWaypoint.x, "y": prevWaypoint.y,
"prev": prevPrevWaypoint,
"next": waypoint,
"z": -1
});
}