// TheOthers.java import javax.microedition.lcdui.Graphics; import javax.microedition.lcdui.Image; import java.util.Vector; import java.util.Date; /** * This class accepts data from the server that defines where remote players should * be rendered, or if they have timedout , or if they need to be added as a new remote user. * It also defines the shared resources for all AnimationPoints (specifically sprite data). * * @author Jack Wootton * @version Last modified 10/03/05 **/ public class TheOthers { public final static int WALK_UP = 0; public final static int WALK_RIGHT = 1; public final static int WALK_DOWN = 2; public final static int WALK_LEFT = 3; private static Vector animPoints; // Used to keep track of connected players private static RemotePlayers rps; // Deals with remote player rendering private static boolean exists; /** * Static, so all AnimationPoints share image resources. * There could be a lot of remote players, so this is vital. */ public static Image[] playerWalkingUp, playerWalkingRight, playerWalkingDown, playerWalkingLeft; TheOthers() { // } public static void init() { animPoints = new Vector(); rps = new RemotePlayers(); Image playerImage = ImageSet.loadClippedImage("/MaleCharacterSprites.png", 0, 0); playerWalkingUp = ImageSet.extractFrames(playerImage, 0, 0, 3, 1, 17, 32); playerWalkingRight = ImageSet.extractFrames(playerImage, 0, 32, 3, 1, 17, 32); playerWalkingDown = ImageSet.extractFrames(playerImage, 0, 65, 3, 1, 17, 32); playerWalkingLeft = ImageSet.extractFrames(playerImage, 0, 96, 3, 1, 17, 32); } /** * @param newData = */ public static void update(String newData) { Date currentDate; if (rps.completed()) { String tmpData = new String(); int start = 0, end = newData.indexOf(';'); for (int i = 0; i < newData.length(); i++ ) { tmpData = newData.substring(start, end); String tmpId = tmpData.substring(0, 4); int firstCom = tmpData.indexOf(','); int secondCom = tmpData.lastIndexOf(','); int tmpX = Integer.parseInt( tmpData.substring(firstCom + 1, secondCom) ); int tmpY = Integer.parseInt( tmpData.substring(secondCom + 1, tmpData.length()) ); exists = false; for (int k = 0; k < animPoints.size() ; k++ ) { AnimationPoint ap = (AnimationPoint) animPoints.elementAt(k); if ( tmpId.equals( ap.getId() ) ) { ap.updatePoints(tmpX, tmpY); exists = true; // break; } long lastUpdated = ap.getLastPoll().getTime(); currentDate = new Date(); if ( ( (currentDate.getTime() - lastUpdated) / 1000) > 10) { animPoints.removeElementAt(k); } } currentDate = new Date(); if (! exists) { animPoints.addElement(new AnimationPoint(tmpX, tmpY, tmpId) ); } start = end + 1; end = newData.indexOf(';', start); if (end < 0) break; } } print(); } /* #DEBUG# */ public static void print() { AnimationPoint a; for (int i = 0; i < animPoints.size() ; i++ ) { a = (AnimationPoint) animPoints.elementAt(i); } } public static void paint(Graphics graphics) { rps.setVector(animPoints); rps.paint(graphics); } }