package sig.objects; import java.awt.event.KeyEvent; import sig.RabiClone; import sig.engine.Action; import sig.engine.Alpha; import sig.engine.Font; import sig.engine.Key; import sig.engine.MouseScrollValue; import sig.engine.PaletteColor; import sig.engine.Panel; import sig.engine.Sprite; import sig.engine.Transform; import sig.engine.objects.AnimatedObject; import sig.engine.objects.Object; import sig.engine.String; import sig.map.Background; import sig.map.DataTile; import sig.map.Map; import sig.map.Tile; import sig.objects.actor.PhysicsObject; import sig.objects.actor.RenderedObject; import sig.objects.actor.State; public class LevelRenderer extends Object{ final static double staggerJitterWaitTime=0.03; double staggerTimer=0; int staggerOffsetX=2; public final static byte MAX_RIPPLE_SIZE = (byte)4; public final static byte RIPPLE_CHANCE = (byte)3; public final static byte RIPPLE_DROP_CHANCE = (byte)6; /** *Ripples will store bit 8 for the direction the ripple is moving. Bits 1-7 are used as the actual value for ripples up to 64 in size (Half used for movement in each direction). */ byte[] ripples = new byte[RabiClone.BASE_HEIGHT/MAX_RIPPLE_SIZE]; byte[] extraStorage = new byte[RabiClone.BASE_WIDTH]; double nextRipple = 0; public LevelRenderer(Panel panel) { super(panel); this.setSprite(Sprite.TILE_SHEET); } @Override public void update(double updateMult) { for (int y=(int)(this.getY()/Tile.TILE_HEIGHT)-RabiClone.EVENT_BOUNDARY_RANGE;y<(int)(RabiClone.BASE_HEIGHT/Tile.TILE_HEIGHT+this.getY()/Tile.TILE_HEIGHT+1+RabiClone.EVENT_BOUNDARY_RANGE);y++) { if (y<0||y>Map.MAP_HEIGHT) { continue; } for (int x=(int)(0+this.getX()/Tile.TILE_WIDTH)-RabiClone.EVENT_BOUNDARY_RANGE;x<(int)(RabiClone.BASE_WIDTH/Tile.TILE_WIDTH+this.getX()/Tile.TILE_WIDTH+1+RabiClone.EVENT_BOUNDARY_RANGE);x++) { if (x<0||x>Map.MAP_WIDTH) { continue; } if (RabiClone.CURRENT_MAP.getDataTileRawValue(x,y)<8192&&RabiClone.CURRENT_MAP.getDataTile(x,y)!=DataTile.NULL) { if (!RabiClone.CURRENT_MAP.getDataTile(x,y).getEvent().perform(x*Tile.TILE_WIDTH,y*Tile.TILE_HEIGHT)) { RabiClone.CURRENT_MAP.ModifyDataTile(x, y, DataTile.NULL); } } } } if ((staggerTimer-=updateMult)<=0) { staggerOffsetX*=-1; staggerTimer=staggerJitterWaitTime; } if (RabiClone.player!=null&&RabiClone.player.isUnderwater()) { updateRipples(updateMult); } } private void updateRipples(double updateMult) { if ((nextRipple-=updateMult)<0) { if (Math.random()*RIPPLE_CHANCE<1) { int selectedIndex=(int)(Math.random()*ripples.length); if (ripples[selectedIndex]==0) { if (Math.random()<0.5) { ripples[selectedIndex]=(byte)(MAX_RIPPLE_SIZE|0b10000000); } else { ripples[selectedIndex]=(byte)(MAX_RIPPLE_SIZE|0b00000000); } } } for (int i=0;i>>7)==-1) { //We are moving left. ripples[i]=(byte)(0b10000000|((ripples[i]&0b1111111)-1)); if ((ripples[i]&0b1111111)==0) //Flip the sign. { ripples[i]=(byte)(((ripples[i]&0b1111111)+1)); } else if ((ripples[i]&0b1111111)==MAX_RIPPLE_SIZE&&Math.random()*RIPPLE_DROP_CHANCE<1) { ripples[i]=0; } } else { //We are moving right. ripples[i]=(byte)((ripples[i]&0b1111111)+1); if ((ripples[i]&0b1111111)==MAX_RIPPLE_SIZE*2) //Flip the sign. { ripples[i]=(byte)(0b10000000|((ripples[i]&0b1111111)-1)); } else if ((ripples[i]&0b1111111)==MAX_RIPPLE_SIZE&&Math.random()*RIPPLE_DROP_CHANCE<1) { ripples[i]=0; } } } } nextRipple=0.2; } } @Override public void draw(byte[] p) { for (int y=(int)(this.getY()/Tile.TILE_HEIGHT);y<(int)(RabiClone.BASE_HEIGHT/Tile.TILE_HEIGHT+this.getY()/Tile.TILE_HEIGHT+1);y++) { if (y<0||y>Map.MAP_HEIGHT) { continue; } for (int x=(int)(0+this.getX()/Tile.TILE_WIDTH);x<(int)(RabiClone.BASE_WIDTH/Tile.TILE_WIDTH+this.getX()/Tile.TILE_WIDTH+1);x++) { if (x<0||x>Map.MAP_WIDTH) { continue; } if (RabiClone.CURRENT_MAP.getTile(x,y)!=Tile.VOID) { DrawTile(x*Tile.TILE_WIDTH-this.getX(),y*Tile.TILE_HEIGHT-this.getY(),RabiClone.CURRENT_MAP.getTile(x,y)); //System.out.println((x*Tile.TILE_WIDTH+(this.getX()%Tile.TILE_WIDTH) )+","+(y*Tile.TILE_HEIGHT+(this.getY()%Tile.TILE_HEIGHT))); } } } for (int i=0;i=0&&((i*MAX_RIPPLE_SIZE)+y)=0) { displacement-=y; } else { displacement+=y; } if (displacement>=0) { for (int x=0;x=-displacement;x--) { p[index+x]=p[index+x+displacement]; } for (int x=0;x<-displacement;x++) { p[index+x]=extraStorage[x]; } } } } } } } @SuppressWarnings("unused") private void RenderPlayerCollisionGrid(byte[] p) { for (int y=0;y