Platformer with Level Editor
function clickEntity(entity) {
if(sidebar.activeTool == "erase") {
var undoObject = removeEntity(entity);
if(undoObject)
undoHandler.push([undoObject]);
}
}
function removeEntity(entity) {
if(!entity.preventFromRemovalFromEntityManager) {
var undoObjectProperties = {"target": entity, "action": "remove",
"currentPosition": Qt.point(entity.x, entity.y)};
var undoObject = undoHandler.createUndoObject(undoObjectProperties);
entityManager.removeEntityById(entity.entityId);
audioManager.playSound("removeEntity");
return undoObject;
}
}
function getMouseGridPos(mouseX, mouseY) {
var mouseInLevel = mouseToLevelCoordinates(mouseX, mouseY);
var snappedMousePos = snapToGrid(mouseInLevel.x, mouseInLevel.y);
return snappedMousePos;
}
function isBodyIn32Grid(position) {
var pos1 = Qt.point(position.x + 8, position.y + 8);
var pos2 = Qt.point(position.x + 8, position.y + 24);
var pos3 = Qt.point(position.x + 24, position.y + 8);
var pos4 = Qt.point(position.x + 24, position.y + 24);
if(physicsWorld.bodyAt(pos1) !== null) return true;
if(physicsWorld.bodyAt(pos2) !== null) return true;
if(physicsWorld.bodyAt(pos3) !== null) return true;
if(physicsWorld.bodyAt(pos4) !== null) return true;
return false;
}
function placeEntityAtPosition(mouseX, mouseY) {
if(mouseX < 100 || mouseY > editorOverlay.scene.gameWindowAnchorItem.height)
return;
if(selectedButton === null)
return;
var mouseGridPos = getMouseGridPos(mouseX, mouseY);
if(isBodyIn32Grid(mouseGridPos))
return;
var properties = {};
console.debug("selectedButton.createdEntity", selectedButton.createdEntity);
properties.variationType = selectedButton.createdEntity.variationType;
var levelCoords = mouseToLevelCoordinates(mouseX, mouseY);
var pos = snapToGrid(levelCoords.x, levelCoords.y);
properties.x = pos.x;
properties.y = pos.y;
properties.visible = true;
var entityId = entityManager.createEntityFromUrlWithProperties(selectedButton.toCreateEntityTypeUrl, properties);
var entity = entityManager.getEntityById(entityId);
console.debug("Placed entity "+entity+" at "+entity.x+", "+entity.y);
audioManager.playSound("createOrDropEntity");
return entity;
}
function mouseToLevelCoordinates(mouseX, mouseY) {
var realX = (mouseX - containerComponent.x) / camera.zoom;
var realY = (mouseY - containerComponent.y) / camera.zoom;
return Qt.point(realX, realY);
}
function snapToGrid(levelX, levelY) {
var gridPositionX = Math.floor(levelX / gridSize);
var gridPositionY = Math.floor(levelY / gridSize);
var newX = gridPositionX * gridSize;
var newY = gridPositionY * gridSize;
return Qt.point(newX, newY);
}
function saveLevel() {
var saveProperties = {levelMetaData: {}, customData: {}};
saveProperties.levelMetaData.levelName = levelEditor.currentLevelName;
saveProperties.customData.background = bgImage.bg;
saveProperties.customData.playerX = player.x;
saveProperties.customData.playerY = player.y;
gameWindow.levelEditor.saveCurrentLevel(saveProperties);
}
function initEditor() {
editorOverlay.scene.gridSize = 32;
sidebar.gridSizeButton.screenText = "32";
undoHandler.reset();
sidebar.reset();
}
function resetEditor() {