[Picross-commit] SF.net SVN: picross:[138] trunk/phaser
Status: Pre-Alpha
Brought to you by:
yvan_norsa
From: <yva...@us...> - 2020-04-11 13:39:30
|
Revision: 138 http://sourceforge.net/p/picross/code/138 Author: yvan_norsa Date: 2020-04-11 13:39:28 +0000 (Sat, 11 Apr 2020) Log Message: ----------- added menu and level loading Modified Paths: -------------- trunk/phaser/GameScene.js trunk/phaser/MainMenu.js trunk/phaser/RandomPicrossModel.js trunk/phaser/SimpleButton.js trunk/phaser/UIBox.js trunk/phaser/index.html trunk/phaser/picross.js Added Paths: ----------- trunk/phaser/AbstractPicrossModel.js trunk/phaser/LevelMenu.js trunk/phaser/ModeMenu.js trunk/phaser/XBMModel.js Added: trunk/phaser/AbstractPicrossModel.js =================================================================== --- trunk/phaser/AbstractPicrossModel.js (rev 0) +++ trunk/phaser/AbstractPicrossModel.js 2020-04-11 13:39:28 UTC (rev 138) @@ -0,0 +1,88 @@ +/* Copyright Yvan Norsa (2007-2020) + * + * yva...@gm... + * + * This software is a computer program whose purpose is to [describe + * functionalities and technical features of your software]. + * + * This software is governed by the CeCILL license under French law and + * abiding by the rules of distribution of free software. You can use, + * modify and/ or redistribute the software under the terms of the CeCILL + * license as circulated by CEA, CNRS and INRIA at the following URL + * "http://www.cecill.info". + * + * As a counterpart to the access to the source code and rights to copy, + * modify and redistribute granted by the license, users are provided only + * with a limited warranty and the software's author, the holder of the + * economic rights, and the successive licensors have only limited + * liability. + * + * In this respect, the user's attention is drawn to the risks associated + * with loading, using, modifying and/or developing or reproducing the + * software by the user in light of its specific status of free software, + * that may mean that it is complicated to manipulate, and that also + * therefore means that it is reserved for developers and experienced + * professionals having in-depth computer knowledge. Users are therefore + * encouraged to load and test the software's suitability as regards their + * requirements in conditions enabling the security of their systems and/or + * data to be ensured and, more generally, to use and operate it in the + * same conditions as regards security. + * + * The fact that you are presently reading this means that you have had + * knowledge of the CeCILL license and that you accept its terms. + */ + + +/** + * Model handling the puzzle data. + * + * @author Y. Norsa + */ + class AbstractPicrossModel { + + + /*** Constructor ***/ + + /** Constructor. */ + /*** Accessors ***/ + + /** + * Returns the width. + * + * @return grid width + */ + getWidth() { + return this.width; + } + + /** + * Returns the height. + * + * @return grid height + */ + getHeight() { + return this.height; + } + + /** + * Returns the content. + * + * @return grid content + */ + getData() { + return this.data; + /* + boolean[][] dataCopy = new boolean[this.data.length][]; + + for (int i = 0; i < this.data.length; i++) { + dataCopy[i] = new boolean[this.data[i].length]; + System.arraycopy(this.data[i], 0, + dataCopy[i], 0, + this.data[i].length); + } + + return dataCopy; + */ + } +} + Modified: trunk/phaser/GameScene.js =================================================================== --- trunk/phaser/GameScene.js 2020-03-14 09:39:37 UTC (rev 137) +++ trunk/phaser/GameScene.js 2020-04-11 13:39:28 UTC (rev 138) @@ -37,6 +37,17 @@ super({ key: 'game' }); } + + init(data) { + if (data.file) { + this.gridModel = new XBMModel(this, data.file); + } else { + this.gridModel = new RandomPicrossModel(); + } + + this.gridModel.isComplete; + } + preload () { this.load.setBaseURL('http://picross.sourceforge.net/phaser/images/'); this.load.image('checked-rollover', 'checked-rollover.png'); @@ -73,12 +84,11 @@ } */ - var gridModel = new RandomPicrossModel(); - var width = gridModel.width; - var height = gridModel.height; + var width = this.gridModel.width; + var height = this.gridModel.height; - this.model = new GridMediator(width, height, gridModel.data, this); + this.model = new GridMediator(width, height, this.gridModel.data, this); //this.input.on('pointermove', function (pointer) {this.model.controller.mouseMoved(pointer)}, this); Added: trunk/phaser/LevelMenu.js =================================================================== --- trunk/phaser/LevelMenu.js (rev 0) +++ trunk/phaser/LevelMenu.js 2020-04-11 13:39:28 UTC (rev 138) @@ -0,0 +1,72 @@ +/* Copyright Yvan Norsa (2007-2020) + * + * yva...@gm... + * + * This software is a computer program whose purpose is to [describe + * functionalities and technical features of your software]. + * + * This software is governed by the CeCILL license under French law and + * abiding by the rules of distribution of free software. You can use, + * modify and/ or redistribute the software under the terms of the CeCILL + * license as circulated by CEA, CNRS and INRIA at the following URL + * "http://www.cecill.info". + * + * As a counterpart to the access to the source code and rights to copy, + * modify and redistribute granted by the license, users are provided only + * with a limited warranty and the software's author, the holder of the + * economic rights, and the successive licensors have only limited + * liability. + * + * In this respect, the user's attention is drawn to the risks associated + * with loading, using, modifying and/or developing or reproducing the + * software by the user in light of its specific status of free software, + * that may mean that it is complicated to manipulate, and that also + * therefore means that it is reserved for developers and experienced + * professionals having in-depth computer knowledge. Users are therefore + * encouraged to load and test the software's suitability as regards their + * requirements in conditions enabling the security of their systems and/or + * data to be ensured and, more generally, to use and operate it in the + * same conditions as regards security. + * + * The fact that you are presently reading this means that you have had + * knowledge of the CeCILL license and that you accept its terms. + */ + + class LevelMenu extends Phaser.Scene { + constructor () { + super({ key: 'levelMenu' }); + } + + preload () { + this.load.setBaseURL('http://picross.sourceforge.net/phaser/images/'); + this.load.image('background', 'background.png'); + this.load.image('empty_button', 'empty_button.png'); + } + + create () { + this.add.image(0, 0, "background").setOrigin(0, 0); + + new SimpleButton( this, + 225, + 200, + 'empty_button', + function() { + this.scene.start('game', {file: 'data/apple.xbm'}); + }); + + + this.add.text(200, 190, "apple").setOrigin(0); + + + new SimpleButton( this, + 225, + 300, + 'empty_button', + function() { + this.scene.start('game', {file: 'data/batman2.xbm'}); + }); + + + this.add.text(200, 290, "batman2").setOrigin(0); + } +}; \ No newline at end of file Modified: trunk/phaser/MainMenu.js =================================================================== --- trunk/phaser/MainMenu.js 2020-03-14 09:39:37 UTC (rev 137) +++ trunk/phaser/MainMenu.js 2020-04-11 13:39:28 UTC (rev 138) @@ -44,15 +44,16 @@ //this.load.image('checked', 'checked.png'); //this.load.image('crossed', 'crossed.png'); //this.load.image('crossed-rollover', 'crossed-rollover.png'); - this.load.image('empty_button', 'empty_button.png'); + //this.load.image('empty_button', 'empty_button.png'); //this.load.image('empty-rollover', 'empty-rollover.png'); //this.load.image('empty', 'empty.png'); //this.load.image('hint', 'hint.png'); + this.load.image('play_button', 'en/button_play.png'); } buttonCallback() { //TODO rest of the menu - this.scene.start("game"); + this.scene.start("modeMenu"); } create () { @@ -64,10 +65,10 @@ */ new SimpleButton( this, - 150, - 200, - 'empty_button', + 225, + 225, + 'play_button', this.buttonCallback); - this.add.text(160, 210, "Play"); + //this.add.text(160, 210, "Play"); } }; \ No newline at end of file Added: trunk/phaser/ModeMenu.js =================================================================== --- trunk/phaser/ModeMenu.js (rev 0) +++ trunk/phaser/ModeMenu.js 2020-04-11 13:39:28 UTC (rev 138) @@ -0,0 +1,70 @@ +/* Copyright Yvan Norsa (2007-2020) + * + * yva...@gm... + * + * This software is a computer program whose purpose is to [describe + * functionalities and technical features of your software]. + * + * This software is governed by the CeCILL license under French law and + * abiding by the rules of distribution of free software. You can use, + * modify and/ or redistribute the software under the terms of the CeCILL + * license as circulated by CEA, CNRS and INRIA at the following URL + * "http://www.cecill.info". + * + * As a counterpart to the access to the source code and rights to copy, + * modify and redistribute granted by the license, users are provided only + * with a limited warranty and the software's author, the holder of the + * economic rights, and the successive licensors have only limited + * liability. + * + * In this respect, the user's attention is drawn to the risks associated + * with loading, using, modifying and/or developing or reproducing the + * software by the user in light of its specific status of free software, + * that may mean that it is complicated to manipulate, and that also + * therefore means that it is reserved for developers and experienced + * professionals having in-depth computer knowledge. Users are therefore + * encouraged to load and test the software's suitability as regards their + * requirements in conditions enabling the security of their systems and/or + * data to be ensured and, more generally, to use and operate it in the + * same conditions as regards security. + * + * The fact that you are presently reading this means that you have had + * knowledge of the CeCILL license and that you accept its terms. + */ + + class ModeMenu extends Phaser.Scene { + constructor () { + super({ key: 'modeMenu' }); + } + + preload () { + this.load.setBaseURL('http://picross.sourceforge.net/phaser/images/'); + this.load.image('background', 'background.png'); + this.load.image('select_button', 'en/button_select.png'); + this.load.image('random_button', 'en/button_random.png'); + } + + selectButtonCallback() { + this.scene.start("levelMenu"); + } + + randomButtonCallback() { + this.scene.start("game"); + } + + create () { + this.add.image(0, 0, "background").setOrigin(0, 0); + + new SimpleButton( this, + 225, + 200, + 'select_button', + this.selectButtonCallback); + + new SimpleButton( this, + 225, + 275, + 'random_button', + this.randomButtonCallback); + } +}; \ No newline at end of file Modified: trunk/phaser/RandomPicrossModel.js =================================================================== --- trunk/phaser/RandomPicrossModel.js 2020-03-14 09:39:37 UTC (rev 137) +++ trunk/phaser/RandomPicrossModel.js 2020-04-11 13:39:28 UTC (rev 138) @@ -33,9 +33,11 @@ */ -class RandomPicrossModel { +class RandomPicrossModel extends AbstractPicrossModel { constructor() { +super(); + this.width = 0; this.height = 0; Modified: trunk/phaser/SimpleButton.js =================================================================== --- trunk/phaser/SimpleButton.js 2020-03-14 09:39:37 UTC (rev 137) +++ trunk/phaser/SimpleButton.js 2020-04-11 13:39:28 UTC (rev 138) @@ -33,25 +33,19 @@ */ class SimpleButton extends Phaser.GameObjects.Image { - constructor(_scene, _x, _y, _tex, _callback) { - super(_scene, _x, _y, _tex, _tex); + constructor(scene, x, y, img, callback) { + super(scene, x, y, img, img); - this.myCallback = _callback; - this.myScope = _scene; // scope - + this.callback = callback; + this.scene = scene; + this.setInteractive(); - this.on('pointerup', this.pointerUp, this); - this.on('pointerdown', this.pointerDown, this); + this.on('pointerup', this.mouseReleased, this); - _scene.add.existing(this); - + scene.add.existing(this); } - pointerUp(pointer) { - this.myCallback.call(this.myScope,'up'); + mouseReleased(e) { + this.callback.call(this.scene,'up'); } - - pointerDown(pointer) { - this.myCallback.call(this.myScope,'down'); - } } \ No newline at end of file Modified: trunk/phaser/UIBox.js =================================================================== --- trunk/phaser/UIBox.js 2020-03-14 09:39:37 UTC (rev 137) +++ trunk/phaser/UIBox.js 2020-04-11 13:39:28 UTC (rev 138) @@ -1,4 +1,4 @@ - /* Copyright Yvan Norsa (2007-2020) +/* Copyright Yvan Norsa (2007-2020) * * yva...@gm... * Added: trunk/phaser/XBMModel.js =================================================================== --- trunk/phaser/XBMModel.js (rev 0) +++ trunk/phaser/XBMModel.js 2020-04-11 13:39:28 UTC (rev 138) @@ -0,0 +1,262 @@ +/* Copyright Yvan Norsa (2007-2020) + * + * yva...@gm... + * + * This software is a computer program whose purpose is to [describe + * functionalities and technical features of your software]. + * + * This software is governed by the CeCILL license under French law and + * abiding by the rules of distribution of free software. You can use, + * modify and/ or redistribute the software under the terms of the CeCILL + * license as circulated by CEA, CNRS and INRIA at the following URL + * "http://www.cecill.info". + * + * As a counterpart to the access to the source code and rights to copy, + * modify and redistribute granted by the license, users are provided only + * with a limited warranty and the software's author, the holder of the + * economic rights, and the successive licensors have only limited + * liability. + * + * In this respect, the user's attention is drawn to the risks associated + * with loading, using, modifying and/or developing or reproducing the + * software by the user in light of its specific status of free software, + * that may mean that it is complicated to manipulate, and that also + * therefore means that it is reserved for developers and experienced + * professionals having in-depth computer knowledge. Users are therefore + * encouraged to load and test the software's suitability as regards their + * requirements in conditions enabling the security of their systems and/or + * data to be ensured and, more generally, to use and operate it in the + * same conditions as regards security. + * + * The fact that you are presently reading this means that you have had + * knowledge of the CeCILL license and that you accept its terms. + */ + +/** + * Class loading a XBM file. + * + * @author Y. Norsa + */ +class XBMModel extends AbstractPicrossModel { + + /*** Constructor ***/ + + /** + * Constructor. + * + * @param input XBM file input stream + * @throws IllegalArgumentException if <code>input</code> + * is <code>null</code> + * @throws IOException if there is a problem while reading the file + * @throws XBMException if the file isn't a valid XBM file + */ + constructor(scene, filename) { + super(); + + this.filename = filename; + +/* + if (input == null) { + throw new IllegalArgumentException("input can't be null"); + } + */ + + this.scene = scene; + + scene.load.once('filecomplete', this.buildLevel, this); + scene.load.text(filename, 'http://picross.sourceforge.net/phaser/' + filename); + scene.load.start(); + } + + buildLevel() { + // List<String> byteValues = new ArrayList<String>(); + const byteValues = []; + + var data = this.scene.cache.text.get(this.filename); + var lines = data.split('\n'); + + for (var i = 0; i < lines.length; i++) { + var line = lines[i]; + //while ((line = in.readLine()) != null) { + line = line.trim(); + + if (line.startsWith(XBMModel.DEFINE_DIRECTIVE) + && line.includes(XBMModel.WIDTH_KEYWORD)) { + + this.width = XBMModel.readLineValue(line); + + //XBMModel.log.debug("width = " + this.width); + continue; + } + + if (line.startsWith(XBMModel.DEFINE_DIRECTIVE) + && line.includes(XBMModel.HEIGHT_KEYWORD)) { + + this.height = XBMModel.readLineValue(line); + + //XBMModel.log.debug("height = " + this.height); + continue; + } + + if (line.includes(XBMModel.BITS_KEYWORD)) { + //this.data = new boolean[this.width][this.height]; + this.data = Array(this.width).fill().map(() => Array(this.height).fill(false)); + + //while ((line = in.readLine()) != null) + //FIXME read file again + while ((line = lines[++i])) + { + var values = line.split(XBMModel.VALUE_SEPARATOR); + + for (var k = 0; k < values.length; k++) { + if (values[k].includes(XBMModel.HEX_LEADING)) { + values[k] = values[k].trim(); + + //XBMModel.log.debug("values[" + i + "] = " + // + values[i]); + + if ((values[k].length + < XBMModel.VALUE_END_POS) + || !XBMModel.isLetterOrDigit(values[k] + .charAt(XBMModel + .VALUE_END_POS + - 1))) { + values[k] = values[k] + .substring(0, + XBMModel.VALUE_BEGINNING_POS) + + 0 + + values[k] + .substring(XBMModel + .VALUE_BEGINNING_POS); + } + + var byteStr = + values[k] + .substring(XBMModel.VALUE_BEGINNING_POS, + XBMModel.VALUE_END_POS); + byteValues.push(byteStr); + } + } + } + } + } + + // in.close(); + +/* + if (width <= 0 || height <= 0 || this.data == null) { + throw new XBMException("Invalid XBM file"); + } +*/ + var xIndex = 0; + var yIndex = 0; + + for (var i = 0; i < byteValues.length; i++) { + var byteStr = byteValues[i]; + var binaryStr = XBMModel.toBits(byteStr); + //XBMModel.log.debug("binaryStr : " + binaryStr); + + for (var j = XBMModel.BYTE_LENGTH - 1; j >= 0; j--) { + //XBMModel.log.debug("this.data[" + yIndex + //+ "][" + xIndex + "] = " + //+ (binaryStr.charAt(j) == '1' ? true + //: false)); + + this.data[yIndex++][xIndex] = + (binaryStr.charAt(j) == '1' ? true : false); + + if (yIndex == this.width) { + xIndex++; + yIndex = 0; + + break; + } + + } + } + + } + + /*** Static methods ***/ + + static isLetterOrDigit(character) { + var letterNumber = /^[0-9a-zA-Z]+$/; + + if (character.match(letterNumber)) { + return true; + } + + return false; + } + + /** + * Reads a constant value (C-style) in a line. + * + * @param line the line to read + * @return defined value or -1 + * @throws XBMException if the line format is incorrect + */ + static readLineValue(line) { + var spaceIndex = line.lastIndexOf(XBMModel.VALUE_MARKER); + + if (spaceIndex == -1) { + return XBMModel.ERROR_VALUE; + } + + return Number(line.substring(spaceIndex).trim()); + + } + + /** + * Converts a hex value to its binary representation. + * + * @param byteStr the original String + * @return binary representation + */ + static toBits( byteStr) { + var byteVal = parseInt(byteStr, XBMModel.HEX_RADIX); + var binaryStr = byteVal.toString(2); + + binaryStr = binaryStr.padStart(XBMModel.BYTE_LENGTH, '0'); + + return binaryStr; + } +} + + /*** Constants ***/ + + /** Indicates a constant definition. */ + XBMModel.DEFINE_DIRECTIVE = "#define "; + + /** The width value. */ + XBMModel.WIDTH_KEYWORD = "width"; + + /** The height value. */ + XBMModel.HEIGHT_KEYWORD = "height"; + + /** Character before the int value. */ + XBMModel.VALUE_MARKER = ' '; + + /** The data values. */ + XBMModel.BITS_KEYWORD = "bits"; + + /** Value separator. */ + XBMModel.VALUE_SEPARATOR = ","; + + /** String indicating a hex value. */ + XBMModel.HEX_LEADING = "0x"; + + /** In case of error. */ + XBMModel.ERROR_VALUE = -1; + + /** Beginning of the actual value. */ + XBMModel.VALUE_BEGINNING_POS = 2; + + /** End of the actual value. */ + XBMModel.VALUE_END_POS = 4; + + /** Hexadecimal number radix. */ + XBMModel.HEX_RADIX = 16; + + /** Length of a byte. */ + XBMModel.BYTE_LENGTH = 8; Modified: trunk/phaser/index.html =================================================================== --- trunk/phaser/index.html 2020-03-14 09:39:37 UTC (rev 137) +++ trunk/phaser/index.html 2020-04-11 13:39:28 UTC (rev 138) @@ -10,7 +10,11 @@ <script src="SimpleButton.js"></script> <script src="MainMenu.js"></script> + <script src="ModeMenu.js"></script> + <script src="LevelMenu.js"></script> + <script src="AbstractPicrossModel.js"></script> <script src="RandomPicrossModel.js"></script> + <script src="XBMModel.js"></script> <script src="CompletedHints.js"></script> <script src="HintBox.js"></script> <script src="Box.js"></script> Modified: trunk/phaser/picross.js =================================================================== --- trunk/phaser/picross.js 2020-03-14 09:39:37 UTC (rev 137) +++ trunk/phaser/picross.js 2020-04-11 13:39:28 UTC (rev 138) @@ -36,11 +36,11 @@ var config = { type: Phaser.AUTO, - width: 900, + width: 1000, height: 900, backgroundColor: '#ffffff', disableContextMenu: true, - scene: [/*MainMenu, */GameScene] + scene: [MainMenu, ModeMenu, LevelMenu, GameScene] }; var game = new Phaser.Game(config); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |