//.............................................................................................................................................................
// S P R I T E C L A S S
//.............................................................................................................................................................
class sprite {
// GLOBAL VARIABLES..
PImage graph;
String graphFile;
int ID;
float x = 0;
float y = 0;
float xx, yy;
int z = 127; // prioridad de pintado.. cuanto mas baja mas tarde se pintará..
float angle;
float size = 100.0;
boolean deadStatus = false; // poner a true para matar este objeto..
boolean visible = true;
int flags = 0;
int alpha = 255;
// variables para la entidad fisica..
FCircle body_1 = null;
FBox body_2 = null;
FPoly body_3 = null;
FBody body = null;
// variable que indica el tipo de coordenadas del sprite..
int c_type = C_TYPE_NORMAL;
// si el objeto debe representarse en un scroll esta es la variable que indentifica al scroll..
scroll SCROLL_ID = null;
// centro virtual de rotacion para seteo de un centro diferente al fisico normal.-
int centerX = 0;
int centerY = 0;
// es draggable este sprite?
boolean drag = false;
// region donde pintar este sprite..
int region = 0;
// string con el que llamo a la función spriteCodeExtender() para simular el LOOP de Div..
String name = "";
// CONSTRUCTOR..
sprite(PImage graph_, int x_, int y_) {
modSpriteIdSprite_ ++;
ID = modSpriteIdSprite_;
x = x_;
y = y_;
graph = graph_;
}
// constructor..
sprite() {
}
//..............................
// FUNCTIONS..
void render() {
if (body != null) {
x = body.getX();
y = body.getY();
angle = degrees(body.getRotation());
}
//..
if (angle == 0.0) {
angle = 0.01;
}
blitter.pushMatrix();
blitter.imageMode(CORNER);
blitter.clip(regions.x[region], regions.y[region], regions.width[region], regions.height[region]);
blitter.imageMode(CENTER);
xx = x;
yy = y;
switch(c_type) {
case C_TYPE_NORMAL:
if (visible == true) {
blitter.translate(x, y);
}
break;
case C_TYPE_SCROLL:
if (visible == true) {
blitter.translate( x + SCROLL_ID.x, y + SCROLL_ID.y );
}
break;
}
blitter.rotate(radians(angle));
switch(flags) {
case 0:
blitter.scale(size/100.0);
break;
case 1:
blitter.scale(-size/100.0, size/100);
break;
}
if (visible == true) {
blitter.tint(255, alpha);
blitter.image(graph, (centerX*size)/100, (centerY*size)/100); // Draw image using CENTER mode
}
blitter.popMatrix();
blitter.noClip();
}
//..............................
void loadGraph() {
graph = loadImage( graphFile );
}
//..............................
boolean delete() {
return(deadStatus);
}
//..............................
int getId() {
return(ID);
}
//..............................
//..............................
void advance(float dist_, float angle_) {
x = x + dist_*sin(radians(90.0 + -angle_));
y = y - dist_*cos(radians(90.0 + -angle_));
}
//..............................
void advance(float dist_) {
x = x + dist_*sin(radians(90.0 + angle));
y = y - dist_*cos(radians(90.0 + angle));
}
//..............................
void setCenter(int cx, int cy) {
centerX = graph.width/2 - cx;
centerY = graph.height/2 - cy;
}
//..............................
void setGrabbable(boolean flag) {
drag = flag;
}
//..............................
boolean getGrabbable() {
return drag;
}
//..............................
void kill(){
deadStatus = true;
}
//..............................
//----------------------------------------------------------------------------------------------------------
//-------------------------------PHYSICS METHODS------------------------------------------------------------
//----------------------------------------------------------------------------------------------------------
void physicsEntityBuild( int typeBody ) {
switch(typeBody) {
case TYPE_CIRCLE:
body = body_1;
body = new FCircle((graph.width * size) / 100);
break;
case TYPE_BOX:
body = body_2;
body = new FBox((graph.width * size) / 100, (graph.height * size) / 100);
break;
case TYPE_POLY:
//PShape s = spriteVectorice(graph, true);
PShape s = Vectorice(graph, true);
body_3 = new FPoly();
for(int i=0; i<s.getVertexCount(); i++) {
PVector v = s.getVertex(i);
body_3.vertex((int)v.x, (int)v.y);
}
body = body_3;
break;
}
body.setPosition(x, y);
body.setDensity(0.2);
body.setFill(120, 120, 190);
body.setNoStroke();
//body.setDrawable(false);
//body.setStatic(true);
body.setFriction(0.5);
body.setDensity(1);
body.setRestitution(0.7);
body.setName(str(ID));
body.setGrabbable(false);
world.add(body);
}
//..............................
void physicsEntitySetStatic( boolean static_ ) {
body.setStatic(static_);
}
//..............................
void physicsEntitySetMaterial( int material ) {
switch(material) {
case WOOD:
body.setFriction(0.50);
body.setRestitution(0.17);
body.setDensity(0.57);
break;
case METAL:
body.setFriction(0.13);
body.setRestitution(0.17);
body.setDensity(7-80);
break;
case STONE:
body.setFriction(0.75);
body.setRestitution(0.05);
body.setDensity(2.40);
break;
case PLASTIC:
body.setFriction(0.38);
body.setRestitution(0.09);
body.setDensity(0.95);
break;
case RUBBER:
body.setFriction(0.75);
body.setRestitution(0.95);
body.setDensity(1.70);
break;
case HUMAN:
body.setFriction(1.00);
body.setRestitution(0.00);
body.setDensity(0.95);
break;
}
}
//..............................
void physicsEntitySetDamping(float damping) {
body.setDamping(damping);
}
//..............................
//..............................
void physicsEntitySetName(String name) {
body.setName(name);
}
//..............................
void physicsEntityAddImpulse(float angle, float impulse) {
float fx = impulse*sin(radians(angle));
float fy = -impulse*cos(radians(angle));
body.addImpulse(fx, fy);
}
//..............................
void physicsEntityAddImpulse(float angle, float impulse, int offsetX, int offsetY) {
float fx = impulse*sin(radians(angle));
float fy = -impulse*cos(radians(angle));
body.addImpulse(fx, fy, -offsetX, -offsetY);
}
//..............................
void physicsEntityAddVx(int vx) {
body.addImpulse(vx, 0);
}
//..............................
void physicsEntityAddVy(int vy) {
body.addImpulse(0, vy);
}
//..............................
}