Alle Levelarten implementiert
This commit is contained in:
@@ -17,7 +17,7 @@
|
||||
<div class = 'show-when-won height-20 center flex-center'>
|
||||
<b data-translation="won" id = "won-text"></b>
|
||||
</div>
|
||||
<div class="flex-center height-60 overflow-hidden">
|
||||
<div class="flex-center height-60">
|
||||
<div id='level'>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -61,7 +61,48 @@ export class LevelSite extends AbstractSite {
|
||||
|
||||
this.levelCounterAction.setTitle(this.levelCounter);
|
||||
this.templateContainer = new TemplateContainer(leafSegmentTemplate, parentSegmentTemplate, rowSegmentTemplate, triangleTemplate);
|
||||
this.nextLevel();
|
||||
|
||||
this.loadLastLevel();
|
||||
}
|
||||
|
||||
async loadLastLevel() {
|
||||
try {
|
||||
let currentLevelInfo = localStorage.getItem("currentLevel");
|
||||
if (currentLevelInfo !== null) {
|
||||
currentLevelInfo = JSON.parse(currentLevelInfo);
|
||||
|
||||
const db = WordRotatorDb.getInstance();
|
||||
const levelJson = await db.loadLevel(currentLevelInfo["id"]);
|
||||
if (levelJson === null) {
|
||||
return this.nextLevel();
|
||||
}
|
||||
|
||||
const level = LevelHelper.inflateLevel(levelJson, this.templateContainer);
|
||||
level.setStartRotations(currentLevelInfo["rotations"]);
|
||||
|
||||
const self = this;
|
||||
level.getWonPromise().then(() => {
|
||||
self.levelWon(level);
|
||||
});
|
||||
|
||||
level.createSegments();
|
||||
level.getRootSegment()._updateElement();
|
||||
|
||||
level.saveAsCurrentLevel();
|
||||
|
||||
let levelSegment = this.findBy("#level");
|
||||
levelSegment.removeAllChildren().appendChild(level.getRootSegment().getElement());
|
||||
let scaleHelper = new ScaleHelper();
|
||||
scaleHelper.scaleToFull(levelSegment, levelSegment.parentElement, false, false, 2, level.words[0].length * 2);
|
||||
|
||||
this.level = level;
|
||||
return;
|
||||
}
|
||||
}
|
||||
catch (e) {
|
||||
console.error(e);
|
||||
}
|
||||
return this.nextLevel();
|
||||
}
|
||||
|
||||
async nextLevel() {
|
||||
@@ -69,14 +110,13 @@ export class LevelSite extends AbstractSite {
|
||||
this._siteContent.classList.remove('won');
|
||||
|
||||
const db = WordRotatorDb.getInstance();
|
||||
// const nextLevelJson = await db.loadNextLevel([20,40,60, 100]);
|
||||
const nextLevelJson = await db.loadNextLevel([140]);
|
||||
const nextLevelJson = await db.loadNextLevel([20, 40, 60, 100, 120, 140, 160]);
|
||||
// const nextLevelJson = await db.loadNextLevel([120]);
|
||||
if (nextLevelJson === null) {
|
||||
this.startSite(EndSite);
|
||||
return;
|
||||
}
|
||||
const level = LevelHelper.inflateLevel(nextLevelJson, this.templateContainer);
|
||||
|
||||
const self = this;
|
||||
level.getWonPromise().then(() => {
|
||||
self.levelWon(level);
|
||||
@@ -85,6 +125,8 @@ export class LevelSite extends AbstractSite {
|
||||
level.createSegments();
|
||||
level.getRootSegment()._updateElement();
|
||||
|
||||
level.saveAsCurrentLevel();
|
||||
|
||||
let levelSegment = this.findBy("#level");
|
||||
levelSegment.removeAllChildren().appendChild(level.getRootSegment().getElement());
|
||||
let scaleHelper = new ScaleHelper();
|
||||
@@ -108,6 +150,7 @@ export class LevelSite extends AbstractSite {
|
||||
this.levelCounterAction.redraw();
|
||||
|
||||
this._siteContent.classList.add('won');
|
||||
localStorage.removeItem("currentLevel");
|
||||
await savePromise;
|
||||
}
|
||||
catch (e) {
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
import {Level} from "./Level";
|
||||
import {RowSegment} from "../Segment/RowSegment";
|
||||
import {TriangleSegment} from "../Segment/TriangleSegment";
|
||||
import {ParentSegment} from "../Segment/ParentSegment";
|
||||
|
||||
export class FourWordsLevel extends Level {
|
||||
|
||||
constructor(templateContainer, wordLength) {
|
||||
super(templateContainer);
|
||||
this.wordLength = wordLength;
|
||||
}
|
||||
|
||||
createSegments() {
|
||||
if (this.words.length >= 4 &&
|
||||
this.words[0].length >= this.wordLength &&
|
||||
this.words[1].length >= this.wordLength &&
|
||||
this.words[2].length >= this.wordLength &&
|
||||
this.words[3].length >= this.wordLength
|
||||
) {
|
||||
let leafsWords = [];
|
||||
leafsWords[0] = Level._createLeafsForWord(this.words[0], this.templateContainer.copyLeafTemplate());
|
||||
leafsWords[1] = Level._createLeafsForWord(this.words[1], this.templateContainer.copyLeafTemplate());
|
||||
leafsWords[2] = Level._createLeafsForWord(this.words[2], this.templateContainer.copyLeafTemplate());
|
||||
leafsWords[3] = Level._createLeafsForWord(this.words[3], this.templateContainer.copyLeafTemplate());
|
||||
|
||||
let rootSegment = new RowSegment(this.templateContainer.copyRowTemplate());
|
||||
for (let i = 0, n = this.wordLength / 4; i < n; i++) {
|
||||
|
||||
let parents = [];
|
||||
parents[0] = new ParentSegment(this.templateContainer.copyParentTemplate());
|
||||
parents[1] = new ParentSegment(this.templateContainer.copyParentTemplate());
|
||||
parents[2] = new ParentSegment(this.templateContainer.copyParentTemplate());
|
||||
parents[3] = new ParentSegment(this.templateContainer.copyParentTemplate());
|
||||
|
||||
parents[0].addChild(leafsWords[0][4 * i]);
|
||||
parents[0].addChild(leafsWords[0][4 * i + 1]);
|
||||
parents[0].addChild(leafsWords[1][4 * i]);
|
||||
parents[0].addChild(leafsWords[1][4 * i + 1]);
|
||||
|
||||
parents[1].addChild(leafsWords[0][4 * i + 2]);
|
||||
parents[1].addChild(leafsWords[0][4 * i + 3]);
|
||||
parents[1].addChild(leafsWords[1][4 * i + 2]);
|
||||
parents[1].addChild(leafsWords[1][4 * i + 3]);
|
||||
|
||||
parents[2].addChild(leafsWords[2][4 * i]);
|
||||
parents[2].addChild(leafsWords[2][4 * i + 1]);
|
||||
parents[2].addChild(leafsWords[3][4 * i]);
|
||||
parents[2].addChild(leafsWords[3][4 * i + 1]);
|
||||
|
||||
parents[3].addChild(leafsWords[2][4 * i + 2]);
|
||||
parents[3].addChild(leafsWords[2][4 * i + 3]);
|
||||
parents[3].addChild(leafsWords[3][4 * i + 2]);
|
||||
parents[3].addChild(leafsWords[3][4 * i + 3]);
|
||||
|
||||
let parent = new ParentSegment(this.templateContainer.copyParentTemplate());
|
||||
parent.addChild(parents[0]);
|
||||
parent.addChild(parents[1]);
|
||||
parent.addChild(parents[2]);
|
||||
parent.addChild(parents[3]);
|
||||
|
||||
rootSegment.addChild(parent);
|
||||
}
|
||||
this.setRootSegment(rootSegment)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
import {FourWordsLevel} from "./FourWordsLevel";
|
||||
|
||||
export class FourWordsLevel12 extends FourWordsLevel{
|
||||
constructor(templateContainer) {
|
||||
super(templateContainer, 12);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
import {FourWordsLevel} from "./FourWordsLevel";
|
||||
|
||||
export class FourWordsLevel8 extends FourWordsLevel{
|
||||
constructor(templateContainer) {
|
||||
super(templateContainer, 8);
|
||||
}
|
||||
}
|
||||
@@ -21,6 +21,19 @@ export class Level {
|
||||
});
|
||||
}
|
||||
|
||||
saveAsCurrentLevel(){
|
||||
let rotations = this.getCurrentRotations();
|
||||
localStorage.setItem("currentLevel", JSON.stringify({"id": this.id, "rotations": rotations}));
|
||||
}
|
||||
|
||||
getCurrentRotations(){
|
||||
if (this.rootSegment !== null)
|
||||
{
|
||||
return this.rootSegment.getCurrentRotations([]);
|
||||
}
|
||||
return [];
|
||||
}
|
||||
|
||||
setId(id)
|
||||
{
|
||||
this.id = id;
|
||||
@@ -82,6 +95,7 @@ export class Level {
|
||||
});
|
||||
return true;
|
||||
}
|
||||
this.saveAsCurrentLevel();
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@@ -3,6 +3,8 @@ import {RowLevel8} from "./RowLevel8";
|
||||
import {RowLevel10} from "./RowLevel10";
|
||||
import {SixWordsRowLevel8} from "./SixWordsRowLevel8";
|
||||
import {SixWordsRowLevel12} from "./SixWordsRowLevel12";
|
||||
import {FourWordsLevel8} from "./FourWordsLevel8";
|
||||
import {FourWordsLevel12} from "./FourWordsLevel12";
|
||||
|
||||
export class LevelHelper {
|
||||
static setLevelType(typeId, level) {
|
||||
@@ -34,5 +36,7 @@ LevelHelper.types = {
|
||||
40: RowLevel8,
|
||||
60: RowLevel10,
|
||||
100: SixWordsRowLevel8,
|
||||
120: FourWordsLevel8,
|
||||
140: SixWordsRowLevel12,
|
||||
160: FourWordsLevel12,
|
||||
};
|
||||
@@ -13,14 +13,18 @@ export class ParentSegment extends Segment {
|
||||
this.rotation += 90;
|
||||
this.rotation %= 360;
|
||||
|
||||
let currentRotation = this.rotation;
|
||||
|
||||
this._updateRotationClass();
|
||||
this.element.classList.add("rotating");
|
||||
|
||||
let self = this;
|
||||
let delayPromise = new Promise(function (resolve) {
|
||||
setTimeout(resolve, 250);
|
||||
}).then(()=>{
|
||||
self.element.classList.remove("rotating");
|
||||
}).then(() => {
|
||||
if (self.rotation === currentRotation) {
|
||||
self.element.classList.remove("rotating");
|
||||
}
|
||||
});
|
||||
this.getLevel().checkHasWon(delayPromise);
|
||||
return delayPromise;
|
||||
@@ -40,7 +44,6 @@ export class ParentSegment extends Segment {
|
||||
}
|
||||
|
||||
applyRotations(rotations) {
|
||||
// debugger;
|
||||
this.rotation = rotations[0];
|
||||
rotations.splice(0, 1);
|
||||
for (let i = 0, n = this.children.length; i < n; i++) {
|
||||
@@ -49,6 +52,15 @@ export class ParentSegment extends Segment {
|
||||
return rotations;
|
||||
}
|
||||
|
||||
getCurrentRotations(rotations)
|
||||
{
|
||||
rotations.push(this.rotation);
|
||||
for (let i = 0, n = this.children.length; i < n; i++) {
|
||||
rotations = this.children[i].getCurrentRotations(rotations);
|
||||
}
|
||||
return rotations;
|
||||
}
|
||||
|
||||
isSolved() {
|
||||
for (let i = 0, n = this.children.length; i < n; i++) {
|
||||
if (!this.children[i].isSolved()) {
|
||||
@@ -57,7 +69,7 @@ export class ParentSegment extends Segment {
|
||||
}
|
||||
return (this.rotation === 0 || (
|
||||
this.children[0].sameAs(this.children[2]) && this.children[1].sameAs(this.children[3]) && (
|
||||
this.rotation === 2 || this.children[0].sameAs(this.children[1]))))
|
||||
this.rotation === 2 || this.children[0].sameAs(this.children[1]))))
|
||||
}
|
||||
|
||||
setChildren(children) {
|
||||
@@ -98,8 +110,7 @@ export class ParentSegment extends Segment {
|
||||
for (let i = 0, n = this.children.length; i < n; i++) {
|
||||
this.children[i]._updateElement();
|
||||
childContainer.appendChild(this.children[i].getElement());
|
||||
if (i%2 === 1 && this.children.length-1 !== i)
|
||||
{
|
||||
if (i % 2 === 1 && this.children.length - 1 !== i) {
|
||||
childContainer.appendChild(document.createElement("br"));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,6 +11,13 @@ export class RowSegment extends ParentSegment{
|
||||
return rotations;
|
||||
}
|
||||
|
||||
getCurrentRotations(rotations){
|
||||
for (let i = 0, n = this.children.length; i < n; i++) {
|
||||
rotations = this.children[i].getCurrentRotations(rotations);
|
||||
}
|
||||
return rotations;
|
||||
}
|
||||
|
||||
_updateElement() {
|
||||
const childContainer = this.element.querySelector(".child-container");
|
||||
childContainer.removeAllChildren();
|
||||
|
||||
@@ -7,6 +7,10 @@ export class Segment{
|
||||
this.parent = null;
|
||||
}
|
||||
|
||||
getCurrentRotations(rotations){
|
||||
return rotations;
|
||||
}
|
||||
|
||||
sameAs(otherSegment){
|
||||
return false;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user