From f114e70efebaa4dbf8e3bb8515541729315b5c41 Mon Sep 17 00:00:00 2001 From: faucomte97 Date: Tue, 28 Nov 2023 17:58:56 +0000 Subject: [PATCH] fix: Cows are always visible and horn horns on the right square --- game/static/game/js/animation.js | 26 +++++++++++---- game/static/game/js/cow.js | 22 ------------- game/static/game/js/model.js | 55 +++++++------------------------- 3 files changed, 30 insertions(+), 73 deletions(-) diff --git a/game/static/game/js/animation.js b/game/static/game/js/animation.js index fc2b9a1a6..425ec9ff6 100644 --- a/game/static/game/js/animation.js +++ b/game/static/game/js/animation.js @@ -25,15 +25,31 @@ ocargo.Animation = function(model, decor, drawing) { this.drawing.renderOrigin(this.model.map.startingPosition()); this.drawing.renderDestinations(this.model.map.getDestinations()); this.drawing.renderTrafficLights(this.model.trafficLights); + + this.addCows(); + this.drawing.renderCharacter(); - this._updateFuelGauge(100); + this._updateFuelGauge(100); }; ocargo.Animation.prototype.isFinished = function() { return this.finished; }; +ocargo.Animation.prototype.addCows = function() { + let cows = this.model.cows; + + for (let i = 0 ; i < cows.length ; i++){ + let cow = cows[i]; + for (let j = 0; j < cow.potentialNodes.length; j++) { + const cowImage = this.drawing.renderCow(cow.id, cow.potentialNodes[j].coordinate, cow.potentialNodes[j], 0, cow.type); + this.numberOfCowsOnMap++; + this.activeCows.push(cowImage); + } + } +} + ocargo.Animation.prototype.removeCows = function() { for (var i = 0; i < this.activeCows.length; i++) { this.drawing.removeCow(this.activeCows[i]); @@ -60,6 +76,7 @@ ocargo.Animation.prototype.resetAnimation = function() { } this.removeCows(); + this.addCows(); for(var i = 0; i < this.model.map.destinations.length; i++) { var destination = this.model.map.destinations[i]; @@ -112,7 +129,7 @@ ocargo.Animation.prototype.stepAnimation = function(callback) { var animation = timestampQueue.shift(); var delay = this.performAnimation(animation); if (this.crashed && delay != 0) { - //Special case for crashing into cow as the van travel less before crashing + // Special case for crashing into cow as the van travel less before crashing maxDelay = delay; } else { maxDelay = Math.max(maxDelay, delay); @@ -349,11 +366,6 @@ ocargo.Animation.prototype.performAnimation = function(animation) { case 'trafficlight': this.drawing.transitionTrafficLight(animation.id, animation.colour, duration/2); break; - case 'cow': - this.numberOfCowsOnMap++; - var activeCow = this.drawing.renderCow(animation.id, animation.coordinate, animation.node, duration, animation.cowType); - this.activeCows.push(activeCow); - break; case 'cow_leave': this.numberOfCowsOnMap--; var cow = this._extractCowAt(animation.coordinate); diff --git a/game/static/game/js/cow.js b/game/static/game/js/cow.js index df7a2548a..1f0b1e2f0 100644 --- a/game/static/game/js/cow.js +++ b/game/static/game/js/cow.js @@ -23,21 +23,6 @@ ocargo.Cow.prototype.reset = function() { } }; - - - - -ocargo.Cow.prototype.queueAnimation = function(model, node) { - ocargo.animation.appendAnimation({ - type: 'cow', - id: this.id, - node: node, - cowType: this.type, - coordinate: node.coordinate, - description: 'Cow' - }); -}; - ocargo.Cow.prototype.queueLeaveAnimation = function(model, node) { ocargo.animation.appendAnimation({ type: 'callable', @@ -52,13 +37,6 @@ ocargo.Cow.prototype.queueLeaveAnimation = function(model, node) { }); }; -ocargo.Cow.prototype.setActive = function(model, node) { - var jsonCoordinate = JSON.stringify(node.coordinate); //get node coordinates - this.activeNodes[jsonCoordinate] = ocargo.Cow.ACTIVE; //set cow state to active - this.triggerEvent = true; - this.queueAnimation(model, node); -}; - ocargo.Cow.prototype.setInactive = function(model, node) { var jsonCoordinate = JSON.stringify(node.coordinate); //get node coordinates this.activeNodes[jsonCoordinate] = ocargo.Cow.INACTIVE; //set cow state to inactive diff --git a/game/static/game/js/model.js b/game/static/game/js/model.js index 8652943e8..54ad15ee6 100644 --- a/game/static/game/js/model.js +++ b/game/static/game/js/model.js @@ -44,10 +44,6 @@ ocargo.Model.prototype.reset = function() { this.cows[j].reset(); } - // Display cow on origin node if exists - var node = this.map.originCurrentNode; - this.setCowsActive(node); - this.timestamp = 0; this.movementTimestamp = 0; this.reasonForTermination = null; @@ -92,18 +88,9 @@ ocargo.Model.prototype.isDeadEnd = function() { }; ocargo.Model.prototype.isCowCrossing = function(type) { - - var thisNode = this.van.getPosition().currentNode; + const currentNode = this.van.getPosition().currentNode; this.observe('cow crossing'); - var nodes = thisNode.connectedNodes; - for(var i = 0; i < nodes.length; i++){ - var node = nodes[i]; - var cow = this.getCowForNode(node, [ocargo.Cow.ACTIVE, ocargo.Cow.READY]); - if (cow) { - return true; - } - } - return false; + return this.getCowForNode(currentNode, [ocargo.Cow.ACTIVE, ocargo.Cow.READY]); }; ocargo.Model.prototype.isTrafficLightRed = function() { @@ -139,11 +126,10 @@ ocargo.Model.prototype.getPreviousCoordinate = function() { // true if it was a valid action or false otherwise ocargo.Model.prototype.moveVan = function(nextNode, action) { - //Crash? - let previousNodeCow = this.getCowForNode(this.van.getPosition().currentNode, ocargo.Cow.ACTIVE); - let collisionWithCow = previousNodeCow && nextNode !== this.van.getPosition().currentNode; + // Crash? + let currentNodeHasCow = this.getCowForNode(this.van.getPosition().currentNode, [ocargo.Cow.ACTIVE, ocargo.Cow.READY]); - if (collisionWithCow) { + if (currentNodeHasCow) { handleCrash(this, gettext('You ran into a cow! '), 'COLLISION_WITH_COW', 'collision with cow van move action: '); return false; @@ -241,9 +227,6 @@ ocargo.Model.prototype.moveVan = function(nextNode, action) { return false; } - // Display cow on node if exists - this.setCowsActive(nextNode); - this.van.move(nextNode); // Van movement animation @@ -304,17 +287,6 @@ ocargo.Model.prototype.moveVan = function(nextNode, action) { } }; -ocargo.Model.prototype.setCowsActive = function(nextNode) { - var nodes = this.getNodesAhead(nextNode); - for (var i = 0 ; i < nodes.length ; i++){ - var cow = this.getCowForNode(nodes[i], ocargo.Cow.READY); - if (cow){ - cow.setActive(this, nodes[i]); - } - } - return; -}; - ocargo.Model.prototype.makeDelivery = function(destination) { // We're at a destination node and making a delivery! destination.visited = true; @@ -417,23 +389,18 @@ ocargo.Model.prototype.deliver = function() { }; ocargo.Model.prototype.sound_horn = function() { - var currentNode = this.van.getPosition().currentNode + const currentNode = this.van.getPosition().currentNode ocargo.animation.appendAnimation({ type: 'callable', functionType: 'playSound', functionCall: ocargo.sound.sound_horn, description: 'van sound: sounding the horn' }); - - var nodes = currentNode.connectedNodes; - nodes.push(currentNode); - nodes.forEach( (node) => { - var cow = this.getCowForNode(node, [ocargo.Cow.ACTIVE, ocargo.Cow.READY]); - if (cow) { - cow.queueLeaveAnimation(this, node); - cow.setInactive(this, node); - }; - }); + let cow = this.getCowForNode(currentNode, [ocargo.Cow.ACTIVE, ocargo.Cow.READY]); + if (cow) { + cow.queueLeaveAnimation(this, currentNode); + cow.setInactive(this, currentNode); + } return true; };