Spherical forums

User-Made => Libraries => Topic started by: Eggbertx on July 19, 2017, 03:27:43 am

Title: Personlib 1.1
Post by: Eggbertx on July 19, 2017, 03:27:43 am
Update: I made some improvements to Personlib. All setter functions that don't normally have a return value now return the person object for method chaining (see updated example)


I finally got around to completing my Personlib library that I mentioned a while ago. The syntax is almost identical to the Sphere 1.x, with some changes to account for miniSphere's differences. It also makes good use of miniSphere's threading capabilities, and adds some convenience functions of its own. For example:

Code: (JavaScript) [Select]
// Cellscript.mjs 
install('@/lib', files('lib/personlib.mjs'));

// main.mjs
import { Person } from "personlib";

var player = new Person("player","player.rss",false); // similar arguments to CreatePerson()
player.on_update = function() {
this
.attachInput()
.attachCamera();
var adjacent = this.getAdjacentObstructions(5); // where 5 is the offset in pixels
var rand_r = Math.floor(Math.random());
var rand_g = Math.floor(Math.random());
var rand_b = Math.floor(Math.random());
var rand_a = Math.floor(Math.random());
this.setMask(new Color(rand_r, rand_g, rand_b, rand_a)); // setMask detects if the parameter is a miniSphere color object or a 1.x color object, and getMask allows you specify which one you want
if(adjacent.north.tile > -1 || adjacent.north.person != "") Sphere.abort("You are ded. RIP in pieces.");
else this.queueCommand(COMMAND_MOVE_NORTH, true);
return true;
}
In this example, player.on_update is optional, but if you do set on_update, on_render, etc, they need to be set immediately after the Person object is instantiated. Otherwise they won't be used.
Most of the object methods are just call the legacy function given the appropriate arguments, so if you run into issues, there's a decent chance that it's miniSphere's fault. If you suspect that isn't the case or you have some suggestions, let me know.
Title: Re: Personlib 0.5
Post by: Fat Cerberus on July 19, 2017, 10:00:38 am
Tip: If using ES6 with Cell:

Code: (javascript) [Select]

const { Person } = require('personlib');
var someone = new Person("some guy", 'guy.rss', false);
// etc...


Or .mjs module syntax:
Code: (javascript) [Select]

// at top of file
import { Person } from 'personlib';


edit: Wow, REALLY?  iOS 11 uses smart quotes?  I used to like iOS because it was much easier to type code with compared to Android, but the replacement with smart quotes kind of ruins it...
Title: Re: Personlib 0.5
Post by: DaVince on July 19, 2017, 12:26:25 pm
This looks really useful! The adjacent stuff can be useful for stuff like platform movement too. :)
Title: Re: Personlib 0.5
Post by: Eggbertx on July 19, 2017, 02:16:10 pm

Tip: If using ES6 with Cell:

Code: (javascript) [Select]

const { Person } = require('personlib');
var someone = new Person("some guy", 'guy.rss', false);
// etc...


Or .mjs module syntax:
Code: (javascript) [Select]

// at top of file
import { Person } from 'personlib';



I see, I've never really used Cell, but I should probably make use of that.


This looks really useful! The adjacent stuff can be useful for stuff like platform movement too. :)

The example has the COMMAND_MOVE_NORTH code because I originally started working on the library for while working on a SHMUP, but I had platforming in mind as well.
Title: Re: Personlib 0.5
Post by: Fat Cerberus on July 19, 2017, 04:36:07 pm
@Eggbert: By the way, Sphere v2 Color has components in [0,1] range (floating point), not (0,255) like v1.  That was done for consistency with GLSL, since miniSphere supports shaders.
Title: Re: Personlib 0.5
Post by: Eggbertx on July 19, 2017, 06:18:45 pm
Did the old Color object require an integer or did it allow floating point numbers?
Title: Re: Personlib 0.5
Post by: Fat Cerberus on July 19, 2017, 06:21:30 pm
It might have been integer before miniSphere 4.0, because that was the first version that was "Sphere v2" proper.  Before that the constructors were just a convenience to make v1-compatible objects.
Title: Re: Personlib 0.5
Post by: Eggbertx on July 19, 2017, 06:57:57 pm
I meant the v1 Color objects. Given most graphics programs use integers for the RGBA values, should it be safe to limit it to that?
Title: Re: Personlib 0.5
Post by: Fat Cerberus on July 19, 2017, 07:07:49 pm
v1 Color is integer-only, if you try to pass floats you'll just get black.  There's no way to distinguish the two in a constructor because in JS all numbers are IEEE double per the language definition.

As for why v2 uses float colors, it's because GLSL shaders also do (colors are passed as vec4) and it would be confusing for users just learning about shaders if colors are float in the shader but integer in Sphere.  So it's just a matter of consistency.
Title: Re: Personlib 0.5
Post by: Eggbertx on July 19, 2017, 08:41:25 pm
I was experimenting a little on jsfiddle and I figured out how to easily convert between the two without any number type issues (as far as I know)

Code: (JavaScript) [Select]

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);
    );
}
Title: Re: Personlib 0.6
Post by: Eggbertx on July 22, 2017, 03:47:04 am
Is it possible to detect whether or not a person will be destroyed with the map? I'm creating a function that will return an array of person objects based on the people already in the map, given an array of names to not include. If it isn't, does the map engine set destroy_with_map by default?
Title: Re: Personlib 0.6
Post by: Fat Cerberus on July 22, 2017, 10:32:30 am
All persons defined in the map file are transient and get destroyed on a map change:
https://github.com/fatcerberus/minisphere/blob/v4.7.0/src/minisphere/map_engine.c#L1084-L1085
(note false is passed here, because the meaning of the flag is reversed internally)

The only way to have destroy_with_map == true is to do it yourself, using CreatePerson().  I don't think there's any way to detect it programmatically, though, at least not without comparing the results of GetPersonList() before and after a map change.

edit: Hm, your v1/v2 color conversion code is still wrong.  It's actually a lot easier than what you have:
Code: (javascript) [Select]

v2Color = new Color(v1c.red / 255, v1c.green / 255, v1c.blue / 255, v1c.alpha / 255);
v1Color = CreateColor(v2c.r * 255, v2c.g * 255, v2c.b * 255, v2c.a * 255);


You don't have to check whether the alpha channel exists, because it always will.  And Math.round(o.red * 100) / 100 just gives you back the original value.
Title: Re: Personlib 1.0
Post by: Eggbertx on August 08, 2017, 10:06:17 pm
Alright, Personlib is now at 1.0 and should be pretty stable. I'm working on a game called MonoBomber, and it uses it without any issues. The GeneratePersonObjects function returns an array of Person objects from persons already loaded in the map, given an include list of specific persons to use, or an exclude list of specific persons not to use (like the player's object, for example). GeneratePersonObjects() is mostly complete, but I wouldn't depend on it quite yet. if the Person() force parameter is set to true, you can set the spriteset to an empty string, and it will assume that the person already exists.
Title: Re: Personlib 1.1
Post by: Eggbertx on July 10, 2018, 12:53:00 am
Bumping this thread for the update, for anyone who has been using Personlib and might find it useful/convenient.