Durdles - 2-Player Action Game
import QtQuick 2.0
import Felgo 4.0
import "../levels"
import "../common"
import ".."
EntityBase {
id: opponent
entityType: "opponent"
property alias opponent: opponent
property alias opponentBody: opponentBody
property alias circleCollider: circleCollider
property alias shootSound1: shootSound1
property alias shootSound2: shootSound2
property bool targetTankRed: true
AnimatedImage {
playing: false
id: opponentBody
width: 20
height: 27
rotation: 180
anchors.centerIn: parent
z: 2
}
Image {
id: shadow
width: 20
height: 27
rotation: 180
anchors.centerIn: parent
source: Qt.resolvedUrl("../../assets/img/Shadow.png")
z: 1
}
GameSoundEffect {
volume: 0.3
id: shootSound1
source: Qt.resolvedUrl("../../assets/snd/Snowman1.wav")
}
GameSoundEffect {
volume: 0.3
id: shootSound2
source: Qt.resolvedUrl("../../assets/snd/Snowman2.wav")
}
function shoot() {
var random = Math.floor(Math.random() * 2) + 1
if (random == 1){
shootSound1.play()
}else{
shootSound2.play()
}
}
CircleCollider {
id: circleCollider
radius: 10
x: -radius
y: -radius
bodyType: Body.Static
}
MoveToPointHelper {
enabled: GameInfo.gamePaused ? false : true
id: moveToPointHelper
targetObject: targetTankRed ? tankRed : tankBlue;
property bool opponentShooting: false
distanceToTargetThreshold: 80
Timer {
interval: 100;
running: GameInfo.gamePaused ? false : true;
repeat: GameInfo.gamePaused ? false : true;
onTriggered: {
var distanceRed = Math.sqrt(Math.pow(tankRed.x - opponent.x, 2) + Math.pow(tankRed.y - opponent.y, 2));
var distanceBlue = Math.sqrt(Math.pow(tankBlue.x - opponent.x, 2) + Math.pow(tankBlue.y - opponent.y, 2));
targetTankRed = (distanceRed >= distanceBlue) ? false : true;
}
}
Timer {
interval: 3000;
running: GameInfo.gamePaused ? false : true;
repeat: GameInfo.gamePaused ? false : true;
onTriggered: {
if (parent.opponentShooting) {
opponentBody.playing=true;
shoot();
var distanceRed = Math.sqrt(Math.pow(tankRed.x - opponent.x, 2) + Math.pow(tankRed.y - opponent.y, 2));
var distanceBlue = Math.sqrt(Math.pow(tankBlue.x - opponent.x, 2) + Math.pow(tankBlue.y - opponent.y, 2));
var tankX
var tankY
if (distanceRed <= distanceBlue){
tankX = tankRed.x
tankY = tankRed.y
}
else{
tankX = tankBlue.x
tankY = tankBlue.y
}
var angle = Math.atan2(tankY-opponent.y, tankX-opponent.x) * 180 / Math.PI
var speed = 100
var xDirection = Math.cos(angle * Math.PI / 180.0) * speed
var yDirection = Math.sin(angle * Math.PI / 180.0) * speed
var startX = (16 * Math.cos((angle) * Math.PI / 180)) + opponent.x
var startY = (16 * Math.sin((angle) * Math.PI / 180)) + opponent.y
entityManager.createEntityFromComponentWithProperties(
bulletOpponent, {
start: Qt.point(startX, startY),
rotation : angle + 90,
velocity: Qt.point(xDirection, yDirection)
});
}
}
}
onDistanceToTargetChanged: {
if (distanceToTarget < distanceToTargetThreshold) {
opponentShooting = true
} else {
opponentShooting = false
}
}
}
MovementAnimation {
target: opponent
property: "rotation"
velocity: 300*moveToPointHelper.outputXAxis
running: true
maxPropertyValueDifference: moveToPointHelper.absoluteRotationDifference
}
Component {
id: bulletOpponent
EntityBase {
id: singleBulletOpponent
entityType: "singleBulletOpponent"
property point start
property point velocity
x: start.x
y: start.y
Image {
width: 7
height: 14
source: Qt.resolvedUrl("../../assets/img/Iceball.png")
anchors.centerIn: parent
}
BoxCollider {
id: boxCollider
width: 10
height: 10
anchors.fill: parent
collisionTestingOnlyMode: true
density: 0
friction: 0
restitution: 0
body.bullet: true
body.fixedRotation: false
fixture.onBeginContact: (other, contactNormal) => {
var collidedEntity = other.getBody().target;
var otherEntityId = collidedEntity.entityId;
var otherEntityParent = collidedEntity.parent;
if (otherEntityId.substring(0, 3) !== "lak" && otherEntityId.substring(0, 3) !== "pow" && otherEntityId.substring(0, 3) !== "opp") {
singleBulletOpponent.destroy();
entityManager.createEntityFromUrlWithProperties(
Qt.resolvedUrl("Splat.qml"), {
"z": 1,
"x": singleBulletOpponent.x,
"y": singleBulletOpponent.y,
"rotation": singleBulletOpponent.rotation
}
);
if (otherEntityId.substring(0, 4) === "tank") {
otherEntityParent.onDamage();
}
}
}
}
MovementAnimation {
target: singleBulletOpponent
property: "x"
velocity: singleBulletOpponent.velocity.x
running: true
}
MovementAnimation {
target: singleBulletOpponent
property: "y"
velocity: singleBulletOpponent.velocity.y
running: true
onStopped: {
singleBulletOpponent.destroy()
}
}
}
}
function calcAngle(touchX, touchY) {