ccpgames / ccpwgl

CCP WebGL Library
MIT License
103 stars 30 forks source link

Class Utility Functions #264

Closed cppctamber closed 5 years ago

cppctamber commented 6 years ago

Hi @filipppavlov,

During fanfest you mentioned that there were new eve features (like explosions) that weren't currently possible in ccpwgl because we were missing deep clone functionalities.

If I create a system for adding the below methods to any class will that help?

isSerializable

A computed static class property which identifies if the class supports these utility functions

Tw2BaseClass.isSerializable

Assign

Assigns utility functions to a target class

Tw2BaseClass.assign(Class);

Clone

Clones a class instantiation

Tw2BaseClass.prototype.Clone();
Tw2BaseClass.clone(source)

Copy

Copies the values from one class instantiation to another

Tw2BaseClass.prototype.Copy(source);
Tw2BaseClass.copy(target, source);

GetValues

Gets a class instantiation's values as a plain object

Tw2BaseClass.prototype.GetValues(out);
Tw2BaseClass.get(source, out);
Tw2BaseClass._getValues(source, out) // For manually defining serialization

SetValues

Sets the values of a class instantiation from a plain object If a value isn't supplied it is not updated

Tw2BaseClass.prototype.SetValues(values);
Tw2BaseClass.set(out, values);
Tw2BaseClass._setValues(out, values) // For manually defining de-serialization

From

Create a class instantiation with values from a plain object

Tw2BaseClass.from(values);

Create

Create a class instantiation (probably not really needed if .from exists)

Tw2BaseClass.create)

DefineSchema

Creates Tw2BaseClass._getValues and Tw2BaseClass._setValues methods for a class from a plain object. Nice to have This could also be done with descriptors: https://github.com/tc39/ecma262*

// An example schema
Tw2BaseClass.defineSchema(Tw2Class, {

   props: {
       // Property value is of a set type
       name: 'String',  

       // Property value is a typed array of a set size (defaults to math library array type)                                                  
       transform: 'Matrix16',       

       // typed array of any length and a specific type                                     
       indices: 'Uint16Array',          

       // Property value is a specific class 
       effect: 'Tw2Effect',      

       // Property value can be one of the supplied classes                              
       mesh: [ 'Tw2Mesh', 'Tw2InstancedMesh' ], 

       // Property value is an array of a specific class
       spriteSets: [[ 'EveSpriteSet' ]]      

       // Property value is an array containing any of the supplied classes             
       meshAreas: [[ 'Tw2MeshArea', 'Tw2MeshLineArea' ]],

       // Complex prop definition
       someValue: {
           type: 'Number',
           min: 0,
           max: 10,
           onUpdate: function(parent, prop, value)
           {
                // Some custom function
           }
       }
   }
})

Let me know what you think and I'll get started.