Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Cows are always visible and horn horns on the right square #1545

Merged
merged 2 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 19 additions & 7 deletions game/static/game/js/animation.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand All @@ -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];
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
22 changes: 0 additions & 22 deletions game/static/game/js/cow.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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
Expand Down
55 changes: 11 additions & 44 deletions game/static/game/js/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
};

Expand Down