Created
October 27, 2020 21:15
-
-
Save Eggbertx/e988454e0c5b6de17db31263903cab1b to your computer and use it in GitHub Desktop.
A semi-rewrite of my personlib script for miniSphere with more ES6 emphasis
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 2.0.2020-10-27 | |
| * Copyright (c) 2016-2020, 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 } 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(); | |
| } | |
| get offsetX() { | |
| return GetPersonOffsetX(this.name); | |
| } | |
| set offsetX(offset) { | |
| SetPersonOffsetX(this.name, offset); | |
| } | |
| get offsetY() { | |
| return GetPersonOffsetY(this.name); | |
| } | |
| set offsetY(offset) { | |
| SetPersonOffsetY(this.name, offset); | |
| } | |
| get x() { | |
| return GetPersonX(this.name); | |
| } | |
| set x(newX) { | |
| SetPersonX(this.name, newX); | |
| } | |
| get y() { | |
| return GetPersonY(this.name); | |
| } | |
| set y(newY) { | |
| SetPersonY(this.name, newY); | |
| } | |
| get layer() { | |
| return GetPersonLayer(this.name); | |
| } | |
| set layer(newLayer) { | |
| SetPersonLayer(this.name, newLayer); | |
| } | |
| get xFloat() { | |
| return GetPersonXFloat(this.name); | |
| } | |
| get yFloat() { | |
| return GetPersonYFloat(this.name); | |
| } | |
| get position() { | |
| return {x: this.x, y: this.y}; | |
| } | |
| set position(pos) { | |
| SetPersonX(pos.x); | |
| SetPersonY(pos.y); | |
| } | |
| get positionFloat() { | |
| return {x: this.xFloat, y: this.yFloat}; | |
| } | |
| set positionFloat(pos) { | |
| SetPersonXYFloat(pos.x, pos.y) | |
| } | |
| get direction() { | |
| return GetPersonDirection(this.name); | |
| } | |
| set direction(dir) { | |
| SetPersonDirection(this.name, dir); | |
| } | |
| getFrame() { | |
| return GetPersonFrame(this.name); | |
| } | |
| set frame(newFrame) { | |
| SetPersonFrame(this.name, newFrame); | |
| } | |
| get speedX() { | |
| return GetPersonSpeedX(this.name); | |
| } | |
| set speedX() { | |
| SetPersonSpeedXY() | |
| } | |
| getSpeedY() { | |
| return GetPersonSpeedY(this.name); | |
| } | |
| getSpeed() { | |
| return { | |
| x: this.speedX, | |
| y: this.speedY | |
| }; | |
| } | |
| set speed(newSpeed) { | |
| SetPersonSpeed(this.name, newSpeed); | |
| } | |
| 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); | |
| } | |
| get angle() { | |
| return GetPersonAngle(this.name); | |
| } | |
| set angle(newAngle) { | |
| SetPersonAngle(this.name, newAngle); | |
| } | |
| get mask() { | |
| return LegacyToNewColor(GetPersonMask(this.name)); | |
| } | |
| set mask(newColor) { | |
| if(newColor instanceof Color) { | |
| SetPersonMask(this.name, NewToLegacyColor(newColor)); | |
| } else { | |
| SetPersonMask(this.name, newColor); | |
| } | |
| } | |
| get visibility() { | |
| return IsPersonVisible(this.name); | |
| } | |
| set visibility(visible) { | |
| SetPersonVisible(this.name, visible); | |
| } | |
| toggleVisibility() { | |
| SetPersonVisible(this.name, !this.isVisible()); | |
| } | |
| get data() { | |
| return GetPersonData(this.name); | |
| } | |
| set data(newData) { | |
| SetPersonData(this.name, data); | |
| } | |
| getValue(key) { | |
| return GetPersonValue(this.name, key); | |
| } | |
| setValue(key, value) { | |
| SetPersonValue(this.name, key, value); | |
| } | |
| 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); | |
| } | |
| queueScript(script, immediate) { | |
| QueuePersonScript(this.name, script, immediate); | |
| } | |
| queueCommand(command, immediate) { | |
| QueuePersonCommand(this.name, command, immediate); | |
| } | |
| clearQueuedCommands() { | |
| ClearPersonCommands(this.name); | |
| } | |
| clearCommands() { | |
| ClearPersonCommands(this.name); | |
| } | |
| 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); | |
| } | |
| get ignoreList() { | |
| return GetPersonIgnoreList(this.name); | |
| } | |
| set ignoreList(list) { | |
| SetPersonIgnoreList(this.name, list); | |
| } | |
| addToIgnoreList(person) { | |
| this.setIgnoreList(this.getIgnoreList().push(person)); | |
| } | |
| 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); | |
| } | |
| } | |
| get cameraAttached() { | |
| return GetCameraPerson() == this.name; | |
| } | |
| setCameraAttached(yesno) { | |
| if(yesno) { | |
| AttachCamera(this.name); | |
| } else { | |
| DetachCamera(this.name); | |
| } | |
| } | |
| get inputAttached() { | |
| return GetInputPerson() == this.name; | |
| } | |
| set inputAttached(attached) { | |
| if(attached) { | |
| AttachInput(this.name); | |
| } else { | |
| DetachInput(this.name); | |
| } | |
| } | |
| get moveByTiles(yesno) { | |
| return this.getValue("moveByTiles") == true; | |
| } | |
| set moveByTiles(yesno) { | |
| this.setValue("moveByTiles", yesno); | |
| } | |
| get exists() { | |
| return DoesPersonExist(this.name); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment