Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)

Paste

Pasted as Java by nicke ( 16 years ago )
package fi.niklaswahrman.asterope;

import android.content.Context;
import android.graphics.*;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import java.io.*;
import java.util.*;

public class World {
 
 // Render state
 public static int frameNr = 0;
 public static int gfx_level = 2; // low, medium, high
 
 // Mapsize
 public static int map_Width;
 public static int map_Height;
 
 // Minimap
 public static Drawable minimap_Drawable;
 public static int minimap_Width;
 public static int minimap_Height;

 // The obstacles found in this level
 public static int ob_Count;
 public static int[] ob_Hash;
 public static int[] ob_Width;
 public static int[] ob_Height;
 public static float[][][] ob_ConvexShapeVertexX;
 public static float[][][] ob_ConvexShapeVertexY;
 public static float[][][] ob_ConvexShapeNormalX;
 public static float[][][] ob_ConvexShapeNormalY;
 public static Drawable[] ob_Drawable;

 // The instances of the obstacles in this level
 public static int obInst_Count;
 public static int[] obInst_ObstacleReference;
 public static float[] obInst_PositionX;
 public static float[] obInst_PositionY;
 public static int[] obInst_Rotation;
 public static int[] obInst_Special;
 public static int[] obinst_LastFrameDrawn;

 // Map sectors
 public static int map_SectorWidth = 0;
 public static int map_SectorHeight = 0;
 public static int[][][] map_Sectors = null;

 // Map foreground tilemap
 private static Paint tilePaint;
 private static int map_ForegroundWidth;
 private static boolean[] map_Foreground;

 // Map Colors
 public static int color_Background;
 public static int color_Foreground;
  //public static int color_ForegroundShadow;

 // Map special variable to define numerous map features
 public static int map_Special = 0;

 // Special points
 public static int specPt_Count;
 public static int[] specPt_PositionX;
 public static int[] specPt_PositionY;
 public static int[] specPt_ID;
 public static int[][] specPt_Data;

 // Metadata
 public static int meta_Count;
 public static int[] meta_ID;
 public static String[] meta_Data;

 public static String loadLevel( Context context, String fileName ) {

  try {
   String path = "levels";
   DataInputStream reader = new DataInputStream( context.getResources()
     .getAssets().open( path + "/" + fileName + ".ast" ) );

   map_Special = reader.readInt();
   color_Background = reader.readInt();
   color_Foreground = reader.readInt();   
    
   // Create tilepaint
   tilePaint = new Paint();
   tilePaint.setColor( color_Foreground );

   int numTypeOfObstacles = reader.readInt();

   // Read and ignore as all are loaded
   for( int i = 0; i < numTypeOfObstacles; i++ )
    reader.readInt();

   // Number of obstacles
   obInst_Count = reader.readInt();
   obInst_ObstacleReference = new int[ obInst_Count ];
   obInst_PositionX = new float[ obInst_Count ];
   obInst_PositionY = new float[ obInst_Count ];
   obInst_Rotation = new int[ obInst_Count ];
   obInst_Special = new int[ obInst_Count ];
   obinst_LastFrameDrawn = new int[ obInst_Count ];

   for( int i = 0; i < obInst_Count; i++ ) {
    obInst_ObstacleReference[ i ] = reader.readInt();
    obInst_PositionX[ i ] = reader.readInt();
    obInst_PositionY[ i ] = reader.readInt();
    obInst_Rotation[ i ] = reader.readInt();
    obInst_Special[ i ] = reader.readInt();
   }

   // Read map sector size
   int numSectors = reader.readInt();
   map_SectorWidth = reader.readInt();
   map_SectorHeight = numSectors / map_SectorWidth;

   // Read foreground tilemap
   int fgWidth = map_SectorWidth * 2 + 8;
   int fgHeight = map_SectorHeight * 2;

   map_ForegroundWidth = fgWidth;
   map_Foreground = new boolean[ fgWidth * fgHeight ];

   fgWidth = fgWidth / 8;

   for( int y = 0; y < fgHeight; y++ )
    for( int x = 0; x < fgWidth; x++ ) {
     byte tilebuffer = reader.readByte();

     for( int b = 0; b < 8; b++ )
      map_Foreground[ y * map_ForegroundWidth + ( x << 3 ) + b ] = ( ( tilebuffer >> b ) & 1 ) > 0;
    }

   // Read map sectors
   map_Sectors = new int[ map_SectorWidth ][ map_SectorHeight ][];

   for( int y = 0; y < map_SectorHeight; y++ )
    for( int x = 0; x < map_SectorWidth; x++ ) {
     int numOfObstacles = (int) ( reader.readByte() & 0xFF );
     map_Sectors[ x ][ y ] = new int[ numOfObstacles ];

     for( int i = 0; i < numOfObstacles; i++ )
      map_Sectors[ x ][ y ][ i ] = reader.readInt();
    }

   // Number of special points
   specPt_Count = reader.readInt();
   specPt_PositionX = new int[ specPt_Count ];
   specPt_PositionY = new int[ specPt_Count ];
   specPt_ID = new int[ specPt_Count ];
   specPt_Data = new int[ specPt_Count ][];

   for( int i = 0; i < specPt_Count; i++ ) {
    specPt_ID[ i ] = reader.readInt();
    specPt_PositionX[ i ] = reader.readInt();
    specPt_PositionY[ i ] = reader.readInt();
    specPt_Data[ i ] = new int[ reader.readInt() ];

    for( int j = 0; j < specPt_Data[ i ].length; j++ )
     specPt_Data[ i ][ j ] = reader.readInt();
   }

   // Number of metadata
   meta_Count = reader.readInt();
   meta_ID = new int[ meta_Count ];
   meta_Data = new String[ meta_Count ];

   for( int i = 0; i < meta_Count; i++ ) {
    meta_ID[ i ] = reader.readInt();
    int stringLength = reader.readInt();
    meta_Data[ i ] = "";
    
    for( int j = 0; j < stringLength; j++ )
     meta_Data[ i ] += (char) reader.readByte();
   }

   // Optimize this. But for now change references form hash to index
   Map<Integer, Integer> hashMap = new HashMap<Integer, Integer>();

   for( int i = 0; i < ob_Count; i++ )
    hashMap.put( ob_Hash[ i ], i );

   for( int i = 0; i < obInst_Count; i++ )
    obInst_ObstacleReference[ i ] = (int) hashMap
      .get( obInst_ObstacleReference[ i ] );

   // Load minimap
   minimap_Drawable = Drawable.createFromStream( new DataInputStream(
     context.getResources().getAssets().open(
       path + "/" + fileName + ".png" ) ), fileName + ".png" );
   minimap_Width = minimap_Drawable.getIntrinsicWidth();
   minimap_Height = minimap_Drawable.getIntrinsicHeight();

  } catch ( Exception e ) {
   return e.toString();
  }
  
  map_Width = map_SectorWidth * 128 - 64;
  map_Height = map_SectorHeight * 128 - 64;

  return "loaded";
 }

 public static String loadObstacles( Context context ) {
  try {
   String path = "obstacles";
   String[] obstacleFiles = context.getResources().getAssets().list( path );

   ob_Count = obstacleFiles.length / 2;
   ob_ConvexShapeVertexX = new float[ ob_Count ][][];
   ob_ConvexShapeVertexY = new float[ ob_Count ][][];
   ob_ConvexShapeNormalX = new float[ ob_Count ][][];
   ob_ConvexShapeNormalY = new float[ ob_Count ][][];
   ob_Width = new int[ ob_Count ];
   ob_Height = new int[ ob_Count ];
   ob_Hash = new int[ ob_Count ];
   ob_Drawable = new Drawable[ ob_Count ];

   for( int i = 0; i < ob_Count; i++ ) {
    String fileName = obstacleFiles[ i << 1 ];

    ob_Hash[ i ] = StringHash( fileName
      .substring( 0, fileName.length() - 4 ) );

    // Load file with polygon data
    DataInputStream reader = new DataInputStream( context.getResources()
      .getAssets().open( path + "/" + fileName ) );

    byte numConvexShapes = reader.readByte();

    ob_ConvexShapeVertexX[ i ] = new float[ numConvexShapes ][];
    ob_ConvexShapeVertexY[ i ] = new float[ numConvexShapes ][];
    ob_ConvexShapeNormalX[ i ] = new float[ numConvexShapes ][];
    ob_ConvexShapeNormalY[ i ] = new float[ numConvexShapes ][];

    for( int j = 0; j < numConvexShapes; j++ ) {
     byte numPoints = reader.readByte();

     ob_ConvexShapeVertexX[ i ][ j ] = new float[ numPoints ];
     ob_ConvexShapeVertexY[ i ][ j ] = new float[ numPoints ];
     ob_ConvexShapeNormalX[ i ][ j ] = new float[ numPoints ];
     ob_ConvexShapeNormalY[ i ][ j ] = new float[ numPoints ];

     for( int k = 0; k < numPoints; k++ ) {
      ob_ConvexShapeVertexX[ i ][ j ][ k ] = (float)reader.readInt() / 65536.0f;
      ob_ConvexShapeVertexY[ i ][ j ][ k ] = (float)reader.readInt() / 65536.0f;
      ob_ConvexShapeNormalX[ i ][ j ][ k ] = (float)reader.readInt() / 65536.0f;
      ob_ConvexShapeNormalY[ i ][ j ][ k ] = (float)reader.readInt() / 65536.0f;
     }
    }

    // Load image
    String imageFile = obstacleFiles[ ( i << 1 ) + 1 ];
    ob_Drawable[ i ] = Drawable.createFromStream( context.getResources()
      .getAssets().open( path + "/" + imageFile ), imageFile );
    ob_Width[ i ] = ob_Drawable[ i ].getIntrinsicWidth();
    ob_Height[ i ] = ob_Drawable[ i ].getIntrinsicHeight();
   }
  } catch ( IOException ioe ) {
   return ioe.toString();
  }

  return "ok " + ob_Count;
 }

 private static int StringHash( String input ) {
  int hashCode = 0;
  char[] charArray = input.toCharArray();

  for( int i = 0; i < charArray.length; i++ )
   hashCode = charArray[ i ] + ( hashCode << 6 ) + ( hashCode << 16 )
     - hashCode;

  hashCode &= 0x7FFFFFFF;

  return hashCode;
 }

 /*
  * Note: Test to see if the point is out side the world excluded
  */
 public static int pointInObstacle( int x, int y ) {
  int returnValue = -1;

  int[] obstacles = map_Sectors[ x >> 7 ][ y >> 7 ];

  for( int i = 0; i < obstacles.length; i++ )
   if ( pointInObstacle( x, y, obstacles[ i ] ) )
    return obstacles[ i ];

  return returnValue;
 }

 public static boolean pointInObstacle( float x, float y, int obInstIndex ) {
  
  // Quick box check to filter out impossible collisions
  if( x < obInst_PositionX[ obInstIndex ] ||
    x > obInst_PositionX[ obInstIndex ] + ob_Width[ obInst_ObstacleReference[ obInstIndex ] ] ||
    y < obInst_PositionY[ obInstIndex ] ||
    y > obInst_PositionY[ obInstIndex ] + ob_Height[ obInst_ObstacleReference[ obInstIndex ] ] )
     return false;

  float relX = ( x - obInst_PositionX[ obInstIndex ] ); 
  float relY = ( y - obInst_PositionY[ obInstIndex ] );
  int obstacle = obInst_ObstacleReference[ obInstIndex ];

  for( int i = 0; i < ob_ConvexShapeVertexX[ obstacle ].length; i++ ) {
   boolean insideShape = true;

   for( int j = 0; j < ob_ConvexShapeVertexX[ obstacle ][ i ].length; j++ ) {
    float vertRelativeX = relX - ob_ConvexShapeVertexX[ obstacle ][ i ][ j ];
    float vertRelativeY = relY - ob_ConvexShapeVertexY[ obstacle ][ i ][ j ];
    float normalX = ob_ConvexShapeNormalX[ obstacle ][ i ][ j ];
    float normalY = ob_ConvexShapeNormalY[ obstacle ][ i ][ j ];

    if ( ( vertRelativeX * normalX ) + ( vertRelativeY * normalY ) > 0 )
     insideShape = false;
   }

   if ( insideShape )
    return true;
  }

  return false;
 }
 
 public static int darkerBackground( int value ) {
  int cr = ((color_Background & 0xff0000) >> 16) - value;
  int cg = ((color_Background & 0xff00) >> 8) - value;
  int cb = ((color_Background & 0xff)) - value;
  cr = cr < 0 ? 0 : cr;
  cg = cg < 0 ? 0 : cr;
  cb = cb < 0 ? 0 : cr;
  
  return 0xff000000 | (cr  << 16) | (cg << 8) | cb;
 }
 public static void render( Canvas canvas, int x, int y, int width, int height ) {
  // Render level
  frameNr++;

  // Render obstacles as shadow
  // Only enable at very high gfx settings?
  /*
  for( int dy = y; dy < height + y + 127; dy += 128 )
   for( int dx = x; dx < width + x + 127; dx += 128 ) {
    int sectorX = dx >> 7;
    int sectorY = dy >> 7;

    if ( sectorX < map_SectorWidth && sectorY < map_SectorHeight ) {
     int[] obInstances = map_Sectors[ sectorX ][ sectorY ];

     for( int i = 0; i < obInstances.length; i++ ) {
      int obInstIndex = obInstances[ i ];

      if ( obinst_LastFrameDrawn[ obInstIndex ] != frameNr ) {
       int obstRef = obInst_ObstacleReference[ obInstIndex ];
       Drawable image = ob_Drawable[ obstRef ];
       int drawX = obInst_PositionX[ obInstIndex ] - x;
       int drawY = obInst_PositionY[ obInstIndex ] - y + 16;       

       image.setBounds( drawX, drawY, drawX + ob_Width[ obstRef ], drawY
         + ob_Height[ obstRef ] );
       image.draw( canvas );
      }
     }
    }
   }
  */
  
  // Render tilemap
  int tx = x >> 6;
  int ty = y >> 6;
  int tw = width >> 6;
  int th = height >> 6;

    
  for( int dy = ty; dy < ty + th + 2; dy++ )
   for( int dx = tx; dx < tx + tw + 2; dx++ )
    if ( dx < map_ForegroundWidth
      && ( dy * map_ForegroundWidth + dx ) < map_Foreground.length
      && map_Foreground[ dy * map_ForegroundWidth + dx ] ) {
     
     int tilex = ( dx << 6 ) - x;
     int tiley = ( dy << 6 ) - y;
     
     canvas.drawRect(tilex, tiley, tilex + 64, tiley + 64, tilePaint );
     
    } 
  
  // Render obstacles
  
  for( int dy = y; dy < height + y + 127; dy += 128 )
   for( int dx = x; dx < width + x + 127; dx += 128 ) {
    int sectorX = dx >> 7;
    int sectorY = dy >> 7;

    if ( sectorX < map_SectorWidth && sectorY < map_SectorHeight ) {
     int[] obInstances = map_Sectors[ sectorX ][ sectorY ];

     for( int i = 0; i < obInstances.length; i++ ) {
      int obInstIndex = obInstances[ i ];

      if ( obinst_LastFrameDrawn[ obInstIndex ] != frameNr ) {
       int obstRef = obInst_ObstacleReference[ obInstIndex ];
       Drawable image = ob_Drawable[ obstRef ];
       int drawX = (int)obInst_PositionX[ obInstIndex ] - x;
       int drawY = (int)obInst_PositionY[ obInstIndex ] - y;
 
       image.setBounds( drawX, drawY, drawX + ob_Width[ obstRef ], drawY
         + ob_Height[ obstRef ] );

       image.draw( canvas );

       obinst_LastFrameDrawn[ obInstIndex ] = frameNr;
      }
     }
    }
   }
 }
}

 

Revise this Paste

Your Name: Code Language: