Skip to content

Commit

Permalink
Merge pull request #3 from MisterGC/feature/score-times-and-end
Browse files Browse the repository at this point in the history
Feature/score times and end
  • Loading branch information
MisterGC authored Apr 19, 2020
2 parents c5788c9 + 3ba2685 commit 3d1a680
Show file tree
Hide file tree
Showing 14 changed files with 215 additions and 27 deletions.
2 changes: 1 addition & 1 deletion dep/clayground
Binary file added devlog/m2_weather_and_protect.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added devlog/m3_from_start_to_end.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 0 additions & 4 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ find_package(Qt5 COMPONENTS Core Quick REQUIRED)

add_executable (${PROJECT_NAME}
main.cpp
main.qml
Sandbox.qml
Player.qml
Wall.qml
res.qrc
sound/sound.qrc
visual/visual.qrc
Expand Down
2 changes: 1 addition & 1 deletion src/Enemy.qml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ GameEntity
var e = fixture.getBody().target;
if (theWorld.isInstanceOf(e, "Player")) {
if (e.isDodging) theEnemy.destroy();
else text = "Got ya!";
else e.energy -= 1;
}
}

Expand Down
35 changes: 35 additions & 0 deletions src/GameEnding.qml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// (c) [email protected] - zlib license, see "LICENSE" file
import QtQuick 2.12

Rectangle {
id: theGameEnding

property Referee referee: null

visible: false
anchors.fill: parent
Component.onCompleted: {
referee.playerDied.connect(onPlayerDied);
referee.gardenDied.connect(onGardenDied);
referee.seasonEnded.connect(onSeasonEnded);
}

function onGardenDied() {showEnd("Garden died!");}
function onPlayerDied() {showEnd("Player died!");}
function onSeasonEnded() {showEnd("Season ended!");}

function showEnd(txt) {
if (!visible) {
theEndTxt.text = txt;
visible = true;
}
}

Text {
id: theEndTxt
anchors.centerIn: parent
font.pixelSize: parent.height * .1
text: "The Game ends ..."
}
}

3 changes: 2 additions & 1 deletion src/Garden.qml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ GameEntity
collCat.magicProtection
debug: true
color: protection > 0 ? "lightgreen" : "green"
property int maxEnergy: widthWu * heightWu
// Energy of the garden, if it is 0, the garden dead
property int energy: 100
property int energy: maxEnergy
// Protection decreases dealt damage
property int protection: 0

Expand Down
6 changes: 5 additions & 1 deletion src/Player.qml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ GameEntity
id: thePlayer
bodyType: Body.Dynamic
bullet: true

property int energy: 3

property real moveSpeed: 25
property real dodgeSpeed: 0
property real _desiredVeloX: 0
Expand All @@ -23,7 +26,8 @@ GameEntity
onIsProtectingChanged: { awake = false; awake = true; }

debug: true
onIsDodgingChanged: text = isDodging ? "~==>" : ""
text: energy > 0 ? "".repeat(energy) : ""

categories: collCat.player
collidesWith: collCat.enemy | collCat.staticGeo | collCat.garden

Expand Down
69 changes: 69 additions & 0 deletions src/Referee.qml
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// (c) [email protected] - zlib license, see "LICENSE" file
import QtQuick 2.12

Item {
property var gardens: []
property var player: undefined
property real gardenPercentage: 100.0
property int maxEnergyGarden: 0
property int timeElapsed: 0

readonly property int seasonLength: 300
readonly property real seasonPercentage: Math.round(timeElapsed * 100.0 / seasonLength)
onSeasonPercentageChanged: {
if (seasonPercentage >= 100) {
theOneSecTimer.stop();
seasonEnded();
}
}

onPlayerChanged: {
if (player) {
player.energyChanged.connect(_onPlayerEnergyChanged);
theOneSecTimer.start();
}
else {
theOneSecTimer.stop();
gardens=[];
gardenPercentage=100.0;
maxEnergyGarden=0;
timeElapsed=0;
}
}


signal playerDied()
signal gardenDied()
signal seasonEnded()

Timer {
id: theOneSecTimer
interval: 1000
Component.onCompleted: start()
onTriggered: timeElapsed++
repeat: true
}

onGardenPercentageChanged: {
if (gardenPercentage <= 0) gardenDied()
}

function addGarden(garden) {
gardens.push(garden);
garden.energyChanged.connect(_onGardenEnergyChanged);
maxEnergyGarden += garden.maxEnergy;
}

function _onGardenEnergyChanged() {
let energy = 0;
for (const g of gardens)
energy += g.energy;
gardenPercentage = Math.round((energy/maxEnergyGarden) * 100.0);
}

function _onPlayerEnergyChanged() {
if (player.energy <= 0)
playerDied();
}

}
65 changes: 62 additions & 3 deletions src/Sandbox.qml
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ import Clayground.World 1.0
ClayWorld {
id: theWorld

map: "map.svg"
map: ""
pixelPerUnit: height/70
gravity: Qt.point(0,0)
timeStep: 1/60.0

property var player: null
//physicsDebugging: true

QtObject {
id: collCat
Expand All @@ -27,7 +28,10 @@ ClayWorld {
readonly property int noCollision: Box.None
}

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

Weather { }
Weather {id: theWeather }
Referee {
id: theReferee
anchors.horizontalCenter: parent.horizontalCenter
anchors.top: parent.top
anchors.topMargin: 0.04 * parent.height
player: theWorld.player
Text {
id: refereeSays
anchors.centerIn: parent
text: Math.round(parent.gardenPercentage)
+ (parent.player ? ("/" + parent.player.energy) : "")
+ "/" + parent.seasonPercentage
}
}

Keys.forwardTo: theGameCtrl
GameController {
id: theGameCtrl
anchors.fill: parent

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

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

onAxisXChanged: {
if (!player) 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 (player.isProtecting) return;
if (axisY > 0) player.moveUp();
else if (axisY < 0) player.moveDown();
else { player.stopUp(); player.stopDown();}
}
//showDebugOverlay: true

}

Expand All @@ -84,6 +108,9 @@ ClayWorld {
else if (isInstanceOf(obj, "Enemy")) {
obj.source = theWorld.resource("visual/enemy.png");
}
else if (isInstanceOf(obj, "Garden")) {
theReferee.addGarden(obj);
}
}

SoundEffect {
Expand All @@ -93,4 +120,36 @@ ClayWorld {
source: theWorld.resource("sound/bgmusic.wav")
loops: SoundEffect.Infinite
}

StartScreen {
visible: true
Component.onCompleted: {
theGameCtrl.buttonAPressedChanged.connect(startOnDemand)
theGameCtrl.buttonBPressedChanged.connect(startOnDemand)
}
function startOnDemand() {
if (visible) {
map = "";
map = "map.svg";
visible = false;
}
}
}

GameEnding {
referee: theReferee
Component.onCompleted: {
theGameCtrl.buttonAPressedChanged.connect(restartOnDemand)
theGameCtrl.buttonBPressedChanged.connect(restartOnDemand)
}

function restartOnDemand() {
if (visible) {
map = "";
map = "map.svg";
visible = false;
}
}
}

}
20 changes: 20 additions & 0 deletions src/StartScreen.qml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// (c) [email protected] - zlib license, see "LICENSE" file
import QtQuick 2.12

Rectangle {
id: theStartScreen

property Referee referee: null
signal requestStart()

visible: false
anchors.fill: parent

Text {
anchors.centerIn: parent
font.pixelSize: parent.height * .07
text: "Welcome to the Game"
}
}


9 changes: 0 additions & 9 deletions src/Storm.qml
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,5 @@ GameEntity
repeat: true
}

Component.onCompleted: {
for (let i=0; i<fixtures.length; ++i) {
let f = fixtures[i];
f.beginContact.connect(_onCollision);
}
}

function _onCollision(fixture) {
}
}

24 changes: 17 additions & 7 deletions src/Weather.qml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,21 @@
import QtQuick 2.12

Item {
Component.onCompleted: console.log("Test")
id: theWeather

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

Timer {
Component.onCompleted: start()
interval: 10000
id: theTimer
interval: 2000
onTriggered: {
let x = Math.random() * theWorld.worldXMax
let y = Math.random() * theWorld.worldYMax
Expand All @@ -19,11 +30,10 @@ Item {
widthWu: 50,
heightWu: 50
});
theWeather._destroyStorms.connect(storm.destroy);
}
repeat: true
}
Component {
id: theStorm
Storm {}
}

Component { id: theStorm; Storm {} }
}
3 changes: 3 additions & 0 deletions src/res.qrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@
<qresource prefix="/">
<file>main.qml</file>
<file>Player.qml</file>
<file>GameEnding.qml</file>
<file>GameEntity.qml</file>
<file>Garden.qml</file>
<file>Enemy.qml</file>
<file>Referee.qml</file>
<file>Sandbox.qml</file>
<file>StartScreen.qml</file>
<file>Storm.qml</file>
<file>Weather.qml</file>
<file>Wall.qml</file>
Expand Down

0 comments on commit 3d1a680

Please sign in to comment.