Durdles - 2-Player Action Game
import QtQuick 2.0
import Felgo 4.0
import "../common" as Common
import ".."
import "../entities"
import "../entities/powerUps"
import "../scenes"
Rectangle {
id: movement
radius: GameInfo.radius
opacity: GameInfo.opacity
color: "transparent"
focus: true
property var player
property var tank
property alias playerMovementImage: playerMovementImage
property double originX
property double originY
Image {
id: playerMovementImage
source: Qt.resolvedUrl("../../assets/img/Control.png")
opacity: GameInfo.testLevel ? 100 : 0
z: 5
width: 90
height: 90
x: originX
y: originY
}
MultiPointTouchArea {
z: -1
enabled: GameInfo.gamePaused ? false : true
anchors.fill: parent
property bool rotateOnce: true
property int referencePointX: 0
property int referencePointY: 0
property bool didRegisterReferencePoint: false;
property variant playerTwoAxisController: tank.getComponent("TwoAxisController")
property real newPosX: 0.0
property real newPosY: 0.0
property real oldPosX: 0.0
property real oldPosY: 0.0
touchPoints: [
TouchPoint {id: fieldPoint}
]
onUpdated: {
tank.circleCollider.linearDamping = 0
tank.tankBody.playing = true
newPosX = ((fieldPoint.x - referencePointX + playerMovementImage.width / 2) / (playerMovementImage.width / 2) - 1)
newPosY = ((fieldPoint.y - referencePointY + playerMovementImage.height / 2) / (playerMovementImage.height / 2) - 1)
var distance = Math.sqrt((newPosX*newPosX) + (newPosY*newPosY))
if (didRegisterReferencePoint == false) {
didRegisterReferencePoint = true;
referencePointX = fieldPoint.x;
referencePointY = fieldPoint.y;
updatePlayerMovementImagePosition()
return;
}
if (distance > 1) {
var angle = (Math.atan2(newPosX, newPosY) * 180 / Math.PI) - 180
angle = angle * (-1)
angle -= 90
var newX = (playerMovementImage.width / 2) * Math.cos((angle) * Math.PI / 180) + referencePointX
var newY = (playerMovementImage.height / 2) * Math.sin((angle) * Math.PI / 180) + referencePointY
var diffX = fieldPoint.x - newX
var diffY = fieldPoint.y - newY
if ((referencePointX + diffX) <= (playerMovementImage.width / 2)){
referencePointX = playerMovementImage.width / 2
}else if ((referencePointX + diffX) >= (movement.width - playerMovementImage.width/2)){
referencePointX = movement.width - playerMovementImage.width/2
}else{
referencePointX += diffX
}
if ((referencePointY + diffY) <= playerMovementImage.height / 2){
referencePointY = playerMovementImage.height / 2
}else if ((referencePointY + diffY) >= (movement.height - playerMovementImage.height / 2)){
referencePointY = movement.height - playerMovementImage.height / 2
}else{
referencePointY += diffY
}
}
updatePlayerMovementImagePosition()
newPosY = newPosY * -1
if (newPosX > 1) newPosX = 1
if (newPosY > 1) newPosY = 1
if (newPosX < -1) newPosX = -1
if (newPosY < -1) newPosY = -1
if ((player.onLake)){
newPosX = oldPosX +(newPosX * 0.03)
newPosY = oldPosY +(newPosY * 0.03)
if (newPosX > 1) newPosX = 1
if (newPosY > 1) newPosY = 1
if (newPosX < -1) newPosX = -1
if (newPosY < -1) newPosY = -1
}
updateMovement()
}
function updatePlayerMovementImagePosition() {
var newX = referencePointX - playerMovementImage.width / 2;
var newY = referencePointY - playerMovementImage.height / 2;
newX = Math.max(0, newX);
newX = Math.min(movement.width - playerMovementImage.width, newX);
newY = Math.max(0, newY);
newY = Math.min(movement.height - playerMovementImage.height, newY);
playerMovementImage.x = newX;
playerMovementImage.y = newY;
}
function damping(){
if (!player.onLake){
tank.circleCollider.linearDamping = GameInfo.damping
}
}
function updateMovement(){
oldPosX = newPosX
oldPosY = newPosY
newPosX = newPosX * GameInfo.maximumPlayerVelocity
newPosY = newPosY * GameInfo.maximumPlayerVelocity
var velocity = Math.sqrt(newPosX * newPosX + newPosY * newPosY)
var maxVelocity = GameInfo.maximumPlayerVelocity
if (player.activateAccelerator){
maxVelocity *= GameInfo.speed
newPosX *= GameInfo.speed
newPosY *= GameInfo.speed
}
if (velocity > maxVelocity) {
var shrinkFactor = maxVelocity / velocity
newPosX = newPosX * shrinkFactor
newPosY = newPosY * shrinkFactor
}
playerTwoAxisController.xAxis = newPosX
playerTwoAxisController.yAxis = newPosY
var angle = calcAngle(newPosX, newPosY) - 90
if (newPosX !=0 && newPosY != 0){
tank.tankBody.rotation = angle
tank.circleCollider.rotation = angle
if (GameInfo.easyMode)tank.tankCannon.rotation = angle + 90
}
}
onReleased: {
didRegisterReferencePoint = false;
tank.tankBody.playing=false
damping()
}
onEnabledChanged: {
if (rotateOnce){
tank.tankCannon.rotation = 90
if(player.isOfType("playerBlue")){
tank.tankBody.rotation = 180
tank.tankCannon.rotation = 270
}
rotateOnce = false
}
}
}
function calcAngle(touchX, touchY) {
return -180 / Math.PI * Math.atan2(touchY, touchX)
}
function reset(){
playerMovementImage.x = originX;
playerMovementImage.y = originY;
}
Connections {
target: gameScene
function onOffLake(playerVariationType) {
console.debug("Signal offLake")
if (player.variationType === playerVariationType){
if (player.onLake === false && tank.tankBody.playing === false){
tank.circleCollider.linearDamping = GameInfo.damping
}
}
}