squishjs 1.0.6 → 1.0.8

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/README.md CHANGED
@@ -1,119 +1,59 @@
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.
1
+ ## Squish
2
+ This repo contains the game library used by homegames-core and homegames-web to efficiently serialize & deserialize game data.
2
3
 
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
+ ## squish/unsquish
5
+ This is just serialization/deserialization. Conceptually similar to [protobufs](https://protobuf.dev/), squish contains schema definitions shared by the server and client that lets us transmit game data as efficiently as possible.
4
6
 
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
102
7
  ```
103
- {
104
- filled: boolean
105
- north: boolean
106
- south: boolean
107
- east: boolean
108
- west: boolean
8
+ const { squish, unsquish, Colors, GameNode, Shapes, ShapeUtils } = require('squish-1006');
9
+ const node = new GameNode.Shape({
10
+ shapeType: Shapes.POLYGON,
11
+ fill: Colors.COLORS.RED,
12
+ coordinates2d: ShapeUtils.rectangle(20, 20, 10, 10)
13
+ });
14
+
15
+ const squished = squish(node);
16
+ [
17
+ 3, 0, 0, 53,
18
+ 2, 43, 0, 11,
19
+ [Number: 0], [Number: 0], [Number: 32], [Number: 61],
20
+ [Number: 3], [Number: 90], [Number: 63], [Number: 44],
21
+ 44, 0, 3, 53,
22
+ 0, 7, 255, 0,
23
+ 0, 255, 55, 0,
24
+ 4, 3, 52, 0,
25
+ 23, 20, 0, 20,
26
+ 0, 30, 0, 20,
27
+ 0, 30, 0, 30,
28
+ 0, 20, 0, 30,
29
+ 0, 20, 0, 20,
30
+ 0
31
+ ]
32
+
33
+ const unsquished = unsquish(squished);
34
+ Shape {
35
+ node: InternalGameNode {
36
+ id: [Number: 326103906344],
37
+ children: [],
38
+ color: undefined,
39
+ handleClick: undefined,
40
+ coordinates2d: [ [Array], [Array], [Array], [Array], [Array] ],
41
+ border: undefined,
42
+ fill: [ 255, 0, 0, 255 ],
43
+ text: undefined,
44
+ asset: undefined,
45
+ effects: null,
46
+ input: null,
47
+ listeners: Set(0) {},
48
+ playerIds: [],
49
+ subType: 3
50
+ },
51
+ id: [Number: 326103906344]
109
52
  }
110
53
  ```
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
54
 
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".
55
+ ## Game
56
+ The `Game` class is a base class that allows external components (eg. a `Squisher`) to listen to state updates. All games in Homegames extend the `Game` class.
118
57
 
119
- **west** of true means from this place a player can move "west".
58
+ ## Versioning
59
+ Homegames core & web use multiple versions of squish. This allows games using older versions of squish to remain compatible with newer versions of Homegames.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "squishjs",
3
- "version": "1.0.6",
3
+ "version": "1.0.8",
4
4
  "description": "squish & unsquish stuff",
5
5
  "scripts": {
6
6
  "test": "node testRunner.js"
package/src/BaseNode.js CHANGED
@@ -1,15 +1,15 @@
1
1
  const InternalGameNode = require('./InternalGameNode');
2
2
 
3
- const gameNode = (color, onClick, coordinates2d, border, fill, text, asset, playerIds, effects, input, subtype, id) => {
4
- return new InternalGameNode(color, onClick, coordinates2d, border, fill, text, asset, playerIds, effects, input, subtype, id);
3
+ const gameNode = (color, onClick, coordinates2d, border, fill, text, asset, playerIds, effects, input, subtype, id, onHover, offHover) => {
4
+ return new InternalGameNode(color, onClick, coordinates2d, border, fill, text, asset, playerIds, effects, input, subtype, id, onHover, offHover);
5
5
  };
6
6
 
7
7
  class BaseNode {
8
- constructor({color, onClick, coordinates2d, border, fill, textInfo, assetInfo, playerIds, effects, input, subtype, node, id}) {
8
+ constructor({color, onClick, coordinates2d, border, fill, textInfo, assetInfo, playerIds, effects, input, subtype, node, id, onHover, offHover }) {
9
9
  if (node) {
10
10
  this.node = node;
11
11
  } else {
12
- this.node = gameNode(color, onClick, coordinates2d, border, fill, textInfo, assetInfo, playerIds, effects, input, subtype, id);
12
+ this.node = gameNode(color, onClick, coordinates2d, border, fill, textInfo, assetInfo, playerIds, effects, input, subtype, id, onHover, offHover);
13
13
  }
14
14
 
15
15
  this.id = this.node.id;
package/src/GameNode.js CHANGED
@@ -15,7 +15,7 @@ const subtypeToShapeType = {
15
15
  };
16
16
 
17
17
  class Shape extends BaseNode {
18
- constructor({ color, onClick, shapeType, coordinates2d, border, fill, playerIds, effects, input, node, id }) {
18
+ constructor({ color, onClick, shapeType, coordinates2d, border, fill, playerIds, effects, input, node, id, onHover, offHover }) {
19
19
  if ((!coordinates2d || !shapeType) && !(node)) {
20
20
  throw new Error("Shape requires coordinates2d and shapeType");
21
21
  }
@@ -31,7 +31,9 @@ class Shape extends BaseNode {
31
31
  input,
32
32
  node,
33
33
  subtype: shapeTypeToSubtype[shapeType],
34
- id
34
+ id,
35
+ onHover,
36
+ offHover
35
37
  });
36
38
  }
37
39
 
@@ -81,7 +83,7 @@ class Text extends BaseNode {
81
83
  }
82
84
 
83
85
  class Asset extends BaseNode {
84
- constructor({ assetInfo, onClick, coordinates2d, playerIds, effects, node, id }) {
86
+ constructor({ assetInfo, onClick, coordinates2d, playerIds, effects, node, id, onHover, offHover }) {
85
87
  if (!assetInfo && !node) {
86
88
  throw new Error("Asset node requires assetInfo");
87
89
  }
@@ -94,7 +96,9 @@ class Asset extends BaseNode {
94
96
  effects,
95
97
  node,
96
98
  subtype: SUBTYPES.ASSET,
97
- id
99
+ id,
100
+ onHover,
101
+ offHover
98
102
  });
99
103
  }
100
104
 
@@ -1,6 +1,7 @@
1
1
  class InternalGameNode {
2
- constructor(color, onClick, coordinates2d, border, fill, text, asset, playerIds = [], effects = null, input = null, subType = null, id = null) {
3
- this.id = id ? Number(id) : Math.floor(Math.random() * 999999999999);
2
+ constructor(color, onClick, coordinates2d, border, fill, text, asset, playerIds = [], effects = null, input = null, subType = null, id = null, onHover, offHover) {
3
+ // im about to hit the ye button
4
+ this.id = id ? Number(id) : Math.floor(Math.random() * 999999999);
4
5
  this.children = new Array();
5
6
  this.color = color;
6
7
  this.handleClick = onClick;
@@ -17,6 +18,8 @@ class InternalGameNode {
17
18
  }
18
19
  this.playerIds = playerIds || [];
19
20
  this.subType = subType;
21
+ this.onHover = onHover;
22
+ this.offHover = offHover;
20
23
  }
21
24
 
22
25
  addChild(node) {