squishjs 0.6.43 → 0.7.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/workflows/node.js.yml +1 -1
- package/LICENSE.md +21 -0
- package/README.md +116 -42
- package/index.js +11 -1
- package/oldsvgexport.js +89 -0
- package/package.json +1 -1
- package/render-tester.js +120 -0
- package/src/BaseNode.js +101 -0
- package/src/Game.js +15 -3
- package/src/GameNode.js +82 -79
- package/src/InternalGameNode.js +4 -3
- package/src/Squisher.js +224 -0
- package/src/ViewableGame.js +60 -0
- package/src/node-types.js +15 -0
- package/src/squish.js +90 -349
- package/src/squishHelpers/asset.js +63 -0
- package/src/squishHelpers/border.js +16 -0
- package/src/squishHelpers/color.js +16 -0
- package/src/squishHelpers/coordinates2d.js +98 -0
- package/src/squishHelpers/effect.js +55 -0
- package/src/squishHelpers/fill.js +16 -0
- package/src/squishHelpers/handleClick.js +16 -0
- package/src/squishHelpers/id.js +16 -0
- package/src/squishHelpers/input.js +22 -0
- package/src/squishHelpers/playerIds.js +12 -0
- package/src/squishHelpers/pos.js +23 -0
- package/src/squishHelpers/size.js +23 -0
- package/src/squishHelpers/subType.js +16 -0
- package/src/squishHelpers/text.js +154 -0
- package/src/subtype-mappings.js +10 -0
- package/src/subtypes.js +7 -0
- package/{dist/terrain/defs.d.ts → src/terrain/defs.ts} +7 -6
- package/src/terrain/index.ts +156 -0
- package/src/terrain/terrainFunctions.ts +142 -0
- package/src/util/geometry.js +45 -0
- package/src/util/index.ts +6 -0
- package/src/util/listenable.ts +20 -0
- package/src/util/shapes.ts +18 -0
- package/src/util/views.js +108 -0
- package/test/Layers.test.js +63 -0
- package/test/Scale.test.js +235 -0
- package/test/Squisher.test.js +49 -0
- package/test/StateUpdates.js +10 -0
- package/test/Terrain.test.js +0 -1
- package/test/main.test.js +55 -35
- package/test/utils.js +91 -0
- package/testapp.html +26 -0
- package/testapp.js +988 -0
- package/tsconfig.json +22 -0
- package/dist/Colors.d.ts +0 -7
- package/dist/Colors.js +0 -124
- package/dist/Game.d.ts +0 -20
- package/dist/Game.js +0 -48
- package/dist/GameNode.d.ts +0 -66
- package/dist/GameNode.js +0 -148
- package/dist/InternalGameNode.d.ts +0 -20
- package/dist/InternalGameNode.js +0 -32
- package/dist/Shapes.d.ts +0 -6
- package/dist/Shapes.js +0 -8
- package/dist/index.d.ts +0 -7
- package/dist/index.js +0 -18
- package/dist/sharedDefs.d.ts +0 -72
- package/dist/sharedDefs.js +0 -13
- package/dist/squish.d.ts +0 -8
- package/dist/squish.js +0 -336
- package/dist/terrain/defs.js +0 -5
- package/dist/terrain/index.d.ts +0 -2
- package/dist/terrain/index.js +0 -151
- package/dist/terrain/terrainFunctions.d.ts +0 -6
- package/dist/terrain/terrainFunctions.js +0 -152
- package/dist/util/index.d.ts +0 -2
- package/dist/util/index.js +0 -11
- package/dist/util/listenable.d.ts +0 -3
- package/dist/util/listenable.js +0 -21
- package/dist/util/shapes.d.ts +0 -2
- package/dist/util/shapes.js +0 -23
- package/license.txt +0 -674
package/LICENSE.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2021 Joseph Garcia
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,45 +1,119 @@
|
|
|
1
|
+
Squish is a way to utility package for [Homegames-core](https://github.com/homegamesio/homegames-core) and [Homegames-web](https://github.com/homegamesio/homegames-web). It contains several utilities and the class definitions for the entities most important to running Homegames.
|
|
2
|
+
|
|
3
|
+
Below the term `First Class` is used to describe several functions and variables. This means those values should be treated as immutable, and in the case of functions are called automatically.
|
|
4
|
+
|
|
5
|
+
## Exported Features
|
|
6
|
+
* squish
|
|
7
|
+
* unsquish
|
|
8
|
+
* gameNode
|
|
9
|
+
* GameNode
|
|
10
|
+
* Colors
|
|
11
|
+
* Game
|
|
12
|
+
* Shapes
|
|
13
|
+
* ShapeUtils
|
|
14
|
+
* terrainGenerator
|
|
15
|
+
|
|
16
|
+
## squish
|
|
17
|
+
squish is a first class function that is used to compress the information of the game down to a set of integers. This compresses the data required to rendering down.
|
|
18
|
+
|
|
19
|
+
## unsquish
|
|
20
|
+
unsquish is a first class function that is used to uncompress squished entities. This uncompresses the data that was squished down for more easy manipulation and usage.
|
|
21
|
+
|
|
22
|
+
## gameNode
|
|
23
|
+
gameNode is the most foundational component to Homegames. The contents of a game is made up of a set of gameNodes that define entities, and display logic. Wrapped in a listener it contains the following:
|
|
24
|
+
* id :- a first calss numeric identification number. It is unique in a Game.
|
|
25
|
+
* color :- Defines the display color of the gameNode. See **Colors**
|
|
26
|
+
* handleClick :- a function that defines what happens if a user clicks on the gameNode. The function passed in will automatically be called, and will pass the player who clicked as the argument.
|
|
27
|
+
* coordinates2d :- an array of coordinates in a two dimensional plane that defines the vertices of the gameNode. It is recommened that **ShapeUtils** is used
|
|
28
|
+
* border :- a number that defines the thickness of the border on the gameNode
|
|
29
|
+
* fill :- Defines the color that fills the gameNode. See **Colors**
|
|
30
|
+
* text :- Defines the text (if any) that will be displayed by the gameNode. It has the form
|
|
31
|
+
* text :- The text to be displayed;
|
|
32
|
+
* x :- The x-position of the top-left corner of the text. This is relative to the entire screen
|
|
33
|
+
* y :- The y-position of the top-left corner of the text. This is relative to the entire screen
|
|
34
|
+
* size :- The font size of the text
|
|
35
|
+
* align :- 'left' | 'center' | 'right'
|
|
36
|
+
* color :- The Color of the text, see **Colors**
|
|
37
|
+
* asset :- Defines an asset (image, video, etc) to be displayed by the gameNode. Takes the form:
|
|
38
|
+
* assetInfo :- The information for the display of the asset.
|
|
39
|
+
* onClick :- a function that defines what happens if a user clicks on the asset. The function passed in will automatically be called, and will pass the player who clicked as the argument.
|
|
40
|
+
* coordinates2d :- an array of coordinates in a two dimensional plane that defines the vertices of the gameNode. It is recommened that **ShapeUtils** is used
|
|
41
|
+
* playerIds :- An array of player identification numbers. Defining who can see this asset.
|
|
42
|
+
* effects :- Defines any additional effects applied to the gameNode.
|
|
43
|
+
* input :- Defines whether the gameNode should accept an input or not. Takes the form:
|
|
44
|
+
* type :- 'text' | 'file'
|
|
45
|
+
* oninput :- A function that will be called with the inputting player's identification number and the data of the input. (player: number, data: any) => void;
|
|
46
|
+
* playerIds :- An array of player identification numbers, that defines which players can see and interact with the gameNode.
|
|
47
|
+
* listeners :- The set of listeners attached to the gameNode.
|
|
48
|
+
* _animation :- An interval or timeout that can be used to modify the gameNode, such as lightening the color over time to give it a fade in, or darkening the color over time to give it a fade out.
|
|
49
|
+
* onStateChange :- A first class function that will notify listeners of a state change.
|
|
50
|
+
* addListener :- A first class function that will attach an additional listener to the gameNode.
|
|
51
|
+
|
|
52
|
+
## GameNode
|
|
53
|
+
A helpful wrapper and the recommended way of generatin a gameNode. It gives you the following:
|
|
54
|
+
* Asset :- Generates a gameNode with an asset attached to it
|
|
55
|
+
* Example Call: `Asset(onClick: (player) => console.log(player), coordinates2d: [[0, 1], [2, 1], [2, 3], [0, 3], [0, 1]], assetInfo: { myAsset: { pos: { x: 0, y: 1 }, size: { x: 2, y: 3 }}}, playerIds: [2])`
|
|
56
|
+
* This will generate an asset based on `myAsset` and on clicking on the asset the asset will log out the clicking player.
|
|
57
|
+
* Shape :- Generates a generic two dimenstional gameNode
|
|
58
|
+
* Example Call: `Shape({ color: COLORS.CANDY_RED, onClick: (player) => console.log(player), shapeType: Shapes.POLYGON, coordinates2d: [[0, 1], [2, 1], [2, 3], [0, 3], [0, 1]], border: 2, fill: COLORS.CANDY_RED, playerIds: [2], null, null })`
|
|
59
|
+
* This will generate a rectangle of color COLORS.CANDY_RED, visible to player of identification number 2, and onClick will log out the player clicking.
|
|
60
|
+
* Text :- Generates a gameNode with text attached to it.
|
|
61
|
+
* Example Call: `Text({ textInfo: { text: "My Text", color: COLORS.CANDY_RED, align: 'center', x: 2, y: 3, size: 1 }, playerIds: [2] })`
|
|
62
|
+
* This will generate a gameNode with texting starting at the (2, 3) position, visible to player of identification number 2. The Text will be centered and CANDY_RED, and will be "My Text".
|
|
63
|
+
|
|
64
|
+
## Colors
|
|
65
|
+
Colors is a utility for using shared homegames RGBA values. Colors is exported as an object with two keys: COLORS & randomColor.
|
|
66
|
+
|
|
67
|
+
**COLORS** is an object that takes the form of: `string -> RGBA Array`. For example we can GET the RBGA value for Homegames CANDY_RED (RGBA of [246, 84, 106, 255]) by doing the following `COLORS.CANDY_RED`. Thus COLORS serves as a map between strings and in-built colors.
|
|
68
|
+
|
|
69
|
+
**randomColor** is a function that takes the form: `string[] => RGBA array`. It is a function to get a random RGBA from the set of RGBA that Homegames provides. It can take in array of strings that limits the options, allowing for the exclusion of certain RGBA values from returning. For example we can call this function like `randomColor(["CANDY_RED"])` which would return an RGBA array from the set of COLORS while preventing CANDY_RED from being a possible return value.
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
## Game
|
|
73
|
+
Game is the base class definition for a game in Homegames. Most games will extend the definition to Game, and leverage most or all of its internal logic. Game holds the following Member Variables:
|
|
74
|
+
* players :- Object of the form `player id -> player object`
|
|
75
|
+
* timeouts :- Array of NodeJS.Timeout that holds the timeout triggered logic for the Game
|
|
76
|
+
* intervals :- Array of NodeJS.Timeout that holds the interval triggered logic for the Game
|
|
77
|
+
* listeners :- the list of all listeners on this Game.
|
|
78
|
+
* root :- The base GameNode of this game, for which all other GameNodes are children
|
|
79
|
+
|
|
80
|
+
Game also holds the following Instance Functions
|
|
81
|
+
* _hgAddPlayer :- This is a first class function, and is used to add a player to the Game's `players` value.
|
|
82
|
+
* _hgRemovePlayer :- This is a first class function, and is used to remmove a player from the Game's `players` value.
|
|
83
|
+
* addStateListener :- This is a first class function, and is used to add a listener to the Game's `listeners` value.
|
|
84
|
+
* removeStateListener :- This is a first class function, and is used to remove a listener from the Game's `listeners` value.
|
|
85
|
+
* getRoot :- This is a function and returns the Game's `root` value. An example call would be: `getRoot()`. It is expected that this function is overwritten in the extending class's definition.
|
|
86
|
+
* setTimeout :- This is a function, and is used to add a timeout to the Game's `timeouts` value. It takes in two arguments a function to be called at the end of the timer, and how long the timer should be. An example call would be: `setTimeout( () => console.log("Welcome to Homegames"), 200)`
|
|
87
|
+
* setInterval :- This is a function, and is used to add an interval to the Game's `intervals` value. It takes in two arguments a function to be called at the end of the timer, and how long the timer should be. An example call would be: `intervals(() => console.log("Welcome to Homegames"), 200)`
|
|
88
|
+
* close :- This is a first class function, and is used to clear all `timeouts` and `intervals` from the Game, as the Game instance is spun down.
|
|
89
|
+
|
|
90
|
+
## Shapes
|
|
91
|
+
Shapes is functionally an enum of the types of shapes Homegames supports.
|
|
92
|
+
|
|
93
|
+
## ShapeUtils
|
|
94
|
+
ShapeUtils is a utility for building shapes for use in GameNodes. ShapeUtils is exported as an object with two keys rectangle & triangle.
|
|
95
|
+
|
|
96
|
+
**rectangle** is a function that takes the form `(startX, startY, width, height) => the rectangle's coordinate set`. Thus rectangle creates an array from the starting x position, the starting y position, the width, and the height that defines the vertices of the rectangle. An example call would be: `rectangle(0, 1, 2, 3)` which would create a rectangle with the top-left corner at (0,1), and bottom-right corner at (2, 4).
|
|
97
|
+
|
|
98
|
+
**triangle** is a function that takes the form `(x1, y1, x2, y2, x3, y3) => the triangle's coordinate set`. Thus triangle creates an array from provided vertex pairings. An example call would be `triangle(0, 1, 2, 3, 4, 5)` which would create a triangle with vertices at (0, 1), (2, 3), (4, 5).
|
|
99
|
+
|
|
100
|
+
## terrainGenerator
|
|
101
|
+
terrainGenerator is a utility function that generates a "board" from the provided information. This "board" functions as a map, defining regions of the board that are randomly marked to be "filled". This can be used to procedurally generate fields of play with parts of the map unaccessible to players. The "board" is guaranteed that all un-filled points are contiguous. An example call would be: `terrainGenerator({ x: 50, y: 100 }, { x: 2, y: 3 }, 5)`. This would generate board of area 50 * 100. The "filled" regions would be of area 2 * 3, and a maximum of 5 such "filled" regions will be placed. The outcome of this would be two-dimensionl area, where each element has the form
|
|
1
102
|
```
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
... });
|
|
9
|
-
>
|
|
10
|
-
> const squished = squish(redSquare.node);
|
|
11
|
-
> const unsquished = unsquish(squished);
|
|
12
|
-
>
|
|
13
|
-
> squished
|
|
14
|
-
[
|
|
15
|
-
3, 35, 43, 3, 0,
|
|
16
|
-
44, 2, 52, 22, 20,
|
|
17
|
-
0, 20, 0, 80, 0,
|
|
18
|
-
20, 0, 80, 0, 80,
|
|
19
|
-
0, 20, 0, 80, 0,
|
|
20
|
-
20, 0, 20, 0, 53,
|
|
21
|
-
6, 255, 0, 0, 255
|
|
22
|
-
]
|
|
23
|
-
>
|
|
24
|
-
> unsquished
|
|
25
|
-
InternalGameNode {
|
|
26
|
-
id: 0,
|
|
27
|
-
children: [],
|
|
28
|
-
color: undefined,
|
|
29
|
-
handleClick: undefined,
|
|
30
|
-
coordinates2d: [
|
|
31
|
-
20, 20, 80,
|
|
32
|
-
20, 80, 80,
|
|
33
|
-
20, 80, 20,
|
|
34
|
-
20
|
|
35
|
-
],
|
|
36
|
-
border: undefined,
|
|
37
|
-
fill: [ 255, 0, 0, 255 ],
|
|
38
|
-
text: undefined,
|
|
39
|
-
asset: undefined,
|
|
40
|
-
effects: null,
|
|
41
|
-
input: null,
|
|
42
|
-
listeners: Set {},
|
|
43
|
-
playerIds: []
|
|
103
|
+
{
|
|
104
|
+
filled: boolean
|
|
105
|
+
north: boolean
|
|
106
|
+
south: boolean
|
|
107
|
+
east: boolean
|
|
108
|
+
west: boolean
|
|
44
109
|
}
|
|
45
110
|
```
|
|
111
|
+
**filled** of true means this place can be marked as inaccessible to players
|
|
112
|
+
|
|
113
|
+
**north** of true means from this place a player can move "north".
|
|
114
|
+
|
|
115
|
+
**south** of true means from this place a player can move "south".
|
|
116
|
+
|
|
117
|
+
**east** of true means from this place a player can move "east".
|
|
118
|
+
|
|
119
|
+
**west** of true means from this place a player can move "west".
|
package/index.js
CHANGED
|
@@ -3,18 +3,28 @@ const { gameNode, GameNode } = require('./src/GameNode');
|
|
|
3
3
|
const InternalGameNode = require('./src/InternalGameNode');
|
|
4
4
|
const Colors = require('./src/Colors');
|
|
5
5
|
const Game = require('./src/Game');
|
|
6
|
+
const ViewableGame = require('./src/ViewableGame');
|
|
6
7
|
const Shapes = require('./src/Shapes');
|
|
7
8
|
const shapeUtils = require('./src/util/shapes');
|
|
9
|
+
const viewUtils = require('./src/util/views');
|
|
8
10
|
const { terrainGenerator } = require('./src/terrain');
|
|
11
|
+
const subtypes = require('./src/subtypes');
|
|
12
|
+
const Squisher = require('./src/Squisher');
|
|
13
|
+
const GeometryUtils = require('./src/util/geometry');
|
|
9
14
|
|
|
10
15
|
module.exports = {
|
|
11
16
|
squish,
|
|
12
17
|
unsquish,
|
|
13
18
|
gameNode,
|
|
19
|
+
subtypes,
|
|
14
20
|
Game,
|
|
21
|
+
ViewableGame,
|
|
15
22
|
GameNode,
|
|
16
23
|
Colors,
|
|
17
24
|
Shapes,
|
|
25
|
+
Squisher,
|
|
18
26
|
ShapeUtils: shapeUtils,
|
|
19
|
-
|
|
27
|
+
ViewUtils: viewUtils,
|
|
28
|
+
TerrainGenerator: terrainGenerator,
|
|
29
|
+
GeometryUtils
|
|
20
30
|
};
|
package/oldsvgexport.js
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
const fs = require('fs');
|
|
2
|
+
const { unsquish } = require('../src/squish');
|
|
3
|
+
const subtypes = require('../src/subtypes');
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
const svgText = (gameNode, container) => {
|
|
7
|
+
const fill = gameNode.node.text.color;
|
|
8
|
+
const align = gameNode.node.text.align;
|
|
9
|
+
|
|
10
|
+
let svgAlign;
|
|
11
|
+
if (align === 'left') {
|
|
12
|
+
svgAlign = 'end';
|
|
13
|
+
}
|
|
14
|
+
if (align === 'center') {
|
|
15
|
+
svgAlign = 'middle';
|
|
16
|
+
}
|
|
17
|
+
if (align === 'right') {
|
|
18
|
+
svgAlign = 'start';
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const fillString = `rgba(${fill[0]}, ${fill[1]}, ${fill[2]}, ${fill[3]})`
|
|
22
|
+
|
|
23
|
+
return `<text text-anchor="${svgAlign}" x="${scaleX(gameNode.node.text.x, container.x)}" y="${scaleX(gameNode.node.text.y, container.y)}" fill="${fillString}">${gameNode.node.text.text}</text>`;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const svgPolygon = (gameNode, container) => {
|
|
27
|
+
const coordPairs = gameNode.node.coordinates2d;
|
|
28
|
+
const fill = gameNode.node.fill;
|
|
29
|
+
|
|
30
|
+
const fillString = `rgba(${fill[0]}, ${fill[1]}, ${fill[2]}, ${fill[3]})`
|
|
31
|
+
|
|
32
|
+
const scaledCoords = coordPairs.map(coordPair => {
|
|
33
|
+
return [scaleX(coordPair[0], container.x), scaleY(coordPair[1], container.y)];
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
return `<polygon points="${scaledCoords.join(' ')}"
|
|
37
|
+
fill="${fillString}" stroke="black" />`;
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
const scaleX = (x, containerWidth) => {
|
|
41
|
+
const widthPercentage = x / 100;
|
|
42
|
+
return widthPercentage * containerWidth;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
const scaleY = (y, containerHeight) => {
|
|
46
|
+
const heightPercentage = y / 100;
|
|
47
|
+
return heightPercentage * containerHeight;
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
const svgHelper = (gameNode, container) => {
|
|
51
|
+
|
|
52
|
+
if (gameNode.node.subType === subtypes.SHAPE_2D_POLYGON) {
|
|
53
|
+
console.log('sdfdsfdsf');
|
|
54
|
+
return svgPolygon(gameNode, container);
|
|
55
|
+
} else if (gameNode.node.subType === subtypes.SHAPE_2D_LINE) {
|
|
56
|
+
// svgString += svgPolygon(gameNode);
|
|
57
|
+
} else if (gameNode.node.subType === subtypes.SHAPE_2D_CIRCLE) {
|
|
58
|
+
// svgString += svgPolygon(gameNode);
|
|
59
|
+
} else if (gameNode.node.subType === subtypes.ASSET) {
|
|
60
|
+
// svgString += svgPolygon(gameNode);
|
|
61
|
+
} else if (gameNode.node.subType === subtypes.TEXT) {
|
|
62
|
+
console.log('yooooo his dgjdf top of the morning');
|
|
63
|
+
return svgText(gameNode, container);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// for (let i = 0; i < gameNode.node.children.length; i++) {
|
|
67
|
+
// svgHelper(gameNode.node.children[i], container, svgString);
|
|
68
|
+
// }
|
|
69
|
+
|
|
70
|
+
// return svgString;
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
module.exports = (squishedLayers, outFile) => {
|
|
74
|
+
const pageWidth = 600;
|
|
75
|
+
const pageHeight = 600;
|
|
76
|
+
let svgString = `<svg height="${pageHeight}" width="${pageWidth}" xmlns="http://www.w3.org/2000/svg" xmlns:xlink= "http://www.w3.org/1999/xlink">`;
|
|
77
|
+
// <circle cx="50" cy="50" r="40" stroke="black" stroke-width="3" fill="red" />
|
|
78
|
+
// </svg>`;
|
|
79
|
+
const container = { x: pageWidth, y: pageHeight };
|
|
80
|
+
squishedLayers.forEach(squishedNode => {
|
|
81
|
+
console.log('squished node');
|
|
82
|
+
console.log(squishedNode);
|
|
83
|
+
const node = unsquish(squishedNode);
|
|
84
|
+
svgString += svgHelper(node, container);
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
svgString += `</svg>`
|
|
88
|
+
fs.writeFileSync(outFile, svgString);
|
|
89
|
+
};
|
package/package.json
CHANGED
package/render-tester.js
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
console.log('ytoooo');
|
|
2
|
+
|
|
3
|
+
const canvas = document.getElementById('tester-canvas');
|
|
4
|
+
|
|
5
|
+
const ctx = canvas.getContext('2d');
|
|
6
|
+
|
|
7
|
+
const scaleCoordinate = ({ x, y }) => {
|
|
8
|
+
const xPercentage = x / 100;
|
|
9
|
+
const yPercentage = y / 100;
|
|
10
|
+
|
|
11
|
+
return {
|
|
12
|
+
scaledX: xPercentage * canvas.width,
|
|
13
|
+
scaledY: yPercentage * canvas.height
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
let thingToRender = [
|
|
19
|
+
[
|
|
20
|
+
3, 36, 2, 43, 3, 0, 44, 2, 52,
|
|
21
|
+
22, 5, 0, 5, 0, 95, 0, 5, 0,
|
|
22
|
+
95, 0, 95, 0, 5, 0, 95, 0, 5,
|
|
23
|
+
0, 5, 0, 53, 6, 255, 0, 0, 255
|
|
24
|
+
],
|
|
25
|
+
[
|
|
26
|
+
3, 36, 2, 43, 3, 1, 44, 2, 52,
|
|
27
|
+
22, 5, 0, 5, 0, 50, 0, 5, 0,
|
|
28
|
+
50, 0, 50, 0, 5, 0, 50, 0, 5,
|
|
29
|
+
0, 5, 0, 53, 6, 0, 0, 255, 255
|
|
30
|
+
],
|
|
31
|
+
[
|
|
32
|
+
3, 36, 2, 43, 3, 2, 44, 2, 52,
|
|
33
|
+
22, 50, 0, 5, 0, 95, 0, 5, 0,
|
|
34
|
+
95, 0, 50, 0, 50, 0, 50, 0, 50,
|
|
35
|
+
0, 5, 0, 53, 6, 0, 255, 0, 255
|
|
36
|
+
],
|
|
37
|
+
[
|
|
38
|
+
3, 36, 2, 43, 3, 3, 44, 2, 52,
|
|
39
|
+
22, 50, 0, 50, 0, 95, 0, 50, 0,
|
|
40
|
+
95, 0, 95, 0, 50, 0, 95, 0, 50,
|
|
41
|
+
0, 50, 0, 53, 6, 0, 0, 0, 255
|
|
42
|
+
],
|
|
43
|
+
[
|
|
44
|
+
3, 36, 2, 43, 3, 4, 44, 2, 52,
|
|
45
|
+
22, 5, 0, 50, 0, 50, 0, 50, 0,
|
|
46
|
+
50, 0, 95, 0, 5, 0, 95, 0, 5,
|
|
47
|
+
0, 50, 0, 53, 6, 255, 255, 255, 255
|
|
48
|
+
]
|
|
49
|
+
];
|
|
50
|
+
|
|
51
|
+
const drawMarker = ({ x, y, horizontal }) => {
|
|
52
|
+
const { scaledX, scaledY } = scaleCoordinate({ x, y });
|
|
53
|
+
|
|
54
|
+
if (horizontal) {
|
|
55
|
+
const lineWidth = .015 * canvas.width;
|
|
56
|
+
ctx.beginPath();
|
|
57
|
+
ctx.moveTo(scaledX, scaledY);
|
|
58
|
+
ctx.lineTo(scaledX + lineWidth, scaledY);
|
|
59
|
+
ctx.stroke();
|
|
60
|
+
} else {
|
|
61
|
+
// line is 1.5% of height
|
|
62
|
+
const lineHeight = .015 * canvas.height;
|
|
63
|
+
ctx.beginPath();
|
|
64
|
+
ctx.moveTo(scaledX, scaledY);
|
|
65
|
+
ctx.lineTo(scaledX, scaledY + lineHeight);
|
|
66
|
+
ctx.stroke();
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
const drawMarkers = () => {
|
|
71
|
+
const horizontalPoints = [
|
|
72
|
+
[0, 10],
|
|
73
|
+
[0, 20],
|
|
74
|
+
[0, 30],
|
|
75
|
+
[0, 40],
|
|
76
|
+
[0, 50],
|
|
77
|
+
[0, 60],
|
|
78
|
+
[0, 70],
|
|
79
|
+
[0, 80],
|
|
80
|
+
[0, 90]
|
|
81
|
+
];
|
|
82
|
+
|
|
83
|
+
const verticalPoints = [
|
|
84
|
+
[10, 0],
|
|
85
|
+
[20, 0],
|
|
86
|
+
[30, 0],
|
|
87
|
+
[40, 0],
|
|
88
|
+
[50, 0],
|
|
89
|
+
[60, 0],
|
|
90
|
+
[70, 0],
|
|
91
|
+
[80, 0],
|
|
92
|
+
[90, 0]
|
|
93
|
+
];
|
|
94
|
+
|
|
95
|
+
verticalPoints.forEach(coord => {
|
|
96
|
+
drawMarker({x: coord[0], y: coord[1], horizontal: false });
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
horizontalPoints.forEach(coord => {
|
|
100
|
+
drawMarker({x: coord[0], y: coord[1], horizontal: true });
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
const drawThing = () => {
|
|
106
|
+
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
const draw = () => {
|
|
110
|
+
canvas.width = window.innerWidth;
|
|
111
|
+
canvas.height = window.innerHeight;
|
|
112
|
+
|
|
113
|
+
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
|
114
|
+
drawMarkers();
|
|
115
|
+
drawThing();
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
draw();
|
|
119
|
+
|
|
120
|
+
window.addEventListener('resize', draw);
|
package/src/BaseNode.js
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
const listenable = require("./util/listenable");
|
|
2
|
+
const InternalGameNode = require('./InternalGameNode');
|
|
3
|
+
|
|
4
|
+
const gameNode = (color, onClick, coordinates2d, border, fill, text, asset, playerIds, effects, input, subtype, id) => {
|
|
5
|
+
const node = new InternalGameNode(color, onClick, coordinates2d, border, fill, text, asset, playerIds, effects, input, subtype, id);
|
|
6
|
+
return listenable(node, node.onStateChange.bind(node));
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
class BaseNode {
|
|
10
|
+
constructor({color, onClick, coordinates2d, border, fill, textInfo, assetInfo, playerIds, effects, input, subtype, node, id}) {
|
|
11
|
+
if (node) {
|
|
12
|
+
this.node = node;
|
|
13
|
+
} else {
|
|
14
|
+
this.node = gameNode(color, onClick, coordinates2d, border, fill, textInfo, assetInfo, playerIds, effects, input, subtype, id);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
this.id = this.node.id;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
showFor(playerId) {
|
|
21
|
+
const playerIdIndex = this.node.playerIds.indexOf(playerId);
|
|
22
|
+
const zeroIndex = this.node.playerIds.indexOf(0);
|
|
23
|
+
|
|
24
|
+
if (zeroIndex >= 0) {
|
|
25
|
+
const newPlayerIds = this.node.playerIds;
|
|
26
|
+
newPlayerIds.splice(zeroIndex, 1);
|
|
27
|
+
this.node.playerIds = newPlayerIds;
|
|
28
|
+
}
|
|
29
|
+
if (playerIdIndex < 0) {
|
|
30
|
+
const newPlayerIds = this.node.playerIds;
|
|
31
|
+
newPlayerIds.push(playerId);
|
|
32
|
+
this.node.playerIds = newPlayerIds;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
hideFor(playerId) {
|
|
37
|
+
const playerIdIndex = this.node.playerIds.indexOf(playerId);
|
|
38
|
+
if (playerIdIndex >= 0) {
|
|
39
|
+
let newPlayerIds = this.node.playerIds;
|
|
40
|
+
newPlayerIds.splice(playerIdIndex, 1);
|
|
41
|
+
if (newPlayerIds.length == 0) {
|
|
42
|
+
newPlayerIds = [0];
|
|
43
|
+
}
|
|
44
|
+
this.node.playerIds = newPlayerIds;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
addChild(child) {
|
|
49
|
+
this.node.addChild(child);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
addChildren(...nodes) {
|
|
53
|
+
for (let nodeIndex = 0; nodeIndex < nodes.length; nodeIndex++) {
|
|
54
|
+
this.addChild(nodes[nodeIndex]);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
getChildren() {
|
|
59
|
+
return this.node.children;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
removeChild(nodeId) {
|
|
63
|
+
this.node.removeChild(nodeId);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
addListener(listener) {
|
|
67
|
+
this.node.addListener(listener);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
findChild(id) {
|
|
71
|
+
return this.#findChildHelper(id, this);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
#findChildHelper(nodeId, node, found = null) {
|
|
75
|
+
if (node.node.id === nodeId) {
|
|
76
|
+
found = node;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
for (const i in node.node.children) {
|
|
80
|
+
if (found) {
|
|
81
|
+
return found
|
|
82
|
+
}
|
|
83
|
+
found = this.#findChildHelper(nodeId, node.node.children[i], found);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
return found;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
clearChildren(excludedNodeIds) {
|
|
91
|
+
this.node.clearChildren(excludedNodeIds);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
clone() {
|
|
96
|
+
throw new Error("Clone not implemented");
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
module.exports = {gameNode, BaseNode};
|
package/src/Game.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
class Game {
|
|
2
2
|
constructor() {
|
|
3
3
|
this.players = {};
|
|
4
|
+
this.spectators = {};
|
|
4
5
|
this.listeners = new Set();
|
|
5
|
-
this.root = null;
|
|
6
6
|
this.intervals = [];
|
|
7
7
|
this.timeouts = [];
|
|
8
8
|
}
|
|
@@ -23,8 +23,20 @@ class Game {
|
|
|
23
23
|
this.listeners.remove(listener);
|
|
24
24
|
}
|
|
25
25
|
|
|
26
|
-
|
|
27
|
-
|
|
26
|
+
findNode(id) {
|
|
27
|
+
let found = null;
|
|
28
|
+
|
|
29
|
+
if (this.getLayers) {
|
|
30
|
+
const layers = this.getLayers();
|
|
31
|
+
for (let layerIndex in layers) {
|
|
32
|
+
found = layers[layerIndex].root.findChild(id);
|
|
33
|
+
if (found) {
|
|
34
|
+
break;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
return found;
|
|
28
40
|
}
|
|
29
41
|
|
|
30
42
|
setInterval(fun, interval) {
|