Skip to content

Commit

Permalink
Mechanics: Enemies are now able to attack gardens.
Browse files Browse the repository at this point in the history
  • Loading branch information
MisterGC committed Apr 19, 2020
1 parent e404bf8 commit 3900602
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 50 deletions.
26 changes: 24 additions & 2 deletions src/Enemy.qml
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,20 @@ GameEntity
bullet: true
sensor: true
categories: collCat.enemy
collidesWith: collCat.player | collCat.waypoint
collidesWith: collCat.player | collCat.waypoint | collCat.garden
property var wpPath: []
property int _wpIndex: -1
property real maxVelo: 10

debug: true
text: theMunchTimer.running ? "Munch Time" : "Hunnngry"
signal attack(var damage)

Component.onCompleted: {
for (let i=0; i<fixtures.length; ++i) {
let f = fixtures[i];
f.beginContact.connect(_onCollision);
f.endContact.connect(_onEndContact);
}
_goForNextWp();
}
Expand All @@ -33,7 +36,9 @@ GameEntity
if (_wpIndex + 1 < wpPath.length) {
_wpIndex ++;
let dest = wpPath[_wpIndex];
let v = Qt.vector2d(dest.x - x, dest.y - y);
let dX = dest.x + dest.width * .5 - (x + width * .5);
let dY = dest.y + dest.height * .5 - (y + height * .5);
let v = Qt.vector2d(dX, dY);
let l = v.length();
if (l > 1) {
v = v.times(maxVelo/l);
Expand All @@ -56,5 +61,22 @@ GameEntity
else if (gameWorld.isInstanceOf(e, "Waypoint")) {
_goForNextWp();
}
else if (gameWorld.isInstanceOf(e, "Garden")) {
theMunchTimer.start();
}
}

function _onEndContact() {
var e = fixture.getBody().target;
if (gameWorld.isInstanceOf(e, "Garden")) {
theMunchTimer.stop();
}
}

Timer {
id: theMunchTimer
interval: 1000
onTriggered: attack(2);
repeat: true
}
}
2 changes: 1 addition & 1 deletion src/EnemyMaster.qml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ Item {
Component { id: theWayPointComp; Waypoint {}}
Timer {
id: spawnTimer
interval: 3000
interval: 10000
repeat: true
onTriggered: {
let enemy = theSpawner.createObject(gameWorld.coordSys,
Expand Down
10 changes: 8 additions & 2 deletions src/Garden.qml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ GameEntity
// Energy of the garden, if it is 0, the garden dead
property int energy: maxEnergy
// Protection decreases dealt damage
property int protection: 0
property real protection: 0

text: energy + "/" + protection

Expand All @@ -41,17 +41,23 @@ GameEntity
protection = fixture.protection;
}
}
else if (theWorld.isInstanceOf(e, "Enemy")) {
e.attack.connect(_onAttack);
}
}

function _onEndContact(fixture) {
var e = fixture.getBody().target;
if (theWorld.isInstanceOf(e, "Player")) {
if (fixture.hasOwnProperty("protection")) protection = 0;
}
else if (theWorld.isInstanceOf(e, "Enemy")) {
e.attack.disconnect(_onAttack);
}
}

function _onAttack(damage) {
let d = damage > protection ? damage - protection : 0;
let d = damage * ((protection > 0) ? protection : 1)
energy -= d;
}
}
2 changes: 1 addition & 1 deletion src/Player.qml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ GameEntity
sensor: true
categories: collCat.magicProtection
collidesWith: collCat.garden
property int protection: thePlayer.isProtecting ? 2 : 0
property real protection: thePlayer.isProtecting ? .5 : 0
}
}

Expand Down
67 changes: 23 additions & 44 deletions src/map.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 3900602

Please sign in to comment.