import java.util.*; // stringtokenizer import java.awt.*; import java.net.*; import java.awt.event.*; import java.io.*; public class aGame extends Panel implements Game, Runnable, KeyListener, MouseListener, ActionListener{ private boolean stop=false; // run cycle state Global global; // Variables etc. private int MAXWIDTH=1024; private int MAXHEIGHT=760; // GameObject playerobject; Color stepcolor=new Color(255,255,255); Color backgroundcolor=new Color(0,60,0); Image buffer; // Image to be drawn to the Frame (buffered drawing) public Graphics canvas; // Drawing graphics Thread thread; // One thread to rule them all; TextField hidden; // need to gain focus ImageButton button[]=new ImageButton[15]; // Keys stored in a stack // If key is served by the game we will accept new keys int keys[]=new int[1024]; int foundobjectnr=0; // Action on object begin applet; public static void main(String argv[]) { aGame A=new aGame(); A.runit(); } public void stop(){ } public void show(Graphics g){ } public void init(begin appl){ applet=appl; setLayout(null); // no layoutmanager! applet.add(this); runit(); } public void runit(){ global=new Global(this); // loadPlayer(); setBounds(0,0,1,1); setVisible(true); addMouseListener(this); hidden=new TextField(); // hidden.setBounds(1,1,1,1); // Netscape Communicator 4.79 can't handle negative or zero pos hidden.setBounds(MAXWIDTH-1,MAXHEIGHT-1,1,1); // we don't have other controls .. so this will be sufficient add(hidden); hidden.addKeyListener(this); hidden.requestFocus(); try{ thread=new Thread(this); thread.start(); }catch(Exception exc){ } } // public synchronized void run(){ public void run(){ // // DO THIS ONCE // button[0]=new ImageButton(global.buttonimage[0], "Levels",this, ""); button[0].setBounds(740,92,55,55); button[0].addActionListener(this); button[0].setVisible(false); add(button[0]); button[1]=new ImageButton(global.buttonimage[1], "undo",this, ""); button[1].setBounds(740,162,55,55); button[1].addActionListener(this); button[1].setVisible(false); add(button[1]); button[2]=new ImageButton(global.buttonimage[2], "restart",this, ""); button[2].setBounds(740,232,55,55); button[2].addActionListener(this); button[2].setVisible(false); add(button[2]); while(!stop){ // Let the buttons draw themselve if(global.State == global.DRAW && (global.getTime() % 100) > 80){ for(int buttonloop=0; buttonloop < 3; buttonloop++) if(button[buttonloop] != null){ if(!button[buttonloop].isVisible()) button[buttonloop].setVisible(true); // button[buttonloop].repaint(); } } // ===== // KeyChecking // ===== // do we need to put the new keys to the stack if(global.animationstack.isEmpty() && applet.loaded && global.State==global.DRAW){ if(keys[KeyEvent.VK_RIGHT]==1){ global.KeyStack.push(""+KeyEvent.VK_RIGHT); } if(keys[KeyEvent.VK_LEFT]==1){ global.KeyStack.push(""+KeyEvent.VK_LEFT); } if(keys[KeyEvent.VK_UP]==1){ global.KeyStack.push(""+KeyEvent.VK_UP); } if(keys[KeyEvent.VK_DOWN]==1){ global.KeyStack.push(""+KeyEvent.VK_DOWN); } }else{ //System.out.println("("+global.getTime()+")Debug: empty?"+global.animationstack.isEmpty()+" lowesttick: "+global.animationstack.lowesttick()); } if(!global.KeyStack.isEmpty()){ int animationframe=0; // Get Key and react on it Integer I=new Integer(0); try{ I=new Integer((String)global.KeyStack.next()); }catch(Exception exception){ I=new Integer(0);} int k=I.intValue(); // Increase Step if(global.playerx != global.playerxstored || global.playery != global.playerystored){ global.stepcounter++; global.playerxstored=global.playerx; global.playerystored=global.playery; } //System.out.println("Investigation of the key: "+k+"Global.TILESTEPS: "+global.TILESTEPS); switch(k){ case KeyEvent.VK_RIGHT: if(global.undocounter++ > 10000)global.undocounter=1; // zero is reserved for load global.dir=0; // playerobject.setMethod("right"); // playerobject.execute(); global.gameobject[global.usernr].setMethod("right"); global.gameobject[global.usernr].execute(); break; case KeyEvent.VK_LEFT: global.dir=2; if(global.undocounter++ > 10000)global.undocounter=1; // playerobject.setMethod("left"); // playerobject.execute(); global.gameobject[global.usernr].setMethod("left"); global.gameobject[global.usernr].execute(); break; case KeyEvent.VK_UP: global.dir=1; if(global.undocounter++ > 10000)global.undocounter=1; // playerobject.setMethod("up"); // playerobject.execute(); global.gameobject[global.usernr].setMethod("up"); global.gameobject[global.usernr].execute(); break; case KeyEvent.VK_DOWN: global.dir=3; if(global.undocounter++ > 10000)global.undocounter=1; // playerobject.setMethod("down"); // playerobject.execute(); global.gameobject[global.usernr].setMethod("down"); global.gameobject[global.usernr].execute(); break; } global.KeyStack.pop(); } // // Event loops on objects // for(int i=0; i < global.MAXOBJECTS; i++){ if(global.gameobject[i]!=null){ global.gameobject[i].setMethod("event"); global.gameobject[i].execute(); } } // // animationstack is the grid position that is done by program // It puts frames to the stack and each run in this loop // we get all the frames that need to be drawn in the // tickcounter on the screen. // if(!global.animationstack.isEmpty()){ global.animationstack.run(); } // These animation frames can be drawn // while player is moving if(!global.continuousanimation.isEmpty()){ global.continuousanimation.run(); } repaint(); try{ thread.sleep(global.sleep); }catch(Exception e){ } } } public void paint(Graphics g){ update(g); } public void update(Graphics g){ if(buffer==null) buffer=createImage(MAXWIDTH,MAXHEIGHT); if(buffer!=null) canvas=buffer.getGraphics(); //System.out.println("State: "+global.State); // What can we do? if(global.State==global.INIT){ // load global.init(); } // // Menu System // if(global.State > global.MENU){ int menunr=global.State-global.MENU; button[0].setVisible(false); button[1].setVisible(false); button[2].setVisible(false); if(global.menu[menunr]==null) global.menu[menunr]=global.loadImage(global.Server,"menus/menu"+menunr+".gif",this); setBounds(0,0,MAXWIDTH, MAXHEIGHT); applet.loaded=true; // tell progressionbar to stop hidden.requestFocus(); if(global.menu[menunr]==null) global.State = global.DRAW; else canvas.drawImage(global.menu[menunr],0,0,this); // Mouse Press!! } if(global.State==global.LOAD){ setBounds(0,0,1,1); applet.loaded=false; global.objectloadnr=-1; global.objectloadcounter=0; global.load(); global.objectloadnr=-1; } if(global.State== global.BEFOREDRAW){ global.firsttime=true; if(global.objectloadnr++ > global.objectloadcounter){ // // Do we a menu between each screen? // //if(global.level < 2){ global.State=global.MENU+global.level; //}else{ // global.State=global.DRAW; //} }else{ int oo=global.levelobjects[global.objectloadnr]; //System.out.println("loading object: "+oo); global.loadObject(oo); } // // BACKGROUND MUSIC // global.backgroundsound.play(); } if(global.State==global.DRAW && !global.changed){ // Show Score canvas.setColor(backgroundcolor); canvas.fillRect(740,0,1024,40); canvas.setColor(stepcolor); canvas.drawString("Step: "+global.stepcounter, 740,30); canvas.drawString("Level: "+global.level, 740,70); canvas.setColor(backgroundcolor); global.changed=true; setBounds(0,0,MAXWIDTH, MAXHEIGHT); applet.loaded=true; // tell progressionbar to stop hidden.requestFocus(); } if(global.changed && global.State==global.DRAW){ // Check if we are ready! // DUKE-- ELEMENT GAME // boolean found=false; for(int i=0; i < 4; i++){ Point y=global.grid.findobject(995+i); if((int)y.getY() != -1){ found=true; } } // level ready! if(!found){ nextlevel(); } // global.backgroundsound.play(); //System.out.println("Something has changed"); if(global.firsttime){ System.out.println(" *************** painting background ****************"); redrawall(); global.firsttime=false; hidden.requestFocus(); } // for each object in the background // for each player in the level // execute php retrieve in LAST ORDER so that the last character // is drawn FIRST ... (overlapping in right order) // At last the users position global.changed=false; } g.drawImage(buffer,0,0,this); } // // undo last action (until all actions in this level are done) // public void undo(){ if(!global.undostack.isEmpty()&& global.undocounter > 0){ // no more left ove animations! global.animationstack.clear(); // -------------------------------------- // You get a penalty for using UNDO // -------------------------------------- global.stepcounter+=10; int counter=global.undocounter; int y=0; int x=0; int obj=0; // pop all the items that we don't want to keep while(counter == global.undocounter && !global.undostack.isEmpty()){ String str=(String)global.undostack.pop(); StringTokenizer st=new StringTokenizer(str, ","); while(st.hasMoreTokens()){ counter=Integer.parseInt(st.nextToken()); y=Integer.parseInt(st.nextToken()); x=Integer.parseInt(st.nextToken()); obj=Integer.parseInt(st.nextToken()); } // while more tokens // we got one too many .. so push back if(counter != global.undocounter){ global.undostack.push(str); }else{ global._grid[y][x]=obj; if(obj > 999){ global.playerx=x*global.MAXFRAMES; global.playery=y*global.MAXFRAMES; global.usernr=obj; global.playerxstored=global.playerx; global.playerystored=global.playery; //// playerobject=global.gameobject[global.usernr]; } } } // while counter global.undocounter--; if(global.undocounter <1)global.undocounter=9999; // redraw all ! redrawall(); } // if there are undos } // updateImage is for animated gif public boolean imageUpdate( Image img, int flags, int x, int y, int w, int h ) { repaint(); return true; } // Redraw object /griditem public void redraw(int yy, int xx){ int calcx=0; int calcy=0; int o=global.grid.getobject(yy,xx); GameObject gameobject=global.gameobject[o]; Image partimage; Graphics partgraph; if(gameobject != null){ Image image=null; if( (image=gameobject.getImage(0,global.dir)) != null){ partimage=createImage(global.TILESIZE, global.TILESIZE); if(partimage == null){ System.out.println("Error in creating Image"); }else{ partgraph=partimage.getGraphics(); if(partgraph == null){ System.out.println("aGame.partgraph = null!"); }else{ calcx=-((xx/global.MAXFRAMES)*global.TILESIZE)-((xx%global.MAXFRAMES)*global.TILESTEPS); calcy=-((yy/global.MAXFRAMES)*global.TILESIZE)-((yy%global.MAXFRAMES)*global.TILESTEPS); partgraph.drawImage(global.background, calcx,calcy, this); partgraph.drawImage(image, 0,0, this); calcx=((xx/global.MAXFRAMES)*global.TILESIZE)+((xx%global.MAXFRAMES)*global.TILESTEPS); calcy=((yy/global.MAXFRAMES)*global.TILESIZE)+((yy%global.MAXFRAMES)*global.TILESTEPS); canvas.drawImage(partimage, calcx,calcy, this); } // if partgraph } // Partimage == null }else{ System.out.println("Image "+o+" does not exist!"); } } // if object not null else{ System.out.println("Gameobject: "+o+" does not exist!"); } } // Redraw object /griditem public void redrawframe(int yy, int xx, int obj, int dir, int framenr,int oldy, int oldx){ //int obj=global.grid.getobject(yy,xx); Image partimage=createImage(global.TILESIZE, global.TILESIZE); Graphics partgraph=partimage.getGraphics(); int calcx=0; int calcy=0; if(obj > 0){ GameObject gameobject=global.gameobject[obj]; if(gameobject != null){ //System.out.println("objec found"); Image image=null; if( (image=gameobject.getImage(framenr,dir)) != null){ //System.out.println("No empty image"); // where we have left (put background there) partimage=createImage(global.TILESIZE, global.TILESIZE); partgraph=partimage.getGraphics(); calcx=-((oldx/global.MAXFRAMES)*global.TILESIZE)-((oldx%global.MAXFRAMES)*global.TILESTEPS); calcy=-((oldy/global.MAXFRAMES)*global.TILESIZE)-((oldy%global.MAXFRAMES)*global.TILESTEPS); partgraph.drawImage(global.background, calcx, calcy, this); calcx=((oldx/global.MAXFRAMES)*global.TILESIZE)+((oldx%global.MAXFRAMES)*global.TILESTEPS); calcy=((oldy/global.MAXFRAMES)*global.TILESIZE)+((oldy%global.MAXFRAMES)*global.TILESTEPS); canvas.drawImage(partimage, calcx, calcy, this); //System.out.println("Background: "+calcx+"."+calcy); partimage=createImage(global.TILESIZE, global.TILESIZE); partgraph=partimage.getGraphics(); calcx=-((xx/global.MAXFRAMES)*global.TILESIZE)-((xx%global.MAXFRAMES)*global.TILESTEPS); calcy=-((yy/global.MAXFRAMES)*global.TILESIZE)-((yy%global.MAXFRAMES)*global.TILESTEPS); partgraph.drawImage(global.background, calcx, calcy, this); partgraph.drawImage(image, 0,0, this); //System.out.println("Background: "+calcx+"."+calcy); calcx=((xx/global.MAXFRAMES)*global.TILESIZE)+((xx%global.MAXFRAMES)*global.TILESTEPS); calcy=((yy/global.MAXFRAMES)*global.TILESIZE)+((yy%global.MAXFRAMES)*global.TILESTEPS); //System.out.println(" Draw location : "+calcx+","+calcy); //System.out.println(" Going to paint object on location: row:"+calcy+" col: "+calcx+": "+obj); canvas.drawImage(partimage, calcx, calcy, this); //System.out.println("Object: "+calcx+"."+calcy); } // if(getImage != null) } // if gameobject not null }// if obj > 1 } // redrawframe // // animation across multiple grids // public void redrawframe(int yy, int xx, int obj, int w, int h, int dir, int framenr,int oldy, int oldx){ Image partimage=createImage(global.TILESIZE*w, global.TILESIZE*h); Graphics partgraph=partimage.getGraphics(); Image image=null; int calcx=0; int calcy=0; if(obj>0){ GameObject gameobject=global.gameobject[obj]; if(gameobject!=null){ if( (image=global.gameobject[obj].getImage(framenr,dir)) != null){ // put background on the animation frame calcx=-((xx/global.MAXFRAMES)*global.TILESIZE)-((xx%global.MAXFRAMES)*global.TILESTEPS); calcy=-((yy/global.MAXFRAMES)*global.TILESIZE)-((yy%global.MAXFRAMES)*global.TILESTEPS); partgraph.drawImage(global.background, calcx, calcy, this); partgraph.drawImage(image, 0,0, this); calcx=((xx/global.MAXFRAMES)*global.TILESIZE)+((xx%global.MAXFRAMES)*global.TILESTEPS); calcy=((yy/global.MAXFRAMES)*global.TILESIZE)+((yy%global.MAXFRAMES)*global.TILESTEPS); //System.out.println("Drawing iamge: "+calcy+", "+calcx+", "+obj+" . h:"+h+"w:"+w+" framenr: "+framenr); canvas.drawImage(partimage, calcx, calcy, this); } // if image != null } // if gameobject 1= null } // if obj > 0 } // redrawframe (animatio public void redrawall(){ //System.out.println("canvas .. clear"); //setVisible(false); canvas.fillRect(0,0,1024,768); if(global.background!=null) canvas.drawImage(global.background, 0,0, this); for(int yy=0; yy0){ //System.out.println(" Going to paint object on location: row:"+yy+" col: "+xx+": "+o); redraw(yy,xx); //redrawframe( yy, xx, o, 0, 0,yy, xx); } // when we need to redraw } // for xx } // for yy //setVisible(true); hidden.requestFocus(); } // redrawall public void nextlevel(){ global.level++; global.State=global.LOAD; } public void loadPlayer(){ /* try{ Class c=Class.forName("object"+global.usernr); if(c != null){ //System.out.println("Going to newInstance .."); playerobject=(GameObject)c.newInstance(); if(playerobject!= null){ //System.out.println("We have an instance!"); playerobject.init(global); } } }catch(Exception e){} */ } // Key Routine // ---- // This method ensures that the key // is captured even if keyrepeat usually prevents it // // Smooth key capture is possible // public void keyPressed(KeyEvent ke){ ////// //System.out.println("KeyPressed: "+ke.toString()); // // This should be used in a game where the keys are continously used // Object reaction is very important and therefore we cannot use this // ----- // using keytime and also switches didn't resolve the problem // Too sensative key reaction is the result! // if(ke.getKeyCode() == KeyEvent.VK_BACK_SPACE){ undo(); } if(ke.getKeyCode() == 27){ global.State=global.LOAD; } if(global.animationstack.isEmpty()){ keys[ke.getKeyCode()&0xFF]=1; } // if nothing in animationstack } // --------------------- // // --------------------- public void keyReleased(KeyEvent ke){ keys[ke.getKeyCode()&0xFF]=0; } public void keyTyped(KeyEvent ke){ } // // // Mouse routine // public void mousePressed(MouseEvent me){ if(global.State > global.MENU){ if(global.State > global.MENU+999){ int x=me.getX(); int y=me.getY(); if(me.getX() < 150*5){ // Which coordinates for which screen? int lx=x>0?x/150:0; int ly=y>0?y/150:0; // 5 on each row // Page = global.level=((global.screenpage*20)+(ly*5)+lx)+1; global.State=global.LOAD; }else{ // menu options /* if(x > 150*5){ int ly=y>0?y/150:0; if(ly == 0){ if(global.screenpage==0)return; global.screenpage--; global.State=global.MENU+999+global.screenpage+1; } if(ly == 3){ global.screenpage++; global.State=global.MENU+999+global.screenpage+1; } } */ } }else{ // Mouse Pressed global.State=global.DRAW; } }else{ if(global.State == global.DRAW){ // check if we hit an user-object int x=me.getX(); int y=me.getY(); int calcx=x>0?x/global.TILEWIDTH:0; int calcy=y>0?y/global.TILEHEIGHT:0; int objnr=global.grid.getobject(calcy*global.MAXFRAMES, calcx*global.MAXFRAMES); if(objnr > 999){ System.out.println("OBJNR: "+objnr); global.playerx=calcx*global.MAXFRAMES; global.playery=calcy*global.MAXFRAMES; global.usernr=objnr; // loadPlayer(); global.playerxstored=global.playerx; global.playerystored=global.playery; } } } // Set focus to hidden object that catches our keys hidden.requestFocus(); } public void mouseReleased(MouseEvent me){ } public void mouseClicked(MouseEvent me){} public void mouseEntered(MouseEvent me){} public void mouseExited(MouseEvent me){} public void actionListener(ActionEvent ae){ System.out.println("AE: "+ae); } public void actionPerformed(ActionEvent ae){ System.out.println("AE PEFROMERD: "+ae); if(ae.toString().toUpperCase().indexOf("LEVELS") > 0){ global.screenpage=0; global.State=global.MENU+1000; } if(ae.toString().toUpperCase().indexOf("UNDO") > 0){ undo(); } if(ae.toString().toUpperCase().indexOf("RESTART") > 0){ global.State=global.LOAD; } } }