Last active
December 24, 2018 20:38
-
-
Save Eggbertx/b73cf72a3e311c822a37429089a841a4 to your computer and use it in GitHub Desktop.
The miniSphere Personlib module
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * PersonLib 1.1.2018-07-09 | |
| * Copyright (c) 2016-2018, Eggbertx | |
| * All rights reserved. | |
| * | |
| * Redistribution and use in source and binary forms, with or without | |
| * modification, are permitted provided that the following conditions are met: | |
| * | |
| * 1 Redistributions of source code must retain the above copyright notice, this | |
| * list of conditions and the following disclaimer. | |
| * | |
| * 2 Redistributions in binary form must reproduce the above copyright notice, | |
| * this list of conditions and the following disclaimer in the documentation | |
| * and/or other materials provided with the distribution. | |
| * | |
| * 3 Neither the name of miniSphere nor the names of its contributors may be used | |
| * to endorse or promote products derived from this software without specific | |
| * prior written permission. | |
| * | |
| * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
| * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
| * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
| * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | |
| * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
| * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |
| * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | |
| * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | |
| * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
| * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| */ | |
| import { Thread, Console } from 'sphere-runtime'; | |
| function LegacyToNewColor(o) { | |
| return new Color(o.red / 255, o.green / 255, o.blue / 255, o.alpha / 255); | |
| } | |
| function NewToLegacyColor(n) { | |
| return CreateColor(n.r * 255, n.g * 255, n.b * 255, n.a * 255); | |
| } | |
| export function GeneratePersonObjects(arr, ignore) { | |
| // if ignore, arr = list of people not to use | |
| var objects = []; | |
| var personlist = GetPersonList(); | |
| if(ignore) { | |
| for(let person of personlist) | |
| if(arr.indexOf(person) == -1) | |
| objects.push(new Person(person, "", true, true)); | |
| } else { | |
| for(let person of personlist) | |
| if(arr.indexOf(person) > -1) | |
| objects.push(new Person(person, "", true, true)); | |
| } | |
| return objects; | |
| } | |
| function oldToNew(o) { | |
| alpha = 1; | |
| if(o.alpha) alpha = o.alpha; | |
| return new Color( | |
| Math.round(o.red * 100) / 100, | |
| Math.round(o.green * 100) / 100, | |
| Math.round(o.blue * 100) / 100, | |
| Math.round(alpha * 100) / 100 | |
| ); | |
| } | |
| function newToOld(n) { | |
| var alpha = 255; | |
| if(n.a) alpha = o.a; | |
| return CreateColor( | |
| Math.floor(n.r * 255), | |
| Math.floor(n.g * 255), | |
| Math.floor(n.g * 255), | |
| Math.floor(alpha * 255) | |
| ); | |
| } | |
| export class Person extends Thread { | |
| get [Symbol.toStringTag]() { return "Person"; } | |
| constructor(name,spriteset,destroy_with_map,force) { | |
| super(); | |
| if(DoesPersonExist(name) && !force) { | |
| throw "\"" + name + "\" already exists. Duplicate names are not allowed unless `force` is true."; | |
| } else { | |
| CreatePerson(name, spriteset, destroy_with_map); | |
| } | |
| this.name = name; | |
| this.alive = true; | |
| this.start(); | |
| } | |
| destroy(killthread) { | |
| this.alive = false; | |
| if(DoesPersonExist(this.name)) DestroyPerson(this.name); | |
| this.stop(); | |
| } | |
| getOffsetX() { | |
| return GetPersonOffsetX(this.name); | |
| } | |
| setOffsetX(offset) { | |
| SetPersonOffsetX(this.name, offset); | |
| return this; | |
| } | |
| getOffsetY() { | |
| return GetPersonOffsetY(this.name); | |
| } | |
| setOffsetY(offset) { | |
| SetPersonOffsetY(this.name, offset); | |
| return this; | |
| } | |
| getX() { | |
| return GetPersonX(this.name); | |
| } | |
| setX(x) { | |
| SetPersonX(this.name, x); | |
| return this; | |
| } | |
| getY() { | |
| return GetPersonY(this.name); | |
| } | |
| setY(y) { | |
| SetPersonY(this.name,y); | |
| return this; | |
| } | |
| getLayer() { | |
| return GetPersonLayer(this.name); | |
| } | |
| setLayer(layer) { | |
| SetPersonLayer(this.name, layer); | |
| return this; | |
| } | |
| getXFloat() { | |
| return GetPersonXFloat(this.name); | |
| } | |
| getYFloat() { | |
| return GetPersonYFloat(this.name); | |
| } | |
| setXYFloat(x,y) { | |
| SetPersonXYFloat(this.name, x, y); | |
| return this; | |
| } | |
| getPosition() { | |
| return {x: this.getX(), y: this.getY()}; | |
| } | |
| getPositionFloat() { | |
| return {x: this.getXFloat(), y: this.getYFloat()}; | |
| } | |
| setPosition() { | |
| // can take x and y numbers or object with x/y properties | |
| if(arguments.length == 1) { | |
| SetPersonX(this.name, arguments[0].x); | |
| SetPersonY(this.name, arguments[0].y); | |
| } else { | |
| SetPersonX(this.name, arguments[0]); | |
| SetPersonY(this.name, arguments[1]); | |
| } | |
| return this; | |
| } | |
| setPositionFloat() { | |
| // can take x and y numbers or object with x/y properties | |
| if(arguments.length == 1) { | |
| SetPersonXYFloat(this.name, arguments[0].x, arguments[0].y); | |
| } else { | |
| SetPersonXYFloat(this.name, arguments[0], arguments[1]); | |
| } | |
| return this; | |
| } | |
| getDirection() { | |
| return GetPersonDirection(this.name); | |
| } | |
| setDirection(dir) { | |
| SetPersonDirection(this.name, dir); | |
| return this; | |
| } | |
| getFrame() { | |
| return GetPersonFrame(this.name); | |
| } | |
| setFrame(frame) { | |
| SetPersonFrame(this.name,frame); | |
| return this; | |
| } | |
| getSpeedX() { | |
| return GetPersonSpeedX(this.name); | |
| } | |
| getSpeedY() { | |
| return GetPersonSpeedY(this.name); | |
| } | |
| getSpeed() { | |
| return { | |
| x: this.getSpeedX(), | |
| y: this.getSpeedY() | |
| }; | |
| } | |
| setSpeed(speed) { | |
| SetPersonSpeed(this.name, speed); | |
| return this; | |
| } | |
| setSpeedXY() { | |
| if(arguments.length == 1) | |
| SetPersonSpeedXY(this.name, arguments[0].x, arguments[0].y); | |
| else SetPersonSpeedXY(this.name, arguments[0], arguments[1]); | |
| return this; | |
| } | |
| getFrameRevert() { | |
| return GetPersonFrameRevert(this.name); | |
| } | |
| setFrameRevert(delay) { | |
| SetPersonFrameRevert(this.name, delay); | |
| return this; | |
| } | |
| setScaleFactor() { | |
| if(arguments.length == 1) | |
| SetPersonScaleFactor(this.name, arguments[0].w, arguments[0].h); | |
| else SetPersonScaleFactor(this.name, arguments[0], arguments[1]); | |
| return this; | |
| } | |
| setScaleAbsolute() { | |
| if(arguments.length == 1) | |
| SetPersonScaleAbsolute(this.name, arguments[0].w, arguments[0].h); | |
| else SetPersonScaleAbsolute(this.name, arguments[0], arguments[1]); | |
| return this; | |
| } | |
| getSpriteset() { | |
| return GetPersonSpriteset(this.name); | |
| } | |
| setSpriteset(spriteset) { | |
| SetPersonSpriteset(this.name, spriteset); | |
| return this; | |
| } | |
| getBase(base) { | |
| return GetPersonBase(this.name); | |
| } | |
| getAngle() { | |
| return GetPersonAngle(this.name) | |
| } | |
| setAngle(angle) { | |
| SetPersonAngle(this.name, angle); | |
| return this; | |
| } | |
| getMask(minisphere) { | |
| // if minisphere is true, return miniSphere Color object | |
| var mask = GetPersonMask(this.name); | |
| if(minisphere) return LegacyToNewColor(mask); | |
| else return mask; | |
| } | |
| setMask(color) { | |
| if(color instanceof Color) | |
| SetPersonMask(this.name, NewToLegacyColor(color)); | |
| else SetPersonMask(this.name, color); | |
| return this; | |
| } | |
| isVisible() { | |
| return IsPersonVisible(this.name); | |
| } | |
| setVisibility(visible) { | |
| SetPersonVisible(this.name, visible); | |
| return this; | |
| } | |
| toggleVisibility() { | |
| SetPersonVisible(this.name, !this.isVisible()); | |
| return this; | |
| } | |
| getData() { | |
| return GetPersonData(this.name); | |
| } | |
| setData(data) { | |
| SetPersonData(this.name, data); | |
| return this; | |
| } | |
| getValue(key) { | |
| return GetPersonValue(this.name, key); | |
| } | |
| setValue(key, value) { | |
| SetPersonValue(this.name, key, value); | |
| return this; | |
| } | |
| followPerson(leader, pixels) { | |
| FollowPerson(this.name, leader, pixels); | |
| return this; | |
| } | |
| setScript(which, script) { | |
| SetPersonScript(this.name, which, script); | |
| return this; | |
| } | |
| callScript(which) { | |
| CallPersonScript(this.name, which); | |
| return this; | |
| } | |
| queueCommand(command, immediate) { | |
| QueuePersonCommand(this.name, command, immediate); | |
| return this; | |
| } | |
| queueScript(script, immediate) { | |
| QueuePersonScript(this.name, script, immediate); | |
| return this; | |
| } | |
| clearQueuedCommands() { | |
| ClearPersonCommands(this.name); | |
| return this; | |
| } | |
| clearCommands() { | |
| ClearPersonCommands(this.name); | |
| return this; | |
| } | |
| isCommandQueueEmpty() { | |
| return IsCommandQueueEmpty(this.name); | |
| } | |
| isObstructed(x, y) { | |
| return IsPersonObstructed(this.name,x,y); | |
| } | |
| getObstructingTile(x, y) { | |
| return GetObstructingTile(this.name, x, y) | |
| } | |
| getObstructingPerson(x, y) { | |
| return GetObstructingPerson(this.name, x, y) | |
| } | |
| getObstruction(x, y) { | |
| return {tile: this.getObstructingTile(x,y), person: this.getObstructingPerson(x,y)} | |
| } | |
| getAdjacentObstructions(offset) { | |
| return { | |
| north: this.getObstruction(this.getX(),this.getY()-offset), | |
| south: this.getObstruction(this.getX(),this.getY()+offset), | |
| west: this.getObstruction(this.getX()-offset,this.getY()), | |
| east: this.getObstruction(this.getX()+offset,this.getY()) | |
| }; | |
| } | |
| ignorePersonObstructions(ignore) { | |
| IgnorePersonObstructions(this.name,ignore); | |
| return this; | |
| } | |
| isIgnoringPersonObstructions() { | |
| return IsIgnoringPersonObstructions(thisname); | |
| } | |
| ignoreTileObstructions(ignore) { | |
| IgnoreTileObstructions(this.name,ignore); | |
| return this; | |
| } | |
| isIgnoringTileObstructions() { | |
| return IsIgnoringTileObstructions(this.name); | |
| } | |
| getIgnoreList() { | |
| return GetPersonIgnoreList(this.name); | |
| } | |
| setIgnoreList(list) { | |
| SetPersonIgnoreList(this.name, list); | |
| return this; | |
| } | |
| addToIgnoreList(person) { | |
| this.setIgnoreList(this.getIgnoreList().push(person)); | |
| return this; | |
| } | |
| removeFromIgnoreList(person) { | |
| var ignore_list = this.getIgnoreList(); | |
| var index = ignore_list.indexOf(person); | |
| if(index == -1) throw "Error: person \"" + person + "\" does not exist (removeFromIgnoreList)"; | |
| else { | |
| ignore_list.splice(index,1); | |
| this.setIgnoreList(ignore_list); | |
| } | |
| return this; | |
| } | |
| attachCamera() { | |
| AttachCamera(this.name); | |
| return this; | |
| } | |
| detachCamera() { | |
| DetachCamera(this.name); | |
| return this; | |
| } | |
| isCameraAttached() { | |
| return GetCameraPerson() == this.name; | |
| } | |
| attachInput() { | |
| AttachInput(this.name); | |
| return this; | |
| } | |
| detachInput() { | |
| DetachInput(this.name); | |
| return this; | |
| } | |
| isInputAttached() { | |
| return GetInputPerson() == this.name; | |
| } | |
| setMoveByTiles(yesno) { | |
| this.setValue("moveByTiles", true); | |
| return this; | |
| } | |
| getMoveByTiles() { | |
| return this.getValue("moveByTiles"); | |
| } | |
| exists() { | |
| return DoesPersonExist(this.name); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment