Squaby Demo
import QtQuick 2.0
import Felgo 4.0
EntityBaseDraggable {
id: towerBase
z: 2
property QtObject targetEntity: null
property alias fireRangeCollider: collider
property real colliderRadius: 4*scene.gridSize
inLevelEditingMode: true
property int shootDelayInMilliSeconds
property int cost
property int saleRevenue
property variant upgradeLevels
property variant __currentUpgradeLevels: { "range": 0, "shootDelay": 0 }
property int __saleRevenue: saleRevenue
signal aimingAtTargetChanged(bool aimingAtTarget)
signal targetRemoved
signal towerUpgradedWithCustomUpgrade(string upgradeType, variant upgradeData)
signal towerUpgraded(string upgradeType, variant upgradeData)
selectionMouseArea.width: foundationCollider.width
selectionMouseArea.height: foundationCollider.height
selectionMouseArea.anchors.centerIn: towerBase
colliderComponent: foundationCollider
colliderCategoriesWhileDragged: Box.Category4
colliderCollidesWithWhileDragged: Box.Category3
clickingAllowed: scene.state !== "levelEditing"
draggingAllowed: false
showRectangleWhenBuildingNotAllowed: false
property bool emitAimingAtTargetSignal: true
onEntityClicked: {
console.debug("tower clicked");
towerSelected();
}
Connections {
id: targetEntityConnection
target: targetEntity
function onEntityDestroyed() {
console.debug("entityDestroyed received in targetEntityConnection, set target to 0");
removeTarget();
}
}
CircleCollider {
id: collider
radius: colliderRadius
x: -radius
y: -radius
collisionTestingOnlyMode: true
categories: Box.Category2
fixture.collidesWith: Box.Category1
Component.onCompleted: {
console.debug("isSensor of nailgun is", collider.fixture.sensor);
}
fixture.onBeginContact: (other, contactNormal) => {
if(targetEntity)
return;
var fixture = other;
var body = other.getBody();
var entity = body.target;
setTarget(entity);
}
Connections {
target: targetEntity ? null : collider.fixture
function onContactChanged(other) {
if(targetEntity) {
console.debug("TowerBase: onContactChanged() - this should never be called, because the connection shouldnt be enabled when no targetEntity exists!")
return;
}
console.debug("target of tower got removed, set to new one...")
var entity = other.getBody().target;
setTarget(entity);
}
}
fixture.onEndContact: other => {
var entity = other.getBody().target;
if(entity === targetEntity)
removeTarget();
}
}
BoxCollider {
id: foundationCollider
width: 2*scene.gridSize-1
height: 2*scene.gridSize-1
collisionTestingOnlyMode: true
categories: Box.Category3
collidesWith: Box.Category4
anchors.centerIn: parent
body.fixedRotation: true
}
MoveToPointHelper {
id: moveToPointHelper
targetObject: targetEntity
allowSteerForward: false
property real aimingAngleThreshold: 10
property bool aimingAtTarget: false
onAimingAtTargetChanged: {
console.debug("TowerBase: aimintAtTarget changed to", aimingAtTarget);
if(emitAimingAtTargetSignal) {
towerBase.aimingAtTargetChanged(aimingAtTarget);
}
}
onTargetObjectChanged: {
console.debug("TowerBase: targetObject changed to", targetObject);
if(!targetObject)
aimingAtTarget = false;
}
onAbsoluteRotationDifferenceChanged: {
if(absoluteRotationDifference < aimingAngleThreshold && !aimingAtTarget) {
aimingAtTarget = true;
} else if(absoluteRotationDifference > aimingAngleThreshold && aimingAtTarget) {
aimingAtTarget = false;
}
}
}
MovementAnimation {
target: towerBase
property: "rotation"
running: targetEntity ? true : false
velocity: 300*moveToPointHelper.outputXAxis
maxPropertyValueDifference: moveToPointHelper.absoluteRotationDifference
}
function removeTarget() {
console.debug("TowerBase.removeTarget() called");
targetEntity = null;
towerBase.targetRemoved();
}
function setTarget(target) {
console.debug("TowerBase: setTarget() called for", target);
console.debug("TowerBase: previous targetEntity (should be 0!):", targetEntity);
targetEntity = target;
}
function towerSelected() {
var message = createTowerInfo();
hud.towerSelected(message);
}
function createTowerInfo() {
var upgradeArrayForGui = new Array();
for (var upgradeType in upgradeLevels) {
var currentUpgradeLevelForType = __currentUpgradeLevels[upgradeType];
var currentUpgradesForType = upgradeLevels[upgradeType];
if(currentUpgradeLevelForType >= currentUpgradesForType.length)
currentUpgradeLevelForType = currentUpgradesForType.length-1;
var currentUpgradeForType = currentUpgradesForType[currentUpgradeLevelForType];
var upgradeData = { type: upgradeType,
cost: currentUpgradeForType.cost,
upgradeLevel: currentUpgradeForType.level,
currentPlayerUpgradeLevel: __currentUpgradeLevels[upgradeType],
status: currentUpgradeForType.status
}
upgradeArrayForGui.push( upgradeData );
}
var message = {
entityId: towerBase.entityId,
entityType: towerBase.entityType,
colliderRadius: collider.radius,
saleRevenue: towerBase.__saleRevenue,
upgrades: upgradeArrayForGui,
towerPosition: {x:towerBase.x, y:towerBase.y}
}
return message;
}
function upgradeTower(upgradeType) {
if(upgradeType === "sell") {
if(!scene.tutorials.nextAction("upgradeButton","sold"))
return
if(entityType === "nailgun")
player.nailgunsDestroyed++;
else if(entityType === "flamethrower")
player.flamethrowersDestroyed++;
else if(entityType === "taser")
player.tasersDestroyed++;
else if(entityType === "tesla")
player.teslasDestroyed++;
else if(entityType === "turbine")
player.turbinesDestroyed++;
flurry.logEvent("Tower.Sold", {"towerType": entityType, "playerGold": player.gold, "wave": player.wave});
player.gold += towerBase.__saleRevenue;
entityManager.removeEntityById(towerBase.entityId);
} else {
var currentUpgradeLevelForType = __currentUpgradeLevels[upgradeType];
var currentUpgradesForType = upgradeLevels[upgradeType];
if(currentUpgradeLevelForType < currentUpgradesForType.length) {
var currentUpgradeForType = currentUpgradesForType[currentUpgradeLevelForType];
if(currentUpgradeForType.cost > player.gold) {
print("insufficient funds for upgrading the weapon with upgradeType: " + upgradeType);
return false;
}
flurry.logEvent("Tower.Upgraded", { "upgradeType": upgradeType, "towerType": variationType, "playerGold": player.gold, "wave": player.wave});
print("upgrade can be bought... value for upgrade:", currentUpgradeForType.value);
player.gold -= currentUpgradeForType.cost;
__saleRevenue += currentUpgradeForType.additionalSaleRevenue;
if(upgradeType === "range") {
colliderRadius = currentUpgradeForType.value;
console.debug("update colliderRadius from", colliderRadius, "to", currentUpgradeForType.value)
} else if(upgradeType === "shootDelay")
shootDelayInMilliSeconds = currentUpgradeForType.value;
else
towerUpgradedWithCustomUpgrade(upgradeType, currentUpgradeForType);
towerUpgraded(upgradeType, currentUpgradeForType);
var newUpgradeLevels = __currentUpgradeLevels;
newUpgradeLevels[upgradeType]++;
__currentUpgradeLevels = newUpgradeLevels;
var message = createTowerInfo();
sprite.setTowerImageFromEvent(message);
console.debug("upgradeLevel changed to", __currentUpgradeLevels[upgradeType], "for upgrade type", upgradeType);
towerSelected();
} else {
print("no upgrading is possible because no more upgrades are available for upgradetype: " + upgradeType );
return false;
}
}