Files

163 lines
5.4 KiB
Markdown
Raw Permalink Normal View History

2017-01-27 01:19:31 -08:00
# Things
2016-11-26 01:53:15 -05:00
2017-01-27 01:19:31 -08:00
This is a general guide about in-game Things for all FullScreenShenanigans projects.
2016-03-08 17:48:58 -05:00
2017-01-27 01:19:31 -08:00
## Things
2016-03-06 22:18:54 -05:00
2017-01-27 01:19:31 -08:00
Every in-game object in GameStartr games is a Thing.
Things and their classes are generated using [ObjectMakr](https://github.com/FullScreenShenanigans/ObjectMakr), which generates classes based on the inheritance structure given to it by its module settings.
2016-03-06 22:18:54 -05:00
2017-01-27 01:19:31 -08:00
```javascript
const thing = FSP.objectMaker.make("MyThing");
```
2016-03-06 22:18:54 -05:00
2017-01-27 01:19:31 -08:00
Most of the basic properties, like `height`, `width`, and `opacity` are defined in the parent Thing class.
ObjectMakr's `make` is a shortcut to initializing a new instance of a Thing class and assigning properties to it in one step.
2016-03-06 22:18:54 -05:00
2017-01-27 01:19:31 -08:00
More can be read on ObjectMakr on its [Readme](https://github.com/FullScreenShenanigans/ObjectMakr/blob/master/README.md).
2016-03-06 22:18:54 -05:00
2017-01-27 01:19:31 -08:00
## Groups
Each Thing has a `.groupType` string defining which "group" of Things it belongs to.
These groups are stored within the GroupHoldr module, which contains an array or object for each group.
Things are added to their respective group when added to the game and removed when they die.
### Getting groups
The groups are accessible both by static name and via passing in a String.
2016-03-06 22:18:54 -05:00
```javascript
// Returns the Solid group
2017-01-27 01:19:31 -08:00
FSP.groupHolder.getGroup("Solid");
2016-03-06 22:18:54 -05:00
// Returns the first Solid
2017-01-27 01:19:31 -08:00
FSP.groupHolder.getGroup("Solid")[0];
2016-03-06 22:18:54 -05:00
```
More can be read on GroupHoldr on its [Readme](https://github.com/FullScreenShenanigans/GroupHoldr/blob/master/README.md).
2016-03-08 17:48:58 -05:00
2016-11-26 01:53:15 -05:00
## Physics
2016-03-08 17:48:58 -05:00
2016-11-26 01:53:15 -05:00
A Thing's placement on the screen is based on its `top`, `right`, `bottom`, and `left` properties.
These define the bounding box for a Thing.
A Thing is essentially a rectangle, with its height, width, and location defined by these properties.
The coordinate system in which these Things are placed has an origin at the top left corner (0,0).
2016-03-08 17:48:58 -05:00
There are a number of ways to move Things, all of which are done by manipulating these 4 properties.
2016-03-06 22:18:54 -05:00
```javascript
// Instantiates Things used in the following code samples.
2016-11-26 01:53:15 -05:00
const thing = FSP.GroupHolder.getGroup("Solid")[0];
const otherThing = FSP.GroupHolder.getGroup("Solid")[1];
2016-03-06 22:18:54 -05:00
```
2016-11-26 01:53:15 -05:00
Physics methods are stored under the `physics` member of a GameStartr instance.
2017-01-27 01:19:31 -08:00
### Shift
2016-03-08 17:48:58 -05:00
2017-01-27 01:19:31 -08:00
You can use the shift functions `shiftVert` and `shiftHoriz`.
These move the Thing up and down or left and right.
2016-03-08 17:48:58 -05:00
2016-03-06 22:18:54 -05:00
```javascript
// Shifts a Thing vertically by 1.
2016-11-26 01:53:15 -05:00
FSP.physics.shiftVert(thing, 1);
2016-03-06 22:18:54 -05:00
2017-01-27 01:19:31 -08:00
// Shifts a Thing horizontally by 1.
2016-11-26 01:53:15 -05:00
FSP.physics.shiftHoriz(thing, 1);
2016-03-06 22:18:54 -05:00
```
2017-01-27 01:19:31 -08:00
### Set
2016-03-08 17:48:58 -05:00
2016-03-06 22:18:54 -05:00
The `set` functions (`setTop`, `setRight`, `setBottom`, or `setLeft`) moves the Thing to line up with the bounding property.
2016-03-08 17:48:58 -05:00
2016-03-06 22:18:54 -05:00
```javascript
2017-01-27 01:19:31 -08:00
// Sets the top bound of a Thing to 10.
2016-11-26 01:53:15 -05:00
FSP.physics.setTop(thing, 10);
2016-03-06 22:18:54 -05:00
2017-01-27 01:19:31 -08:00
// Sets the left bound of a Thing to 10.
2016-11-26 01:53:15 -05:00
FSP.physics.setLeft(thing, 10);
2016-03-06 22:18:54 -05:00
2017-01-27 01:19:31 -08:00
// Sets the bottom bound of a Thing to 10.
2016-11-26 01:53:15 -05:00
FSP.physics.setBottom(thing, 10);
2016-03-06 22:18:54 -05:00
2017-01-27 01:19:31 -08:00
// Sets the right bound of a Thing to 10.
2016-11-26 01:53:15 -05:00
FSP.physics.setRight(thing, 10);
2016-03-06 22:18:54 -05:00
```
2017-01-27 01:19:31 -08:00
### Midpoints
2016-03-08 17:48:58 -05:00
2017-01-27 01:19:31 -08:00
It's possible to set the Thing's midpoint using `setMidX`, `setMidY`, and `setMid` (the latter of which sets both the `x` and `y` midpoints).
These functions make it so the Thing is centered on the given `x` and `y`.
2016-03-08 17:48:58 -05:00
2016-03-06 22:18:54 -05:00
```javascript
2017-01-27 01:19:31 -08:00
// Sets both x and y midpoints to 1 and 2, respectively.
2016-11-26 01:53:15 -05:00
FSP.physics.setMid(thing, 1, 2);
2016-03-06 22:18:54 -05:00
2017-01-27 01:19:31 -08:00
// The above is equivalent to the following 2 function calls.
2016-11-26 01:53:15 -05:00
FSP.physics.setMidX(thing, 1);
FSP.physics.setMidY(thing, 2);
2016-03-06 22:18:54 -05:00
```
2016-03-08 17:48:58 -05:00
2017-01-27 01:19:31 -08:00
Going along with setting midpoints, you can do that based on the midpoint of another Thing.
Using `setMidXObj`, `setMidYObj`, and `setMidObj` you can shift a Thing so that its horizontal and/or vertical midpoint(s) are centered on that of another Thing.
2016-03-08 17:48:58 -05:00
2016-03-06 22:18:54 -05:00
```javascript
2017-01-27 01:19:31 -08:00
// Aligns a Thing's horizontal and vertical midpoints with those of anotherThing.
2016-03-06 22:18:54 -05:00
// This is equivalent to the following 2 function calls combined.
2016-11-26 01:53:15 -05:00
FSP.physics.setMidObj(thing, otherThing);
2016-03-06 22:18:54 -05:00
2017-01-27 01:19:31 -08:00
// Aligns just the horizontal midpoint.
2016-11-26 01:53:15 -05:00
FSP.physics.setMidXObj(thing, otherThing);
2016-03-06 22:18:54 -05:00
2017-01-27 01:19:31 -08:00
// Aligns just the vertical midpoint.
2016-11-26 01:53:15 -05:00
FSP.physics.setMidYObj(thing, otherThing);
2016-03-06 22:18:54 -05:00
```
2017-01-27 01:19:31 -08:00
### Slide
2016-03-08 17:48:58 -05:00
`slideToX` or `slideToY` will slide a Thing toward a target `x` or `y`, while limiting the total distance allowed (distance computed from the Thing's original midpoint).
2016-03-06 22:18:54 -05:00
```javascript
2017-01-27 01:19:31 -08:00
// Slides a Thing to x position 10, moving a max distance of 5.
2016-11-26 01:53:15 -05:00
FSP.physics.slideToX(thing, 10, 5);
2016-03-06 22:18:54 -05:00
2017-01-27 01:19:31 -08:00
// Slides a Thing to y position 10, moving a max distance of 5.
2016-11-26 01:53:15 -05:00
FSP.physics.slideToY(thing, 10, 5);
2016-03-06 22:18:54 -05:00
```
2017-01-27 01:19:31 -08:00
2016-03-08 17:48:58 -05:00
## Opacity
2017-01-27 01:19:31 -08:00
Opacity defines how transparent a Thing is.
It's defined in the `opacity` property and ranges from 0 to 1 (0 = fully transparent, 1 = fully visible).
2016-03-08 17:48:58 -05:00
2016-03-06 22:18:54 -05:00
You can set a Thing's opacity using `setOpacity`.
2016-03-08 17:48:58 -05:00
2016-03-06 22:18:54 -05:00
```javascript
2017-01-27 01:19:31 -08:00
// Sets a Thing's opacity to 1.
2016-11-26 01:53:15 -05:00
FSP.physics.setOpacity(thing, 1);
2016-03-06 22:18:54 -05:00
2017-01-27 01:19:31 -08:00
// Sets a Thing's opacity to 0 (transparent).
2016-11-26 01:53:15 -05:00
FSP.physics.setOpacity(thing, 0);
2016-03-08 17:48:58 -05:00
```
2017-01-27 01:19:31 -08:00
## Adding Things
2016-03-08 17:48:58 -05:00
2016-11-26 01:53:15 -05:00
You're able to add Things to the game via `addThing`, specifying the `x` and `y` positions (again this is relative to the top left corner of the screen).
The `x` is where the Thing's `left` bound will be and the `y` is where the Thing's `top` bound will be.
You can add it using a GameStartr instance's `things` component and the class title for the Thing:
2016-03-08 17:48:58 -05:00
```javascript
// Adds a Thing to the game at position (1,2) using the class name.
2016-11-26 01:53:15 -05:00
const first = FSP.things.add("FenceWide", 1, 2);
2016-03-08 17:48:58 -05:00
// Adds a Thing to the right of another Thing.
2016-11-26 01:53:15 -05:00
const second = FSP.things.add("FenceWide", first.right, first.top);
2016-03-08 17:48:58 -05:00
// Adds a Thing below another Thing.
2016-11-26 01:53:15 -05:00
FSP.things.add("FenceWide", second.left, second.bottom);
2016-03-08 17:48:58 -05:00
```