//CLASS OBJECT
import java.util.ArrayList;
import java.util.Random;
import org.lwjgl.opengl.GL11;
public class GameObject
{
private float x_,y_;//object world space coordinates
private float r_,g_,b_;//red, green, blue component colours
private ArrayList <GameObjectComponent> components_ =
new ArrayList<GameObjectComponent>();
private static int count_ = 0; // count of calls to constructor
/**
* This is the default constructor. This will set all your world attributes
* to some pre-determined values; i.e. x,y coordinates, colour.
*/
public GameObject()
{
initialise_();
float[][] data = {{400, 300, 0.5f, 0.5f, 1.0f}, // player
{100, 250, 0.5f, 0.25f, 0.5f}, // barrier1
{200, 500, 0.5f, 0.75f, 0.5f}}; // barrier2
x_ = data[count_][0];
y_ = data[count_][1];
r_ = data[count_][2];
g_ = data[count_][3];
b_ = data[count_][4];
count_++;
}
/**
* This constructor allows you to pre-set your GameObject's coordinates
*
* @param newX the new x coordinate
* @param newY the new y coordinate
*/
public GameObject(float newX, float newY)
{
initialise_();
x_ = newX;
y_ = newY;
}
/**
* This constructor allows you to pre-set your GameObject's coordinates
*
* @param newX the new x coordinate
* @param newY the new y coordinate
* @param newR the new value of the red component
* @param newG the new value of the green component
* @param newB the new value of the blue component
*/
public GameObject(float newX, float newY,
float newR, float newG, float newB )
{
initialise_();
x_ = newX;
y_ = newY;
setColour(newR,newG,newB);
}
/**
* This constructor allows you to pre-set your GameObject's coordinates
* and its colour.
*
* @param newX the new x coordinate
* @param newY the new y coordinate
* @param newR the new red component
* @param newG the new green component
* @param newB the new blue component
*/
public GameObject(double newX, double newY,
double newR,double newG, double newB )
{
initialise_();
x_ = (float)newX;
y_ = (float)newY;
setColour(newR,newG,newB);
}
public void setX(double newX)
{
x_ = (float)newX;
}
public void setY(double newY)
{
y_ = (float)newY;
}
public float getX()
{
return x_;
}
public float getY()
{
return y_;
}
public void setColour(double newR, double newG, double newB)
{
setColour((float)newR,(float)newG,(float)newB);
.
}
/**
* Sets the red, green and blue colour components all at once.
*
* @param newR the new red component
* @param newG the new green component
* @param newB the new blue component
*/
public void setColour(float newR,float newG, float newB)
{
float [] colArr = {newR,newG,newB};
r_ = newR;
g_ = newG;
b_ = newB;
//The r, g, b values need to be somewhere between 0 and 1.
//but there is another convention that allows them to be
//between 1 and 255. This takes care of that eventuality.
for(int i = 0; i < 3; i++)
{
if(colArr[i] > 1.0f)
{
if(colArr[i] <= 255)
{
setColour(i,(float)(colArr[i]/255.0f));
}
else
{
setColour(i,1f);
}
}
}
}
/**
public void setColour(int channel, double value)
{
setColour(channel,(float)value);
}
/**
* Sets the colour channels individually.
*
* @param channel 0 for red, 1 for green, 2 for blue.
* @param value this needs to be between 0 and 1
*/
public void setColour(int channel, float value)
{
if(value > 1f && value < 255f)
{
value = value/255f;
}
else if(value > 255f)
{
value = 1f;
}
switch(channel)
{
case 0:
{
r_ = value;
break;
}
case 1:
{
g_ = value;
break;
}
case 2:
{
b_ = value;
break;
}
}
}
/**
* Gets the colour value of a certain channel.
*
* @param channel 0 for red, 1 for green, 2 for blue
* @return the value of the specified colour channel
*/
public float getColour(int channel)
{
switch(channel)
{
case 0:
{
return r_;
}
case 1:
{
return g_;
}
case 2:
{
return b_;
}
}
return -1; // error value
}
/**
* Randomises the attributes of the GameObject
*
public void randomiseDeltas(int newSeed)
{
Random rnd = new Random();
rnd.setSeed(newSeed);
r_ = rnd.nextFloat();
g_ = rnd.nextFloat();
b_ = rnd.nextFloat();
}
/**
* Adds a component to the game object
* @param newComp the component to add
*/
public void addComponent(GameObjectComponent newComp)
{
components_.add(newComp);
//make sure the component itself knows who it belongs to
newComp.gameObject = this;
}
public void draw()
{
// draw quad
GL11.glPushMatrix();
GL11.glColor3f(r_, g_, b_);
GL11.glTranslatef(x_, y_, 0);
GL11.glTranslatef(-x_, -y_, 0);
GL11.glBegin(GL11.GL_QUADS);
GL11.glVertex2f(x_ - 50, y_ - 50);
GL11.glVertex2f(x_ + 50, y_ - 50);
GL11.glVertex2f(x_ + 50, y_ + 50);
GL11.glVertex2f(x_ - 50, y_ + 50);
GL11.glEnd();
GL11.glPopMatrix();
}
/**
* This function is responsible for object movement, rotations and
* other object behaviours. This function should ideally be called
* once per frame. But that depends on the engine that's using it.
*
public void update(int delta)
{
//move the object, step by step
//x += dx * delta;
//y += dy * delta;
//now run through all the components and do them
for(int i = 0; i < components_.size(); i++)
{
components_.get(i).update(delta);
}
}
private void initialise_()
{
x_ = 0f;
y_ = 0f;
r_ = 1f;
g_ = 1f;
b_ = 1f;
}
}