diff --git a/NextStation-Paris/game.js b/NextStation-Paris/game.js index 86e4402..9866e59 100644 --- a/NextStation-Paris/game.js +++ b/NextStation-Paris/game.js @@ -43,6 +43,24 @@ const COLORS = { GREEN: "green", }; +const getStation = (stations, x, y) => { + return stations.find( + (station) => station.position.x == x && station.position.y == y + ); +}; + +const findSection = (sections, stationFrom, stationTo) => { + for (const section of sections) { + if ( + section.stations.includes(stationFrom) && + section.stations.includes(stationTo) + ) { + return section; + } + } + return null; +}; + function buildMap() { const stations = []; // line 1 @@ -121,18 +139,12 @@ function buildMap() { stations.push(new Station(SHAPES.JOKER, 13, { x: 5, y: 6 })); stations.push(new Station(SHAPES.JOKER, 13, { x: 6, y: 6 })); - const getStation = (x, y) => { - return stations.find( - (station) => station.position.x == x && station.position.y == y - ); - }; - // create also sections ? const sections = []; const addSection = (x1, y1, x2, y2) => { - const stationFrom = getStation(x1, y1); - const stationTo = getStation(x2, y2); + const stationFrom = getStation(stations, x1, y1); + const stationTo = getStation(stations, x2, y2); if (!stationFrom || !stationTo) { throw "error in add Section" + x1 + "," + y1 + ":" + x2 + "," + y2; } @@ -211,7 +223,26 @@ function buildMap() { // TODO: compute 'crossing' sections - return { stations: stations, sections: sections }; + const overheads = []; + const addOverHead = (x1, y1, x2, y2, x3, y3, x4, y4) => { + const stationFrom1 = getStation(stations, x1, y1); + const stationTo1 = getStation(stations, x2, y2); + const stationFrom2 = getStation(stations, x3, y3); + const stationTo2 = getStation(stations, x4, y4); + const section1 = findSection(sections, stationFrom1, stationTo1); + const section2 = findSection(sections, stationFrom2, stationTo2); + overheads.push(new Overhead(section1, section2)); + }; + addOverHead(4, 1, 4, 3, 2, 2, 5, 2); + addOverHead(2, 4, 4, 6, 3, 4, 1, 6); + addOverHead(3, 7, 5, 9, 3, 8, 5, 6); + addOverHead(3, 8, 3, 10, 2, 9, 4, 9); + addOverHead(6, 8, 8, 10, 7, 10, 9, 8); + addOverHead(6, 5, 9, 8, 8, 6, 8, 8); + addOverHead(9, 3, 9, 8, 8, 4, 10, 4); + addOverHead(8, 1, 6, 3, 7, 1, 9, 3); + + return { stations: stations, sections: sections, overheads: overheads }; } class Section { @@ -251,11 +282,12 @@ const CARDS = { }; class Card { - constructor(cardIndex, color, symbol) { + constructor(cardIndex, color, symbol, monument = false) { this.index = cardIndex; this.color = color; this.symbol = symbol; this.switch = color === "switch"; + this.monument = monument; } } @@ -265,13 +297,13 @@ function getCards() { cards.push(new Card(2, CARDS.UNDERGROUND, SHAPES.TRIANGLE)); cards.push(new Card(3, CARDS.UNDERGROUND, SHAPES.PENTAGONE)); cards.push(new Card(4, CARDS.UNDERGROUND, SHAPES.SQUARE)); - cards.push(new Card(5, CARDS.UNDERGROUND, SHAPES.JOKER)); + cards.push(new Card(5, CARDS.UNDERGROUND, SHAPES.JOKER, true)); cards.push(new Card(6, CARDS.SWITCH, SHAPES.JOKER)); // SWITCH cards.push(new Card(7, CARDS.SURFACE, SHAPES.PENTAGONE)); cards.push(new Card(8, CARDS.SURFACE, SHAPES.CIRCLE)); cards.push(new Card(9, CARDS.SURFACE, SHAPES.TRIANGLE)); cards.push(new Card(10, CARDS.SURFACE, SHAPES.SQUARE)); - cards.push(new Card(11, CARDS.SURFACE, SHAPES.JOKER)); + cards.push(new Card(11, CARDS.SURFACE, SHAPES.JOKER, true)); return cards; } diff --git a/NextStation-Paris/main.js b/NextStation-Paris/main.js index cc057ad..703916c 100644 --- a/NextStation-Paris/main.js +++ b/NextStation-Paris/main.js @@ -25,6 +25,8 @@ let lastTime = 0; let randomizer = null; let useSwitch = false; +let withMonument = false; +let withMonumentBorder = false; let stationLine = []; const allStationLines = []; @@ -84,20 +86,6 @@ const scoreUI = { bottomRight: { x: 1017, y: 665 }, }, }, - { - districts: { - topLeft: { x: 980, y: 513 }, - bottomRight: { x: 1017, y: 550 }, - }, - stations: { - topLeft: { x: 980, y: 570 }, - bottomRight: { x: 1017, y: 607 }, - }, - monuments: { - topLeft: { x: 980, y: 625 }, - bottomRight: { x: 1017, y: 665 }, - }, - }, { districts: { topLeft: { x: 1036, y: 513 }, @@ -126,9 +114,7 @@ const scoreUI = { function preload() {} -function musicClicked() { - // TODO -} +function musicClicked() {} const speakerStorageKey = "DrDr3ck/GameEngine/Speaker"; function speakerClicked() { @@ -152,8 +138,8 @@ function startClicked() { } const speakerButton = new BFloatingSwitchButton( - windowWidth - 70 - 10 - 70, - 70, + windowWidth - 70, + 70 + 10 + 70, "\uD83D\uDD0A", speakerClicked ); @@ -192,6 +178,7 @@ function setup() { spritesheet.addSpriteSheet("paris", "./paris.png", 770, 765); spritesheet.addSpriteSheet("score", "./score.png", 615, 315); spritesheet.addSpriteSheet("cards", "./cards.png", 325, 210); + spritesheet.addSpriteSheet("mini", "./mini.png", 81, 81); spritesheet.addSpriteSheet("skip", "./skip.png", 100, 100); spritesheet.addSpriteSheet("switch", "./switch.png", 131, 131); spritesheet.addSpriteSheet("crayons", "./crayons.png", 93, 93); @@ -211,12 +198,6 @@ function getY(y) { return (y - 1) * ((747 - 53) / 9) + 50; } -const getStation = (stations, x, y) => { - return stations.find( - (station) => station.position.x == x && station.position.y == y - ); -}; - function startLine() { // which color for current line ? switch (pencils[round]) { @@ -239,6 +220,10 @@ function startLine() { nextCard(); useSwitch = true; } + // check if first card may start with a monument (remark: it will never be used) + if (cards[0].monument) { + withMonument = true; + } } // display district for beginner @@ -368,12 +353,26 @@ function displayDistrict(district) { } } +function displayOverHead(overhead) { + noFill(); + strokeWeight(7); + stroke(110, 160, 130, 128); + displaySection(overhead.sections[0]); + displaySection(overhead.sections[1]); +} + function displayOverHeads(overheadType) { - const overheads = getOverHeads(overheadType); - overheads.forEach((station) => displayStation(station, "black")); + if (!overheadType) { + return; + } + const typedOverheads = getOverHeads(overheadType); + typedOverheads.forEach((overhead) => displayOverHead(overhead)); } function displayLinks(linkType) { + if (!linkType) { + return; + } const links = getLinks(linkType); links.forEach((station) => displayStation(station, "black")); } @@ -464,8 +463,8 @@ function displayStation(station, lineColor) { strokeWeight(1); fill(0); textSize(15); - text(station.district, X, Y); - //text(`${station.position.x},${station.position.y}`, X, Y); + //text(station.district, X, Y); + text(`${station.position.x},${station.position.y}`, X, Y); } } @@ -511,6 +510,7 @@ function getRGBColor(color = null) { let stations = []; let sections = []; +let overheads = []; let cards = []; let countUnderground = 0; @@ -521,10 +521,7 @@ function displayLines() { ); } -function displayLine(color, line, lastLine) { - if (lastLine) { - line.forEach((station) => displayStation(station, color)); - } +const getSections = (line, color) => { const sections = []; line.forEach((station) => { for (const section of station.sections) { @@ -533,6 +530,14 @@ function displayLine(color, line, lastLine) { } } }); + return sections; +}; + +function displayLine(color, line, lastLine) { + if (lastLine) { + line.forEach((station) => displayStation(station, color)); + } + const sections = getSections(line, color); strokeWeight(3); const colors = getRGBColor(color); stroke(colors[0], colors[1], colors[2]); @@ -540,13 +545,48 @@ function displayLine(color, line, lastLine) { } function getOverHeads(num) { - // TODO: score for overhead - return []; + const overheadsNum = []; + overheads.forEach((overhead) => { + if (num === 1) { + if (overhead.sections[0].color && !overhead.sections[1].color) { + overheadsNum.push(overhead); + } else if (!overhead.sections[0].color && overhead.sections[1].color) { + overheadsNum.push(overhead); + } + } else { + // num === 2 + if ( + overhead.sections[0].color && + overhead.sections[1].color && + num === 2 + ) { + overheadsNum.push(overhead); + } + } + }); + return overheadsNum; +} + +function countLink(station) { + const allLines = []; + station.sections.forEach((section) => { + if (section.color && !allLines.includes(section.color)) { + allLines.push(section.color); + } + }); + return allLines.length; } function getLinks(num) { - // TODO: score for links - return []; + const links = []; + allStationLines.forEach((line) => { + line.line.forEach((station) => { + if (countLink(station) === num && !links.includes(station)) { + links.push(station); + } + }); + }); + return links; } function getCrossedDistricts(stationLine) { @@ -611,7 +651,8 @@ function drawScore() { }); // total station lines text(total, 1115, 700); - // TODO: overhead + + // overhead const overhead2 = getOverHeads(1).length; const overhead6 = getOverHeads(2).length; text(overhead2, 1056, 755); @@ -619,7 +660,8 @@ function drawScore() { text(overhead2 * 2 + overhead6 * 6, 1178, 700); total += overhead2 * 2 + overhead6 * 6; - // TODO: correspondance + + // links const link2 = getLinks(2).length; const link5 = getLinks(3).length; const link9 = getLinks(4).length; @@ -647,11 +689,24 @@ function drawLine(station) { line(X, Y, mouseX, mouseY); } -function drawCard() { +function displayCard() { + if (round === 4) { + // end of game + spritesheet.drawSprite("cards", 0, 815, 15); + return; + } spritesheet.drawSprite("cards", cards[0].index, 815, 15); if (useSwitch) { spritesheet.drawScaledSprite("switch", 0, 1080, 160, 0.8); } + + const indices = cards.map((c) => c.index); + for (let i = 1; i <= 11; i++) { + spritesheet.drawScaledSprite("mini", i, 1363, -50 + 70 * i, 0.8); + if (!indices.includes(i) && (i !== 6 || !useSwitch)) { + spritesheet.drawScaledSprite("mini", 0, 1363, -50 + 70 * i, 0.8); + } + } } function drawGame() { @@ -666,11 +721,11 @@ function drawGame() { ); drawScore(); - drawCard(); - - displayPencil(); + displayCard(); - spritesheet.drawSprite("skip", 0, 1360, 120); + if (round !== 4) { + displayPencil(); + } displayLines(); @@ -689,12 +744,15 @@ function drawGame() { } const rgb = getRGBColor(); - noFill(); - strokeWeight(10); - stroke(rgb[0], rgb[1], rgb[2]); - rect(0, 0, width, height); + if (rgb) { + noFill(); + strokeWeight(10); + stroke(rgb[0], rgb[1], rgb[2]); + rect(0, 0, width, height); + } - /* debug + /* + // debug stroke(80); strokeWeight(2); noFill(); @@ -704,13 +762,18 @@ function drawGame() { stations.forEach((element) => { displayStation(element, null); }); - // end debug*/ + overheads.forEach((element) => { + displayOverHead(element); + }); + // end debug + */ } function initGame() { const map = buildMap(); stations = map.stations; sections = map.sections; + overheads = map.overheads; cards = getCards(); @@ -774,24 +837,17 @@ function draw() { function getBorderStations(line) { const border = []; - line.forEach((station) => { - if (useSwitch || station.onBorder(pencils[round])) { - border.push(station); - } - }); - return border; -} - -function findSection(stationFrom, stationTo) { - for (const section of sections) { - if ( - section.stations.includes(stationFrom) && - section.stations.includes(stationTo) - ) { - return section; - } + if (withMonumentBorder) { + // only put last station that is the monument + border.push(line[line.length - 1]); + } else { + line.forEach((station) => { + if (useSwitch || station.onBorder(pencils[round])) { + border.push(station); + } + }); } - return null; + return border; } // check if station is clickable. @@ -810,7 +866,7 @@ function isClickable(station) { return false; } // check if a section exists between the two stations - const section = findSection(clickedStation, station); + const section = findSection(sections, clickedStation, station); if (!section) { return false; } @@ -820,10 +876,10 @@ function isClickable(station) { function nextRound() { round++; - if (round === 4) { - // TODO: end of game - } else { - cards = getCards(); + cards = getCards(); + countUnderground = 0; + if (round !== 4) { + // otherwise: end of game randomizer.shuffleArray(cards); startLine(); } @@ -831,8 +887,13 @@ function nextRound() { function nextCard() { useSwitch = false; + withMonument = false; + withMonumentBorder = false; + if (cards[0].color === CARDS.UNDERGROUND) { + countUnderground++; + } cards.shift(); - if (cards.length === 0) { + if (cards.length === 0 || countUnderground === 5) { nextRound(); return; } @@ -841,13 +902,23 @@ function nextCard() { useSwitch = true; return; } + if (cards[0].monument) { + withMonument = true; + return; + } } function addLine() { - const section = findSection(clickedStation, over.station); + const section = findSection(sections, clickedStation, over.station); section.color = pencils[round]; stationLine.push(over.station); clickedStation = null; + if (withMonument && over.station.monument) { + // keep card + withMonument = false; + withMonumentBorder = true; + return; + } nextCard(); } @@ -876,7 +947,7 @@ function mouseClicked() { } // skip turn - if (distance(mouseX, mouseY, 1410, 169) < 40) { + if (distance(mouseX, mouseY, 1246, 169) < 40) { nextCard(); } return false; @@ -925,7 +996,7 @@ function mouseMoved() { } if (mouseInSquare(scoreUI.overheads.one)) { over.overheads = 1; - } else if (mouseInSquare(scoreUI.links.two)) { + } else if (mouseInSquare(scoreUI.overheads.two)) { over.overheads = 2; } } diff --git a/NextStation-Paris/mini.png b/NextStation-Paris/mini.png new file mode 100644 index 0000000..c67ee6e Binary files /dev/null and b/NextStation-Paris/mini.png differ