Incorporate tile drawing and partial sprite drawing functions

Co-authored-by: r3cp3ct <45179536+r3cp3ct@users.noreply.github.com>
Co-authored-by: sigonasr2 <sigonasr2@gmail.com>
main
Nic0Nic0Nii 3 years ago
parent 0250b70a5d
commit 95a923fad3
  1. BIN
      bin/RabiClone.jar
  2. 22
      src/sig/DrawLoop.java
  3. 15
      src/sig/RabiClone.java
  4. 10
      src/sig/engine/Object.java
  5. 1
      src/sig/engine/Sprite.java
  6. 0
      src/sig/engine/SpriteImage.java
  7. 9
      src/sig/map/Maps.java
  8. 2
      src/sig/map/Tile.java
  9. 47
      src/sig/objects/LevelRenderer.java
  10. 13
      src/sig/objects/Player.java

Binary file not shown.

@ -11,7 +11,7 @@ public class DrawLoop {
for (int y=0;y<RabiClone.BASE_HEIGHT;y++) { for (int y=0;y<RabiClone.BASE_HEIGHT;y++) {
for (int x=0;x<RabiClone.BASE_WIDTH;x++) { for (int x=0;x<RabiClone.BASE_WIDTH;x++) {
p[y*RabiClone.BASE_WIDTH+x]=(0<<16)+(0<<8)+0;//RGB p[y*RabiClone.BASE_WIDTH+x]=(16<<16)+(16<<8)+16;//RGB
} }
} }
@ -21,15 +21,23 @@ public class DrawLoop {
} }
public static void Draw_Sprite(double x, double y, Sprite sprite){ public static void Draw_Sprite(double x, double y, Sprite sprite){
Draw_Sprite_Partial(x,y,0,0,sprite.getWidth(),sprite.getHeight(),sprite);
}
public static void Draw_Sprite_Partial(double x, double y, double xOffset, double yOffset, double w, double h, Sprite sprite){
int[] p = panel.pixel; int[] p = panel.pixel;
for(int X=0;X<sprite.getHeight();X++){ for(int X=(int)xOffset;X<(int)(w+xOffset);X++){
for(int Y=0;Y<sprite.getWidth();Y++){ for(int Y=(int)yOffset;Y<(int)(h+yOffset);Y++){
int index = (Y+(int)y)*RabiClone.BASE_WIDTH+X+(int)x; if (X+x-xOffset<0||Y+y-yOffset<0||X-xOffset+x>=RabiClone.BASE_WIDTH||Y-yOffset+y>=RabiClone.BASE_HEIGHT) {
if (index<0||index>=p.length||p[index]==sprite.getBi_array()[Y*sprite.getWidth()+X]) {
continue; continue;
} else { } else {
Draw(p,index,sprite.getBi_array()[Y*sprite.getWidth()+X],true); int index = (Y-(int)yOffset+(int)y)*RabiClone.BASE_WIDTH+X-(int)xOffset+(int)x;
//Draw(p,index,sprite.getBi_array()[Y*sprite.getWidth()+X],false); if (index<0||index>=p.length||p[index]==sprite.getBi_array()[Y*sprite.getWidth()+X]) {
continue;
} else {
Draw(p,index,sprite.getBi_array()[Y*sprite.getWidth()+X],true);
//Draw(p,index,sprite.getBi_array()[Y*sprite.getWidth()+X],false);
}
} }
} }
} }

@ -6,9 +6,8 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import sig.engine.Panel; import sig.engine.Panel;
import sig.map.Map;
import sig.map.Maps; import sig.map.Maps;
import sig.map.Tile; import sig.objects.LevelRenderer;
import sig.objects.Player; import sig.objects.Player;
import sig.engine.Object; import sig.engine.Object;
import java.awt.Toolkit; import java.awt.Toolkit;
@ -44,19 +43,9 @@ public class RabiClone{
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true); f.setVisible(true);
OBJ.add(new LevelRenderer(p));
OBJ.add(new Player(p)); OBJ.add(new Player(p));
for (int i=0;i<12;i++) {
CURRENT_MAP.ModifyTile(0, i, Tile.WALL);
}
for (int i=0;i<36;i++) {
CURRENT_MAP.ModifyTile(i, 11, Tile.FLOOR);
}
for (int i=0;i<10;i++) {
CURRENT_MAP.ModifyTile(i+5, 8, Tile.PLATFORM_LEDGE);
}
Map.SaveMap(CURRENT_MAP);
p.render(); p.render();
long lastGameTime = System.nanoTime(); long lastGameTime = System.nanoTime();

@ -1,5 +1,7 @@
package sig.engine; package sig.engine;
import sig.DrawLoop;
public abstract class Object implements GameEntity{ public abstract class Object implements GameEntity{
double x,y; double x,y;
Sprite spr; Sprite spr;
@ -45,6 +47,14 @@ public abstract class Object implements GameEntity{
this.spr = spr; this.spr = spr;
} }
protected void Draw_Sprite(double x, double y, Sprite sprite){
DrawLoop.Draw_Sprite(x,y,sprite);
}
protected void Draw_Sprite_Partial(double x, double y, double xOffset, double yOffset, double w, double h, Sprite sprite){
DrawLoop.Draw_Sprite_Partial(x,y,xOffset,yOffset,w,h,sprite);
}
protected boolean KeyHeld(int key) { protected boolean KeyHeld(int key) {
return panel.KEYS.getOrDefault(key,false); return panel.KEYS.getOrDefault(key,false);
} }

@ -10,6 +10,7 @@ public enum Sprite{
NANA(new File("..","3x.png")), NANA(new File("..","3x.png")),
NANA_SMALL(new File("..","1x.png")), NANA_SMALL(new File("..","1x.png")),
TILE_SHEET(new File("..","tiles.png")),
; ;

@ -28,4 +28,13 @@ public enum Maps {
map.ModifyTile(x, y, t); map.ModifyTile(x, y, t);
//System.out.println("Tile "+(y*MAP_WIDTH+x)+" is now "+tiles[y*MAP_WIDTH+x]+"."); //System.out.println("Tile "+(y*MAP_WIDTH+x)+" is now "+tiles[y*MAP_WIDTH+x]+".");
} }
public Tile getTile(int x,int y) {
int index = y*Map.MAP_WIDTH+x;
if (index<0||index>=this.map.tiles.length) {
return Tile.VOID;
} else {
return Tile.values()[this.map.tiles[index]];
}
}
} }

@ -5,7 +5,7 @@ public enum Tile {
WALL(0,0), WALL(0,0),
FLOOR(1,0), FLOOR(1,0),
PLATFORM_LEDGE(2,0), PLATFORM_LEDGE(2,0),
INVISIBLE_WALL(0,0,false), INVISIBLE_WALL(0,0,true),
; ;
final public static int TILE_WIDTH=32; final public static int TILE_WIDTH=32;

@ -0,0 +1,47 @@
package sig.objects;
import java.awt.event.KeyEvent;
import sig.RabiClone;
import sig.engine.Object;
import sig.engine.Panel;
import sig.engine.Sprite;
import sig.map.Tile;
public class LevelRenderer extends Object{
public LevelRenderer(Panel panel) {
super(panel);
this.setSprite(Sprite.TILE_SHEET);
}
@Override
public void update(double updateMult) {
int right = KeyHeld(KeyEvent.VK_RIGHT)?1:0;
int left = KeyHeld(KeyEvent.VK_LEFT)?1:0;
int up = KeyHeld(KeyEvent.VK_UP)?1:0;
int down = KeyHeld(KeyEvent.VK_DOWN)?1:0;
if (right-left!=0) {
setX(getX()+(right-left)*256*updateMult);
}
if (up-down!=0) {
setY(getY()+(down-up)*256*updateMult);
}
}
@Override
public void draw(int[] p) {
for (int y=(int)(0+this.getY()/Tile.TILE_HEIGHT);y<(int)(RabiClone.BASE_HEIGHT/Tile.TILE_HEIGHT+this.getY()/Tile.TILE_HEIGHT+1);y++) {
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 (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)));
}
}
}
}
private void DrawTile(double x, double y, Tile tile) {
Draw_Sprite_Partial(x,y, tile.getSpriteSheetX()*tile.getTileWidth(), tile.getSpriteSheetY()*tile.getTileHeight(), tile.getTileWidth(), tile.getTileHeight(), getSprite());
}
}

@ -1,6 +1,5 @@
package sig.objects; package sig.objects;
import sig.DrawLoop;
import sig.RabiClone; import sig.RabiClone;
import sig.engine.Object; import sig.engine.Object;
import sig.engine.Panel; import sig.engine.Panel;
@ -19,21 +18,11 @@ public class Player extends Object{
@Override @Override
public void update(double updateMult) { public void update(double updateMult) {
int right = KeyHeld(KeyEvent.VK_RIGHT)?1:0;
int left = KeyHeld(KeyEvent.VK_LEFT)?1:0;
int up = KeyHeld(KeyEvent.VK_UP)?1:0;
int down = KeyHeld(KeyEvent.VK_DOWN)?1:0;
if (right-left!=0) {
setX(getX()+(right-left)*32*updateMult);
}
if (up-down!=0) {
setY(getY()+(down-up)*32*updateMult);
}
} }
@Override @Override
public void draw(int[] p) { public void draw(int[] p) {
DrawLoop.Draw_Sprite(this.getX(), this.getY(), this.getSprite()); Draw_Sprite(this.getX(), this.getY(), this.getSprite());
} }
} }

Loading…
Cancel
Save