Commit in batoru/src/games/batoru/entities on MAIN | |||
TurretEntity.java | +76 | -16 | 1.2 -> 1.3 |
Entities now know how to render themselves.
diff -u -r1.2 -r1.3 --- TurretEntity.java 18 Nov 2003 11:07:06 -0000 1.2 +++ TurretEntity.java 1 Dec 2003 22:48:21 -0000 1.3 @@ -3,10 +3,20 @@
*/ package games.batoru.entities;
+import games.batoru.EntityBuilder; +
import java.util.Iterator; import javax.vecmath.*;
+import net.java.games.jogl.GL; +import net.java.games.jogl.GLU; +import net.java.games.jogl.GLUquadric; +import net.java.games.jogl.util.GLUT; + +import org.codejive.utils4gl.GLColor; +import org.codejive.utils4gl.RenderContext; +import org.codejive.utils4gl.Vectors;
import org.codejive.world3d.*; /**
@@ -18,7 +28,7 @@
private float m_fWaitHeartbeat = 0; private float m_fLastFired = 0;
- private Matrix3f m_barrelTrans = new Matrix3f();
+ private Vector3f m_barrelDir = new Vector3f();
private Entity m_target = null;
@@ -30,24 +40,38 @@
static final private float TARGET_RADIUS = 45.0f;
- static final private int WAIT_TIME = 2000; // 2 seconds - static final private int FIRE_TIME = 1000; // 2 seconds
+ static final private float WAIT_TIME = 2.0f; // 2 seconds + static final private float FIRE_TIME = 1.0f; // 2 seconds
// Only here to improve speed private Vector3f m_tmpVect = new Vector3f(); private Matrix3f m_tmpTrans = new Matrix3f();
- public TurretEntity(Universe _universe, EntityClass _class, Point3f _position) { - super(_universe, _class, _position, 0.0f); - m_barrelTrans.setIdentity();
+ private static final GLColor m_baseColor = new GLColor(.5f, .5f, .5f); + private static final GLColor m_boxColor = new GLColor(0, 0, .9f); + private static final GLColor m_barrelColor = new GLColor(.5f, 0, 0); + + private static final Point3f m_basePos = new Point3f(0.0f, 0.0f, 0.0f); + private static final Vector3f m_topPos = new Vector3f(0.0f, 1.6f, 0.0f); + private static final Point3f m_boxPos = new Point3f(0.0f, 0.0f, 0.0f); + private static final Vector3f m_barrelPos = new Vector3f(1.0f, 0.0f, 0.0f); + + public TurretEntity() { + super(); + m_barrelDir.set(Vectors.VECTF_IN); + } + + public TurretEntity(Universe _universe, Point3f _position) { + super(_universe, _position, 0.0f); + m_barrelDir.set(Vectors.VECTF_IN);
} public int getState() { return m_nState; }
- public Matrix3f getBarrelOrientation() { - return m_barrelTrans;
+ public Vector3f getBarrelOrientation() { + return m_barrelDir;
} public void heartbeat(float _fTime) {
@@ -58,9 +82,9 @@
switch (m_nState) { case STATE_IDLE: m_tmpTrans.rotY(IDLE_ROTATION_SPEED * fSecs);
- m_barrelTrans.mul(m_tmpTrans);
+ m_tmpTrans.transform(m_barrelDir);
- i = m_universe.getLiveEntitiesWithinRadius(getPosition(), TARGET_RADIUS, PlayerClass.class, true);
+ i = m_universe.getLiveEntitiesWithinRadius(getPosition(), TARGET_RADIUS, PlayerEntity.class, true);
if (i.hasNext()) { m_nState = STATE_TRACKING; m_target = (Entity)i.next();
@@ -72,6 +96,15 @@
m_tmpVect.set(m_target.getPosition()); m_tmpVect.sub(getPosition()); m_tmpVect.normalize();
+ m_barrelDir.set(m_tmpVect); + + if ((_fTime - m_fLastFired) >= FIRE_TIME) { + Universe.log(this, "Shot fired @" + m_target.toString()); + Point3f p = (Point3f)getPosition().clone(); + p.y += 1.6f; + Entity bullet = EntityBuilder.createBullet(m_universe, p, m_tmpVect, 20.0f, 5.0f); + m_fLastFired = _fTime; + }
/* Point3d q = new Point3d(getPosition()); Vector3d v = new Vector3d(m_tmpVect);
@@ -122,7 +155,7 @@
break; case STATE_WAITING: if ((_fTime - m_fWaitHeartbeat) <= WAIT_TIME) {
- i = m_universe.getLiveEntitiesWithinRadius(getPosition(), TARGET_RADIUS, PlayerClass.class, true);
+ i = m_universe.getLiveEntitiesWithinRadius(getPosition(), TARGET_RADIUS, PlayerEntity.class, true);
if (i.hasNext()) { m_nState = STATE_TRACKING; m_target = (Entity)i.next();
@@ -138,10 +171,37 @@
m_fLastHeartbeat = _fTime; }
- /* (non-Javadoc) - * @see test.Entity#terminateEntity() - */ - public void terminateEntity() { - m_universe.removeLiveEntity(this);
+ public void render(RenderContext _context) { + GL gl = _context.getGl(); + GLU glu = _context.getGlu(); + GLUT glut = _context.getGlut(); + + gl.glPushMatrix(); + gl.glColor3fv(m_baseColor.toArray3f()); + gl.glTranslatef(m_basePos.x, m_basePos.y, m_basePos.z); + gl.glRotatef(-90.0f, 1.0f, 0.0f, 0.0f); + glut.glutSolidCone(glu, 0.5f, 1.6f, 12, 1); + gl.glPopMatrix(); + + gl.glPushMatrix(); + gl.glTranslatef(m_topPos.x, m_topPos.y, m_topPos.z); + Vector3f orientation = getBarrelOrientation(); + glu.gluLookAt(0, 0, 0, orientation.x, orientation.y, orientation.z, 0, 1, 0); + gl.glPushMatrix(); + gl.glColor3fv(m_boxColor.toArray3f()); + gl.glTranslatef(m_boxPos.x, m_boxPos.y, m_boxPos.z); + gl.glScalef(1.0f, 0.4f, 0.4f); + glut.glutSolidCube(gl, 1.0f); + gl.glPopMatrix(); + + gl.glPushMatrix(); + gl.glColor3fv(m_barrelColor.toArray3f()); + gl.glTranslatef(m_barrelPos.x, m_barrelPos.y, m_barrelPos.z); + gl.glRotatef(-90.0f, 0.0f, 1.0f, 0.0f); + GLUquadric quad = glu.gluNewQuadric(); + glu.gluCylinder(quad, 0.1d, 0.1d, 0.8d, 6, 1); + glu.gluDeleteQuadric(quad); + gl.glPopMatrix(); + gl.glPopMatrix();
} }