Skip to content

Commit

Permalink
Merge pull request #4 from MisterGC/feature/enemies-are-alive
Browse files Browse the repository at this point in the history
Feature/enemies are alive
  • Loading branch information
MisterGC authored Apr 19, 2020
2 parents 3d1a680 + 3900602 commit 5a001b2
Show file tree
Hide file tree
Showing 11 changed files with 223 additions and 69 deletions.
54 changes: 51 additions & 3 deletions src/Enemy.qml
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,75 @@ import Clayground.ScalingCanvas 1.0
GameEntity
{
id: theEnemy

source: gameWorld.resource("visual/enemy.png");
bodyType: Body.Dynamic
bullet: true
sensor: true
categories: collCat.enemy
collidesWith: collCat.player
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();
}

function _goForNextWp() {
if (_wpIndex + 1 < wpPath.length) {
_wpIndex ++;
let dest = wpPath[_wpIndex];
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);
linearVelocity.x = v.x;
linearVelocity.y = v.y;
}
}
else {
linearVelocity.x = 0;
linearVelocity.y = 0;
}
}

function _onCollision(fixture) {
var e = fixture.getBody().target;
if (theWorld.isInstanceOf(e, "Player")) {
if (gameWorld.isInstanceOf(e, "Player")) {
if (e.isDodging) theEnemy.destroy();
else e.energy -= 1;
}
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();
}
}

property int energy: 10000
Timer {
id: theMunchTimer
interval: 1000
onTriggered: attack(2);
repeat: true
}
}
81 changes: 81 additions & 0 deletions src/EnemyMaster.qml
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// (c) [email protected] - zlib license, see "LICENSE" file
import QtQuick 2.12
import Clayground.Physics 1.0
import Clayground.ScalingCanvas 1.0
import QtQuick.Shapes 1.14
import Box2D 2.0

Item {
id: theMaster
property var path: []
property var _waypoints: []
property var gameWorld: parent
property var _spawned: []
onEnabledChanged: {
if (enabled) spawnTimer.start();
else spawnTimer.stop();
}

Component.onCompleted: {
let lenWuH = 1.0;
for (let i=1; i<path.length; ++i) {
let p = path[i];
let ent = theWayPointComp.createObject(gameWorld.coordSys,
{
world: gameWorld.physics,
pixelPerUnit: gameWorld.pixelPerUnit,
xWu: p.x - lenWuH,
yWu: p.y + lenWuH,
widthWu: 2*lenWuH,
heightWu: 2*lenWuH
});
_waypoints.push(ent);
_spawned.push(ent);
}
}

Component { id: theWayPointComp; Waypoint {}}
Timer {
id: spawnTimer
interval: 10000
repeat: true
onTriggered: {
let enemy = theSpawner.createObject(gameWorld.coordSys,
{
world: gameWorld.physics,
pixelPerUnit: gameWorld.pixelPerUnit,
xWu: path[0].x,
yWu: path[0].y,
widthWu: 5,
heightWu: 5,
wpPath: _waypoints
});
enemy.pixelPerUnit = Qt.binding( _ => {return gameWorld.pixelPerUnit;} );
enemy.active = Qt.binding( _ => {return gameWorld.running;} );
_spawned.push(enemy);
}
}

Component { id: theSpawner; Enemy {} }


ScalingPoly {
canvas: gameWorld
strokeStyle: ShapePath.DashLine
dashPattern: [ 1, 4 ]
vertices: path
strokeColor: "red"
opacity: .5
}

Component.onDestruction: {
spawnTimer.stop()
for (let i=0; i<_spawned.length; ++i) {
let ent = _spawned[i];
if (typeof ent !== 'undefined' &&
ent.hasOwnProperty("destroy"))
ent.destroy();
}
_spawned = [];
}
}
1 change: 1 addition & 0 deletions src/GameEntity.qml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ VisualizedBoxBody

property bool debug: false
property string text: ""
property var gameWorld: theWorld
Loader { sourceComponent: debug ? theDebugTxtComp : null }
Component {
id: theDebugTxtComp
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
32 changes: 22 additions & 10 deletions src/Sandbox.qml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ ClayWorld {
pixelPerUnit: height/70
gravity: Qt.point(0,0)
timeStep: 1/60.0
running: false

property var player: null
//physicsDebugging: true
Expand All @@ -25,12 +26,13 @@ ClayWorld {
readonly property int naturalForce: Box.Category4
readonly property int garden: Box.Category5
readonly property int magicProtection: Box.Category6
readonly property int waypoint: Box.Category7
readonly property int noCollision: Box.None
}

onWorldAboutToBeCreated: {
running = false;
player = null;
theWeather.running = false;
}
onWorldCreated: {
// theGameCtrl.selectKeyboard(Qt.Key_Up,
Expand All @@ -41,10 +43,10 @@ ClayWorld {
// Qt.Key_S);
theGameCtrl.selectGamepad(0, true);
theWorld.observedItem = player;
theWeather.running = true;
theWorld.running = true;
}

Weather {id: theWeather }
Weather {id: theWeather; enabled: theWorld.running }
Referee {
id: theReferee
anchors.horizontalCenter: parent.horizontalCenter
Expand All @@ -65,14 +67,16 @@ ClayWorld {
id: theGameCtrl
anchors.fill: parent

property bool inGameCtrlEnabled: theWorld.running && theWorld.player

onButtonAPressedChanged: {
if (!player) return;
if (!inGameCtrlEnabled) return;
if (player.isProtecting) return;
player.moveSpeed = buttonAPressed ? 35 : 18;
}

onButtonBPressedChanged: {
if (!player) return;
if (!inGameCtrlEnabled) return;
let p = player;
if (buttonBPressed) {
if (p.desiresToMove) p.dodgeSpeed = 75;
Expand All @@ -83,14 +87,14 @@ ClayWorld {
}

onAxisXChanged: {
if (!player) return;
if (!inGameCtrlEnabled) return;
if (player.isProtecting) return;
if (axisX > 0) player.moveRight();
else if (axisX < 0) player.moveLeft();
else { player.stopLeft(); player.stopRight();}
}
onAxisYChanged: {
if (!player) return;
if (!inGameCtrlEnabled) return;
if (player.isProtecting) return;
if (axisY > 0) player.moveUp();
else if (axisY < 0) player.moveDown();
Expand All @@ -105,14 +109,22 @@ ClayWorld {
player = obj;
player.source = theWorld.resource("visual/player.png");
}
else if (isInstanceOf(obj, "Enemy")) {
obj.source = theWorld.resource("visual/enemy.png");
}
else if (isInstanceOf(obj, "Garden")) {
theReferee.addGarden(obj);
}
}

Component {id: theEnemyMasterComp; EnemyMaster {}}
onPolylineLoaded: {
let ent = theEnemyMasterComp.createObject(theWorld,
{
path: points,
enabled: theWorld.running
});
ent.enabled = Qt.binding( _ => {return theWorld.running;} );
entities.push(ent);
}

SoundEffect {
id: bgMusic
//TODO Replace music place-holder and react. play
Expand Down
17 changes: 17 additions & 0 deletions src/Waypoint.qml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// (c) [email protected] - zlib license, see "LICENSE" file
import QtQuick 2.12
import Clayground.Physics 1.0
import Box2D 2.0

PhysicsItem {
id: thePhyItem
fixtures: [
Box {
sensor: true
width: thePhyItem.width
height: thePhyItem.height
categories: collCat.waypoint
collidesWith: collCat.enemy
}
]
}
12 changes: 6 additions & 6 deletions src/Weather.qml
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@ import QtQuick 2.12
Item {
id: theWeather

property bool running: false
signal _destroyStorms()
onRunningChanged: {
if (running) theTimer.start()
onEnabledChanged: {
if (enabled)
theTimer.start();
else {
theTimer.stop()
_destroyStorms()
theTimer.stop();
_destroyStorms();
}
}

Timer {
id: theTimer
interval: 2000
interval: 4000
onTriggered: {
let x = Math.random() * theWorld.worldXMax
let y = Math.random() * theWorld.worldYMax
Expand Down
Loading

0 comments on commit 5a001b2

Please sign in to comment.