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: allow cows to be placed on a non-road square #1746

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
113 changes: 54 additions & 59 deletions game/static/game/js/level_editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -1061,7 +1061,7 @@ ocargo.LevelEditor = function(levelId) {
} else {
closeTrashcan();
}
}
}

function openTrashcan() {
$('#trashcanLidOpen').css('display', 'block');
Expand Down Expand Up @@ -1226,8 +1226,8 @@ ocargo.LevelEditor = function(levelId) {
if (cows) {
for (var i = 0; i < cows.length; i++) {
var internalCow = cows[i];
if (internalCow.controlledNode) {
mark(internalCow.controlledNode.coordinate, internalCow.data.group.color, 0.3, true);
if (internalCow.coordinate) {
mark(internalCow.coordinate, internalCow.data.group.color, 0.3, true);
}
}
}
Expand Down Expand Up @@ -1683,7 +1683,7 @@ ocargo.LevelEditor = function(levelId) {
var bBox = image.getBBox();
imageWidth = bBox.width;
imageHeight = bBox.height;

var paperPosition = paper.position();
originX = x - paperPosition.left + paper.scrollLeft() - imageWidth/2;
originY = y - paperPosition.top + paper.scrollTop() - imageHeight/2;
Expand All @@ -1699,46 +1699,44 @@ ocargo.LevelEditor = function(levelId) {

if (trashcanOpen) {
cow.destroy();
} else if (isValidDraggedCowPlacement(controlledCoord, cow)) {
// Add back to the list of cows if on valid nodes
var controlledNode = ocargo.Node.findNodeByCoordinate(controlledCoord, nodes);
cow.controlledNode = controlledNode;
cow.valid = true;
drawing.setCowImagePosition(controlledCoord, image, controlledNode);
closeTrashcan();
} else {
cow.controlledNode = null;
cow.valid = false;

var cowX = paperX;
var cowY = paperY;

if (paperWidth < paperX + imageWidth) {
cowX = paperWidth - imageWidth
cow.coordinate = controlledCoord;
cow.valid = isValidDraggedCowPlacement(controlledCoord, cow);
if (cow.isOnRoad()) {
const controlledNode = ocargo.Node.findNodeByCoordinate(controlledCoord, nodes);
drawing.setCowImagePosition(controlledCoord, image, controlledNode);
}

if (paperHeight < paperY + imageHeight) {
cowY = paperHeight - imageHeight
else {
var cowX = paperX;
var cowY = paperY;

if (paperWidth < paperX + imageWidth) {
cowX = paperWidth - imageWidth
}

if (paperHeight < paperY + imageHeight) {
cowY = paperHeight - imageHeight
}

image.transform('t' + cowX + ',' + cowY);
}

image.transform('t' + cowX + ',' + cowY);
}
adjustCowGroupMinMaxFields(cow);

adjustCowGroupMinMaxFields(cow);
image.attr({'cursor':'pointer'});
closeTrashcan();
}

image.drag(onDragMove, onDragStart, onDragEnd);
addReleaseListeners(image.node);
}

function isValidDraggedCowPlacement(controlledCoord, cow){
var controlledNode = ocargo.Node.findNodeByCoordinate(controlledCoord, nodes);
if (!controlledNode || isOriginCoordinate(controlledCoord) || isHouseCoordinate(controlledCoord))
if (isOriginCoordinate(controlledCoord) || isHouseCoordinate(controlledCoord))
return false;
for (var i=0; i < cows.length; i++) {
var otherCow = cows[i];
if (otherCow.controlledNode == controlledNode && (cow === "undefined" || cow != otherCow))
if (cow != otherCow && otherCow.coordinate && otherCow.coordinate.equals(controlledCoord))
return false;
}
return true;
Expand All @@ -1764,14 +1762,6 @@ ocargo.LevelEditor = function(levelId) {
if (controlledCoord) {
markAsBackground(controlledCoord);
}
if (cows) {
for( let i = 0; i < cows.length; i++){
let internalCow = cows[i];
if(internalCow !== cow && internalCow.controlledNode) {
mark(internalCow.controlledNode.coordinate, internalCow.data.group.color, 0.3, true);
}
}
}
if (originNode) {
markAsOrigin(originNode.coordinate);
}
Expand All @@ -1783,8 +1773,8 @@ ocargo.LevelEditor = function(levelId) {
}

function setCowMarkingsOnMouseUp(controlledCoord, cow) {
if (cow.controlledNode) {
markAsBackground(cow.controlledNode.coordinate);
if (cow.isOnRoad()) {
markAsBackground(cow.coordinate);
}
if (controlledCoord) {
mark(controlledCoord, cow.data.group.color, 0.3, true);
Expand Down Expand Up @@ -1858,20 +1848,18 @@ ocargo.LevelEditor = function(levelId) {
function handleDraggableCowMouseUp(e){
let internalCow = new InternalCow({group: cowGroups["group1"]});
let image = internalCow.image;
internalCow.coordinate = controlledCoord;
internalCow.valid = isValidDraggedCowPlacement(controlledCoord, internalCow);

if (isValidDraggedCowPlacement(controlledCoord)) {
internalCow.controlledNode = ocargo.Node.findNodeByCoordinate(controlledCoord, nodes);
internalCow.valid = true;
drawing.setCowImagePosition(controlledCoord, image, internalCow.controlledNode);
if (internalCow.isOnRoad()) {
const controlledNode = ocargo.Node.findNodeByCoordinate(controlledCoord, nodes);
drawing.setCowImagePosition(controlledCoord, image, controlledNode);
} else {
internalCow.controlledNode = null;
internalCow.valid = false;

const cowX = e.pageX + paper.scrollLeft() - TAB_PANE_WIDTH - dragged_cow.width / 2;
const cowY = e.pageY + paper.scrollTop() - dragged_cow.height / 2;

if (draggedObjectOnGrid(e, dragged_cow)) {
image.transform('t' + cowX + ',' + cowY + 'r90');
image.transform('t' + cowX + ',' + cowY);
} else {
internalCow.destroy();
}
Expand Down Expand Up @@ -2488,7 +2476,7 @@ ocargo.LevelEditor = function(levelId) {
type: cowGroups[groupId].type}; //editor can only add white cow for now
}

var coordinates = cows[i].controlledNode.coordinate;
var coordinates = cows[i].coordinate;
var strCoordinates = {'x':coordinates.x, 'y':coordinates.y};
cowGroupData[groupId].potentialCoordinates.push(strCoordinates);
}
Expand Down Expand Up @@ -2920,9 +2908,8 @@ ocargo.LevelEditor = function(levelId) {
if (!this.valid) {
throw "Error: cannot create actual cow from invalid internal cow!";
}

// Where the cow is placed.
var coordinates = this.controlledNode.coordinate;
var coordinates = this.coordinate;
var strCoordinates= {'x':coordinates.x, 'y':coordinates.y};

return { "coordinates": [strCoordinates],
Expand All @@ -2944,6 +2931,10 @@ ocargo.LevelEditor = function(levelId) {

};

this.isOnRoad = function() {
return this.coordinate && ocargo.Node.findNodeByCoordinate(this.coordinate, nodes);
}

this.updateTheme = function() {
let newType = currentTheme == THEMES.city ? ocargo.Cow.PIGEON : ocargo.Cow.WHITE;
let transformDimensions = this["image"]["_"]["transform"][0]
Expand All @@ -2958,10 +2949,9 @@ ocargo.LevelEditor = function(levelId) {
this.image.remove();

this.image = drawing.createCowImage(newType);
if (this.controlledNode !== null) {
let controlledNode = this.controlledNode;
let coordinates = controlledNode.coordinate;
drawing.setCowImagePosition(coordinates, this.image, controlledNode);
if (this.isOnRoad()) {
let controlledNode = ocargo.Node.findNodeByCoordinate(coordinates, nodes);
drawing.setCowImagePosition(this.coordinate, this.image, controlledNode);
} else {
this.image.transform("t" + x + "," + y + " r" + r);
}
Expand All @@ -2972,14 +2962,19 @@ ocargo.LevelEditor = function(levelId) {
this.image = drawing.createCowImage(data.group.type);
this.valid = false;


if ( data.coordinates && data.coordinates.length > 0 ) {
var coordinates = new ocargo.Coordinate(data.coordinates[0].x, data.coordinates[0].y);
this.controlledNode = ocargo.Node.findNodeByCoordinate(coordinates, nodes);
this.coordinate = new ocargo.Coordinate(data.coordinates[0].x, data.coordinates[0].y);
this.valid = isValidDraggedCowPlacement(this.coordinate, this);

if (this.controlledNode) {
this.valid = true;
drawing.setCowImagePosition(coordinates, this.image, this.controlledNode);
if (this.isOnRoad()) {
const controlledNode = ocargo.Node.findNodeByCoordinate(this.coordinate, nodes);
drawing.setCowImagePosition(this.coordinate, this.image, controlledNode);
} else {
const box = this.image.getBBox();
// calculate position of the image
const paperX = (this.coordinate.x + 1) * GRID_SPACE_SIZE - box.width/2;
const paperY = (GRID_HEIGHT - this.coordinate.y) * GRID_SPACE_SIZE - box.height/2;
this.image.transform('t' + paperX + ',' + paperY );
}
} else {
this.image.transform('...t' + (-paper.scrollLeft()) + ',' + paper.scrollTop());
Expand Down