2022-05-31 23:46:48 -05:00
|
|
|
package sig.objects;
|
|
|
|
|
|
|
|
import java.awt.event.MouseEvent;
|
2022-06-02 15:52:08 +00:00
|
|
|
import java.io.IOException;
|
2022-05-31 23:46:48 -05:00
|
|
|
|
|
|
|
import sig.RabiClone;
|
2022-06-07 15:47:09 +00:00
|
|
|
import sig.engine.Action;
|
2022-06-01 16:37:01 -05:00
|
|
|
import sig.engine.Alpha;
|
2022-06-01 18:16:26 -05:00
|
|
|
import sig.engine.Font;
|
2022-05-31 23:46:48 -05:00
|
|
|
import sig.engine.MouseScrollValue;
|
2022-06-01 17:40:14 -05:00
|
|
|
import sig.engine.PaletteColor;
|
2022-05-31 23:46:48 -05:00
|
|
|
import sig.engine.Panel;
|
2022-06-09 16:09:03 +00:00
|
|
|
import sig.engine.String;
|
2022-06-02 21:54:20 -05:00
|
|
|
import sig.map.Background;
|
2022-06-13 18:17:02 +00:00
|
|
|
import sig.map.DataTile;
|
2022-05-31 23:46:48 -05:00
|
|
|
import sig.map.Map;
|
|
|
|
import sig.map.Tile;
|
2022-06-02 21:54:20 -05:00
|
|
|
import sig.map.View;
|
|
|
|
import sig.map.Type;
|
2022-05-31 23:46:48 -05:00
|
|
|
|
|
|
|
public class EditorRenderer extends LevelRenderer{
|
|
|
|
|
|
|
|
Tile selectedTile = Tile.WALL;
|
2022-06-13 18:17:02 +00:00
|
|
|
DataTile selectedDataTile = DataTile.BUN1;
|
2022-05-31 23:46:48 -05:00
|
|
|
|
2022-06-09 16:09:03 +00:00
|
|
|
String messageLog = new String();
|
2022-06-02 15:52:08 +00:00
|
|
|
final static long MESSAGE_TIME = 5000;
|
|
|
|
long last_message_log = System.currentTimeMillis();
|
2022-06-01 18:16:26 -05:00
|
|
|
|
2022-06-09 16:09:03 +00:00
|
|
|
final static String HELP_TEXT = new String("(F3) Toggle Camera (F4) Toggle Map Type (F5) Toggle Background");
|
2022-06-02 21:54:20 -05:00
|
|
|
|
|
|
|
final static char CAMERA_SPD = 512;
|
|
|
|
|
2022-06-13 17:36:24 +00:00
|
|
|
boolean dataTileView=false;
|
|
|
|
|
2022-05-31 23:46:48 -05:00
|
|
|
public EditorRenderer(Panel panel) {
|
|
|
|
super(panel);
|
2022-06-02 20:14:36 +00:00
|
|
|
setX(3.5*Tile.TILE_WIDTH);
|
|
|
|
setY(3.5*Tile.TILE_HEIGHT);
|
2022-06-02 18:22:29 +00:00
|
|
|
AddMessage(PaletteColor.YELLOW_GREEN,"Level editing mode",PaletteColor.NORMAL," started.");
|
2022-06-02 15:52:08 +00:00
|
|
|
}
|
|
|
|
|
2022-06-02 18:22:29 +00:00
|
|
|
private void AddMessage(Object...s) {
|
2022-06-02 15:52:08 +00:00
|
|
|
messageLog.append('\n');
|
2022-06-09 16:09:03 +00:00
|
|
|
messageLog.append(s);
|
2022-06-02 15:52:08 +00:00
|
|
|
last_message_log = System.currentTimeMillis();
|
2022-05-31 23:46:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void update(double updateMult) {
|
2022-06-07 15:47:09 +00:00
|
|
|
int right = KeyHeld(Action.MOVE_RIGHT)?1:0;
|
|
|
|
int left = KeyHeld(Action.MOVE_LEFT)?1:0;
|
|
|
|
int up = KeyHeld(Action.JUMP)?1:0;
|
|
|
|
int down = KeyHeld(Action.FALL)?1:0;
|
2022-06-02 21:54:20 -05:00
|
|
|
if (right-left!=0) {
|
2022-06-04 02:51:12 -05:00
|
|
|
setX(Math.max(0,getX()+(right-left)*CAMERA_SPD*updateMult));
|
2022-05-31 23:46:48 -05:00
|
|
|
}
|
|
|
|
if (up-down!=0) {
|
2022-06-04 02:51:12 -05:00
|
|
|
setY(Math.max(0,getY()+(down-up)*CAMERA_SPD*updateMult));
|
2022-06-02 21:54:20 -05:00
|
|
|
}
|
2022-05-31 23:46:48 -05:00
|
|
|
boolean left_mb = MouseHeld(MouseEvent.BUTTON1);
|
2022-06-06 20:17:31 +00:00
|
|
|
// boolean middle_mb = MouseHeld(MouseEvent.BUTTON2);
|
|
|
|
// boolean right_mb = MouseHeld(MouseEvent.BUTTON3);
|
2022-05-31 23:46:48 -05:00
|
|
|
|
|
|
|
if(left_mb){
|
2022-06-02 22:02:59 -05:00
|
|
|
int tileX = (int)(RabiClone.MOUSE_POS.getX()+getX())/Tile.TILE_WIDTH;
|
|
|
|
int tileY = (int)(RabiClone.MOUSE_POS.getY()+getY())/Tile.TILE_HEIGHT;
|
2022-06-13 18:19:59 +00:00
|
|
|
if (dataTileView) {
|
|
|
|
RabiClone.CURRENT_MAP.ModifyDataTile(tileX, tileY, selectedDataTile);
|
|
|
|
} else {
|
|
|
|
RabiClone.CURRENT_MAP.ModifyTile(tileX, tileY, selectedTile);
|
|
|
|
}
|
2022-05-31 23:46:48 -05:00
|
|
|
}
|
2022-06-09 16:09:03 +00:00
|
|
|
if(KeyHeld(Action.SLIDE)&&KeyHeld(Action.FALL)){
|
2022-06-02 15:52:08 +00:00
|
|
|
AddMessage("Saving map...");
|
|
|
|
try {
|
|
|
|
Map.SaveMap(RabiClone.CURRENT_MAP);
|
|
|
|
AddMessage(RabiClone.CURRENT_MAP.toString()," has been saved successfully.");
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
2022-06-02 19:07:29 +00:00
|
|
|
AddMessage(PaletteColor.RED,"Map failed to save: ",e.getLocalizedMessage());
|
2022-06-02 15:52:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
updateMessageLog();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void updateMessageLog() {
|
|
|
|
if (messageLog.length()>0) {
|
|
|
|
if (System.currentTimeMillis()-last_message_log>MESSAGE_TIME) {
|
|
|
|
last_message_log=System.currentTimeMillis();
|
2022-06-09 19:33:09 +00:00
|
|
|
int secondMarker = messageLog.indexOf("\n",messageLog.indexOf("\n")+1);
|
|
|
|
messageLog.replace(messageLog.indexOf("\n"), secondMarker==-1?messageLog.length():secondMarker, "");
|
2022-06-02 15:52:08 +00:00
|
|
|
}
|
2022-05-31 23:46:48 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2022-06-11 18:06:41 -05:00
|
|
|
public void MouseScrolled(MouseScrollValue scrolled) {
|
2022-05-31 23:46:48 -05:00
|
|
|
int up = scrolled==MouseScrollValue.UP?1:0;
|
|
|
|
int down = scrolled==MouseScrollValue.DOWN?1:0;
|
2022-06-13 18:19:59 +00:00
|
|
|
if (dataTileView) {
|
|
|
|
int tempIndex = selectedDataTile.ordinal()+down-up;
|
|
|
|
int selectedIndex = tempIndex<0?DataTile.values().length-1:tempIndex%DataTile.values().length;
|
|
|
|
selectedDataTile = DataTile.values()[selectedIndex];
|
|
|
|
} else {
|
|
|
|
int tempIndex = selectedTile.ordinal()+down-up;
|
|
|
|
int selectedIndex = tempIndex<0?Tile.values().length-1:tempIndex%Tile.values().length;
|
|
|
|
selectedTile = Tile.values()[selectedIndex];
|
|
|
|
}
|
2022-05-31 23:46:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void draw(byte[] p) {
|
|
|
|
super.draw(p);
|
2022-06-02 19:50:39 +00:00
|
|
|
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;
|
|
|
|
}
|
2022-06-13 18:17:02 +00:00
|
|
|
if (dataTileView) {
|
|
|
|
drawMapTileForDataTileMode(p,x,y);
|
2022-06-13 16:51:04 -05:00
|
|
|
if (RabiClone.CURRENT_MAP.getDataTile(x, y)!=DataTile.NULL) {
|
|
|
|
DrawDataTile(p,x*Tile.TILE_WIDTH-this.getX(),y*Tile.TILE_HEIGHT-this.getY(),RabiClone.CURRENT_MAP.getDataTile(x, y));
|
|
|
|
}
|
2022-06-13 18:17:02 +00:00
|
|
|
} else {
|
|
|
|
drawMapTileForEditorMode(x,y);
|
|
|
|
}
|
2022-06-02 19:50:39 +00:00
|
|
|
}
|
|
|
|
}
|
2022-06-02 20:13:34 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
drawTileGrid(p,x,y);
|
|
|
|
}
|
|
|
|
}
|
2022-06-09 16:09:03 +00:00
|
|
|
Draw_Rect(p,PaletteColor.YELLOW,2,0,messageLog.getBounds(Font.PROFONT_12).getX(),messageLog.getBounds(Font.PROFONT_12).getY());
|
2022-06-02 15:52:08 +00:00
|
|
|
Draw_Text(4,0,messageLog,Font.PROFONT_12);
|
2022-06-02 21:54:20 -05:00
|
|
|
Draw_Text(4,RabiClone.BASE_HEIGHT-Font.PROFONT_12.getGlyphHeight()-4,HELP_TEXT,Font.PROFONT_12);
|
2022-06-01 16:37:01 -05:00
|
|
|
}
|
|
|
|
|
2022-06-02 20:13:34 +00:00
|
|
|
private void drawTileGrid(byte[] p, int x, int y) {
|
2022-06-02 21:24:38 -05:00
|
|
|
if (x%Tile.TILE_SCREEN_COUNT_X==0&&y%Tile.TILE_SCREEN_COUNT_Y==0) {
|
|
|
|
int xpos=(int)(x*Tile.TILE_WIDTH-getX());
|
|
|
|
int ypos=(int)(y*Tile.TILE_HEIGHT-getY());
|
|
|
|
Draw_Text(xpos+2,ypos+2,
|
2022-06-09 16:09:03 +00:00
|
|
|
new String("View:").append(PaletteColor.EMERALD).append(RabiClone.CURRENT_MAP.getView(x,y))
|
2022-06-02 22:02:59 -05:00
|
|
|
.append(PaletteColor.NORMAL).append("\nType:").append(PaletteColor.MIDNIGHT_BLUE).append(RabiClone.CURRENT_MAP.getType(x,y))
|
|
|
|
.append(PaletteColor.NORMAL).append("\nBackground:").append(PaletteColor.GRAPE).append(RabiClone.CURRENT_MAP.getBackground(x,y))
|
2022-06-02 21:24:38 -05:00
|
|
|
,Font.PROFONT_12);
|
|
|
|
}
|
2022-06-02 20:13:34 +00:00
|
|
|
if (x%Tile.TILE_SCREEN_COUNT_X==0) {
|
|
|
|
for (int yy=0;yy<Tile.TILE_HEIGHT;yy++) {
|
2022-06-02 16:35:36 -05:00
|
|
|
int ypos=(int)(y*Tile.TILE_HEIGHT-getY()+yy);
|
|
|
|
int xpos=(int)(x*Tile.TILE_WIDTH-getX());
|
|
|
|
int index=ypos*Map.MAP_WIDTH+xpos;
|
2022-06-02 20:13:34 +00:00
|
|
|
if (index<0||index>=p.length) {
|
2022-06-02 16:35:36 -05:00
|
|
|
continue;
|
2022-06-02 20:13:34 +00:00
|
|
|
}
|
|
|
|
Draw(p,index,PaletteColor.BLACK,Alpha.ALPHA0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (y%Tile.TILE_SCREEN_COUNT_Y==0) {
|
|
|
|
for (int xx=0;xx<Tile.TILE_HEIGHT;xx++) {
|
2022-06-02 16:35:36 -05:00
|
|
|
int ypos=(int)(y*Tile.TILE_HEIGHT-getY());
|
|
|
|
int xpos=(int)(x*Tile.TILE_WIDTH-getX()+xx);
|
|
|
|
int index=ypos*Map.MAP_WIDTH+xpos;
|
|
|
|
if (xpos<0||xpos>=Map.MAP_WIDTH||ypos<0||ypos>=Map.MAP_HEIGHT||index<0||index>=p.length) {
|
|
|
|
continue;
|
2022-06-02 20:13:34 +00:00
|
|
|
}
|
|
|
|
Draw(p,index,PaletteColor.BLACK,Alpha.ALPHA0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-01 16:37:01 -05:00
|
|
|
protected void drawMapTileForEditorMode(int x, int y) {
|
2022-06-02 22:02:59 -05:00
|
|
|
int tilerX = (int)(RabiClone.MOUSE_POS.getX()+getX())/Tile.TILE_WIDTH;
|
|
|
|
int tilerY = (int)(RabiClone.MOUSE_POS.getY()+getY())/Tile.TILE_HEIGHT;
|
|
|
|
if (x==tilerX&&y==tilerY) {
|
2022-06-02 18:45:50 +00:00
|
|
|
double tileX = x*Tile.TILE_WIDTH-this.getX();
|
|
|
|
double tileY = y*Tile.TILE_HEIGHT-this.getY();
|
|
|
|
DrawTransparentTile(tileX,tileY,selectedTile,Alpha.ALPHA160);
|
2022-06-09 16:09:03 +00:00
|
|
|
Draw_Text(tileX+2,tileY-Font.PROFONT_12.getGlyphHeight()-2,new String(selectedTile.toString()),Font.PROFONT_12);
|
|
|
|
Draw_Text_Ext(tileX+2,tileY+Tile.TILE_HEIGHT+2,new String(RabiClone.CURRENT_MAP.getTile(x,y).toString()),Font.PROFONT_12,Alpha.ALPHA0,PaletteColor.CRIMSON);
|
2022-06-01 16:37:01 -05:00
|
|
|
}
|
2022-06-02 21:54:20 -05:00
|
|
|
}
|
|
|
|
|
2022-06-13 18:17:02 +00:00
|
|
|
protected void drawMapTileForDataTileMode(byte[] p, int x, int y) {
|
|
|
|
int tilerX = (int)(RabiClone.MOUSE_POS.getX()+getX())/Tile.TILE_WIDTH;
|
|
|
|
int tilerY = (int)(RabiClone.MOUSE_POS.getY()+getY())/Tile.TILE_HEIGHT;
|
|
|
|
if (x==tilerX&&y==tilerY) {
|
|
|
|
double tileX = x*Tile.TILE_WIDTH-this.getX();
|
|
|
|
double tileY = y*Tile.TILE_HEIGHT-this.getY();
|
|
|
|
DrawTransparentDataTile(p,tileX,tileY,selectedDataTile,PaletteColor.GRAPE);
|
2022-06-13 16:51:04 -05:00
|
|
|
Draw_Text_Ext(tileX+2,tileY+Tile.TILE_HEIGHT+2,selectedDataTile.getDescription(),Font.PROFONT_12,Alpha.ALPHA0,PaletteColor.CRIMSON);
|
2022-06-13 18:17:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void DrawTransparentDataTile(byte[] p, double x, double y, DataTile tile,PaletteColor col) {
|
|
|
|
Draw_Rect(p,col,x,y,Tile.TILE_WIDTH,Tile.TILE_HEIGHT);
|
2022-06-13 16:51:04 -05:00
|
|
|
Draw_Text_Ext(x+2,y+2,new String(tile.toString()),Font.PROFONT_12,Alpha.ALPHA0,PaletteColor.WHITE);
|
2022-06-13 18:17:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected void DrawDataTile(byte[] p, double x, double y, DataTile tile) {
|
|
|
|
DrawTransparentDataTile(p,x,y,tile,PaletteColor.MIDNIGHT_BLUE);
|
|
|
|
}
|
|
|
|
|
2022-06-02 21:54:20 -05:00
|
|
|
@Override
|
2022-06-07 15:47:09 +00:00
|
|
|
@SuppressWarnings("incomplete-switch")
|
2022-06-11 18:06:41 -05:00
|
|
|
public void KeyPressed(Action a) {
|
2022-06-02 22:02:59 -05:00
|
|
|
int tileX = (int)(RabiClone.MOUSE_POS.getX()+getX())/Tile.TILE_WIDTH;
|
|
|
|
int tileY = (int)(RabiClone.MOUSE_POS.getY()+getY())/Tile.TILE_HEIGHT;
|
2022-06-07 15:47:09 +00:00
|
|
|
switch (a) {
|
2022-06-13 17:36:24 +00:00
|
|
|
case PLAY_GAME:{
|
|
|
|
RabiClone.OBJ.remove(RabiClone.level_renderer);
|
|
|
|
RabiClone.OBJ.add(RabiClone.level_renderer = new LevelRenderer(RabiClone.p));
|
|
|
|
RabiClone.StartGame();
|
|
|
|
}break;
|
|
|
|
case LEVEL_EDITOR:{
|
|
|
|
dataTileView=!dataTileView;
|
|
|
|
}break;
|
2022-06-07 15:47:09 +00:00
|
|
|
case EDITOR_SET_VIEW:{
|
2022-06-02 22:02:59 -05:00
|
|
|
RabiClone.CURRENT_MAP.setView(tileX,tileY,View.values()[(RabiClone.CURRENT_MAP.getView(tileX, tileY).ordinal()+1)%View.values().length]);
|
2022-06-02 21:54:20 -05:00
|
|
|
}break;
|
2022-06-07 15:47:09 +00:00
|
|
|
case EDITOR_SET_TYPE:{
|
2022-06-02 22:02:59 -05:00
|
|
|
RabiClone.CURRENT_MAP.setType(tileX,tileY,Type.values()[(RabiClone.CURRENT_MAP.getType(tileX, tileY).ordinal()+1)%Type.values().length]);
|
2022-06-02 21:54:20 -05:00
|
|
|
}break;
|
2022-06-07 15:47:09 +00:00
|
|
|
case EDITOR_SET_BACKGROUND:{
|
2022-06-02 22:02:59 -05:00
|
|
|
RabiClone.CURRENT_MAP.setBackground(tileX,tileY,Background.values()[(RabiClone.CURRENT_MAP.getBackground(tileX, tileY).ordinal()+1)%Background.values().length]);
|
2022-06-02 21:54:20 -05:00
|
|
|
}break;
|
|
|
|
}
|
2022-06-13 16:51:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-05-31 23:46:48 -05:00
|
|
|
}
|