Münzen und Hilfe hinzugefügt

This commit is contained in:
silas
2018-09-21 00:34:17 +02:00
parent 6a28266cd9
commit e83d0f8544
18 changed files with 351 additions and 167 deletions

View File

@@ -4793,6 +4793,10 @@ class Segment{
}
}
canRotate(){
return false;
}
isSolved(){
return (this.rotation === 0);
}
@@ -4807,6 +4811,15 @@ class Segment{
return rotations;
}
applyLocks(locks)
{
return locks;
}
getCurrentLocked(lockedArray){
return lockedArray;
}
getElement()
{
return this.element;
@@ -4814,7 +4827,7 @@ class Segment{
}
class ParentSegment extends Segment {
static initListener(){
static initListener() {
window.addEventListener("mousedown", (e) => {
ParentSegment.mouseDownTarget = e.target;
// ParentSegment.mouseDownTarget = e.originalTarget;
@@ -4825,8 +4838,7 @@ class ParentSegment extends Segment {
window.addEventListener("touchstart", (e) => {
console.log("start", e);
if (e.targetTouches.length === 1)
{
if (e.targetTouches.length === 1) {
ParentSegment.mouseDownTarget = e.targetTouches[0].target;
}
});
@@ -4836,26 +4848,27 @@ class ParentSegment extends Segment {
});
}
setIsRotatable(rotatable){
this.rotatable = rotatable;
this._updateElement();
}
constructor(element) {
super(element);
this.children = [];
this.class = "rotate-0";
this.rotatable = true;
let self = this;
this.touchendListener = function(e){
if (e.targetTouches.length === 0 && e.changedTouches.length === 1 && self.element.contains(ParentSegment.mouseDownTarget) && self.element.contains(document.elementFromPoint(e.changedTouches[0].pageX, e.changedTouches[0].pageY)))
{
console.log(e);
this.touchendListener = function (e) {
if (e.targetTouches.length === 0 && e.changedTouches.length === 1 && self.element.contains(ParentSegment.mouseDownTarget) && self.element.contains(document.elementFromPoint(e.changedTouches[0].pageX, e.changedTouches[0].pageY))) {
self.rotate();
e.stopPropagation();
e.preventDefault();
}
};
this.mouseupListener = function(e){
console.log("mouseup", e);
// if (ParentSegment.mouseDownTarget !== null && self.element.contains(ParentSegment.mouseDownTarget) && self.element.contains(e.originalTarget))
if (ParentSegment.mouseDownTarget !== null && self.element.contains(ParentSegment.mouseDownTarget) && self.element.contains(e.target))
{
this.mouseupListener = function (e) {
if (ParentSegment.mouseDownTarget !== null && self.element.contains(ParentSegment.mouseDownTarget) && self.element.contains(e.target)) {
self.rotate();
e.stopPropagation();
e.preventDefault();
@@ -4863,10 +4876,13 @@ class ParentSegment extends Segment {
};
}
canRotate() {
return (this.rotatable && !this.getLevel().getHasWon());
}
async rotate() {
let timeout = 250;
if (!this.getLevel().getHasWon()) {
if (this.canRotate()) {
this.rotation += 90;
this.rotation %= 360;
@@ -4909,8 +4925,17 @@ class ParentSegment extends Segment {
return rotations;
}
getCurrentRotations(rotations)
{
applyLocks(locks) {
console.log(locks);
this.rotatable = locks[0];
locks.splice(0, 1);
for (let i = 0, n = this.children.length; i < n; i++) {
locks = this.children[i].applyLocks(locks);
}
return locks;
}
getCurrentRotations(rotations) {
rotations.push(this.rotation);
for (let i = 0, n = this.children.length; i < n; i++) {
rotations = this.children[i].getCurrentRotations(rotations);
@@ -4918,6 +4943,14 @@ class ParentSegment extends Segment {
return rotations;
}
getCurrentLocked(locked) {
locked.push(this.rotatable);
for (let i = 0, n = this.children.length; i < n; i++) {
locked = this.children[i].getCurrentLocked(locked);
}
return locked;
}
isSolved() {
for (let i = 0, n = this.children.length; i < n; i++) {
if (!this.children[i].isSolved()) {
@@ -4953,11 +4986,13 @@ class ParentSegment extends Segment {
}
_updateElement() {
let layer = this._getLayer();
if (layer >= 2)
{
this.element.classList.add("layer-"+layer);
if (layer >= 2) {
this.element.classList.add("layer-" + layer);
}
if (!this.rotatable){
this.element.classList.add("locked");
}
const childContainer = this.element.querySelector(".child-container");
@@ -4965,14 +5000,8 @@ class ParentSegment extends Segment {
this._updateRotationClass();
// const self = this;
// this.element.onclick = function (e) {
// self.rotate();
// e.stopPropagation();
// };
this.element.removeEventListener("mouseup",this.mouseupListener);
this.element.removeEventListener("touchend",this.touchendListener);
this.element.removeEventListener("mouseup", this.mouseupListener);
this.element.removeEventListener("touchend", this.touchendListener);
this.element.addEventListener("mouseup", this.mouseupListener);
this.element.addEventListener("touchend", this.touchendListener);
@@ -4986,14 +5015,14 @@ class ParentSegment extends Segment {
}
}
_getLayer(){
if (this.children.length >= 1 && this.children[0] && this.children[0] instanceof ParentSegment)
{
return this.children[0]._getLayer()+1;
_getLayer() {
if (this.children.length >= 1 && this.children[0] && this.children[0] instanceof ParentSegment) {
return this.children[0]._getLayer() + 1;
}
return 1;
}
}
ParentSegment.initListener();
class LeafSegment extends Segment {
@@ -5070,7 +5099,16 @@ class Level {
saveAsCurrentLevel(){
let rotations = this.getCurrentRotations();
localStorage.setItem("currentLevel", JSON.stringify({"id": this.id, "rotations": rotations}));
let locked = this.getCurrentLocked();
localStorage.setItem("currentLevel", JSON.stringify({"id": this.id, "rotations": rotations, "locks":locked}));
}
getCurrentLocked(){
if (this.rootSegment !== null)
{
return this.rootSegment.getCurrentLocked([]);
}
return [];
}
getCurrentRotations(){
@@ -5081,6 +5119,13 @@ class Level {
return [];
}
setLocks(locks)
{
if (this.rootSegment !== null){
this.rootSegment.applyLocks(locks);
}
}
setId(id)
{
this.id = id;
@@ -5156,6 +5201,24 @@ class Level {
createSegments() {};
getRotatableSegments(){
return Level._getRotatableSegmentsFrom(this.rootSegment);
}
static _getRotatableSegmentsFrom(segment){
let rotatable = [];
if (segment.canRotate())
{
rotatable.push(segment);
}
if (segment instanceof ParentSegment){
for (let i = 0; i < segment.children.length; i++) {
rotatable.push.apply(rotatable, Level._getRotatableSegmentsFrom(segment.children[i]));
}
}
return rotatable;
}
static _createLeafsForWord(word, leafSegmentTemplate)
{
let leafSegments = [];
@@ -5167,7 +5230,10 @@ class Level {
}
class RowSegment extends ParentSegment{
rotate() {}
constructor(element) {
super(element);
this.rotatable = false;
}
applyRotations(rotations)
{
@@ -5184,6 +5250,20 @@ class RowSegment extends ParentSegment{
return rotations;
}
getCurrentLocked(locked) {
for (let i = 0, n = this.children.length; i < n; i++) {
locked = this.children[i].getCurrentLocked(locked);
}
return locked;
}
applyLocks(locks) {
for (let i = 0, n = this.children.length; i < n; i++) {
locks = this.children[i].applyLocks(locks);
}
return locks;
}
_updateElement() {
const childContainer = this.element.querySelector(".child-container");
childContainer.removeAllChildren();
@@ -5510,7 +5590,8 @@ class LevelSite extends WordRotatorBaseSite {
createActionBarMenu(menu) {
menu = super.createActionBarMenu(menu);
let coinAction = new MenuAction(Helper.nonNull(localStorage.getItem("coins"), "0"), ()=>{}, MenuAction.SHOW_ALWAYS, 900);
let coinAction = new MenuAction(Helper.nonNull(localStorage.getItem("coins"), "0"), () => {
}, MenuAction.SHOW_ALWAYS, 900);
coinAction.setShouldTranslate(false);
coinAction._liClass = "coin-counter";
menu.addAction(coinAction);
@@ -5551,8 +5632,8 @@ class LevelSite extends WordRotatorBaseSite {
let wonText = this.findBy("#won-text");
let scaleHelper = new ScaleHelper();
scaleHelper.scaleToFull(continueButton, continueButton.parentElement, false, true);
scaleHelper.scaleToFull(wonText, wonText.parentElement);
this.continueButtonScaler = scaleHelper.scaleToFull(continueButton, continueButton.parentElement, false, true);
this.wonTextScaler = scaleHelper.scaleToFull(wonText, wonText.parentElement);
//Benutze Document, da Element außerhalb von Seite (eigentlich unschön!)
this.levelCounterActionContainer = document.getElementById("level-number-container");
@@ -5567,6 +5648,10 @@ class LevelSite extends WordRotatorBaseSite {
this.coinTemplate.id = null;
this.coinContainer.removeAllChildren();
this.findBy("#help-button").addEventListener("click", () => {
this.help();
});
this.loadLastLevel();
}
@@ -5592,6 +5677,7 @@ class LevelSite extends WordRotatorBaseSite {
});
level.createSegments();
level.setLocks(currentLevelInfo["locks"]);
level.getRootSegment()._updateElement();
level.saveAsCurrentLevel();
@@ -5681,7 +5767,10 @@ class LevelSite extends WordRotatorBaseSite {
let coinElem = Helper.cloneNode(this.coinTemplate);
this.coinContainer.appendChild(coinElem);
}
localStorage.setItem("coins", parseInt(Helper.nonNull(localStorage.getItem("coins"), "0"))+parseInt(coinsPerLevel));
localStorage.setItem("coins", parseInt(Helper.nonNull(localStorage.getItem("coins"), "0")) + parseInt(coinsPerLevel));
this.wonTextScaler();
this.continueButtonScaler();
await savePromise;
}
@@ -5689,6 +5778,32 @@ class LevelSite extends WordRotatorBaseSite {
console.error(e);
}
}
help() {
let cost = SystemSettings.get("costForHelp", 25);
let currentCoins = parseInt(Helper.nonNull(localStorage.getItem("coins"), 0));
if (currentCoins >= cost) {
currentCoins -= cost;
localStorage.setItem("coins", currentCoins);
this.coinAction.title = currentCoins;
this.coinAction.redraw();
let rotatables = this.level.getRotatableSegments();
let index = Math.floor(Math.random() * rotatables.length);
console.log(rotatables, index);
let segmentToHelp = rotatables[index];
while (segmentToHelp.rotation !== 0) {
segmentToHelp.rotate();
}
segmentToHelp.setIsRotatable(false);
this.level.saveAsCurrentLevel();
}
else{
FlashMessenger.addMessage("not-enough-coins");
}
}
}
class SynchronizeSite extends WordRotatorBaseSite {

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long