Skip to content

Commit

Permalink
added tui & TODO
Browse files Browse the repository at this point in the history
  • Loading branch information
shuntia committed Dec 6, 2024
1 parent b1532de commit 564f7de
Show file tree
Hide file tree
Showing 11 changed files with 229 additions and 24 deletions.
3 changes: 3 additions & 0 deletions Dialogue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
public class Dialogue {

}
22 changes: 22 additions & 0 deletions Eventmgr.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

import java.util.HashMap;

public class Eventmgr {
HashMap<String, Integer> flags = new HashMap<>();
public void addEvent(String name, int value) {
flags.put(name, value);
}
public void removeEvent(String name) {
flags.remove(name);
}
public void setEventValue(String name, int value) {
flags.put(name, value);
}
public int getEventValue(String name) {
return flags.get(name);
}
// If event is 1, checkEvent will return true. if value is 0, false otherwise 0
public boolean checkEvent(String name) {
return flags.get(name) == 1;
}
}
55 changes: 52 additions & 3 deletions Graphicsmgr.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,68 @@

import java.util.HashMap;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Graphicsmgr {
final boolean isGUI;
JPanel panel;
JFrame frame;
HashMap<String, javax.swing.JComponent> components = new HashMap<>();
public Graphicsmgr(boolean isGUI){
this.isGUI = isGUI;
}
public void makePanel(){
if(isGUI){
panel = new JPanel();
}
}
public void makePanel(String name){
if(isGUI){
panel = new JPanel();
frame = new JFrame(name);
frame.add(panel);
}
}
@Deprecated
public void makeTextArea(){
if(isGUI){
javax.swing.JTextArea textArea = new javax.swing.JTextArea();
textArea.setFont(new java.awt.Font("Monospaced", java.awt.Font.PLAIN, 30));
components.put("textArea", textArea);
panel.add(textArea);
}
}
public void makeTextArea(String name){
if(isGUI){
javax.swing.JTextArea textArea = new javax.swing.JTextArea();
textArea.setFont(new java.awt.Font("Monospaced", java.awt.Font.PLAIN, 30));
components.put(name, textArea);
panel.add(textArea);
}
}
public JComponent getComponent(String name){
return components.get(name);
}
public void draw(){
if(isGUI){
drawGUI();
}else{
drawConsole();
drawTUI();
}
}
private void drawGUI(){
System.out.println("Drawing GUI");
}
private void drawConsole(){
System.out.println("Drawing Console");
private void drawTUI(){
components.forEach((k, v) -> {
if(v instanceof javax.swing.JTextArea textArea){
if(k.equals("tilemap")){
RenderedMap rm = Tilemapmgr.draw("test", 0, 0, 10, 10);
rm.overlay(new RenderedMap(2, 4, 'X'), 4, 5);
textArea.setText(rm.toString());
};
}
});
}
}
7 changes: 5 additions & 2 deletions Interactable.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
public class Interactable {

public class Interactable extends MapCharacter{
Dialogue dialogue;
public Interactable(String name) {
super(name);
}
}
20 changes: 20 additions & 0 deletions Location.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
public class Location {
Scene scene;
Position position;
public Location(Scene scene, Position position) {
this.scene = scene;
this.position = position;
}
public Position getPosition() {
return position;
}
public Scene getScene() {
return scene;
}
public void setPosition(Position position) {
this.position = position;
}
public void setScene(Scene scene) {
this.scene = scene;
}
}
3 changes: 3 additions & 0 deletions Logicmgr.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
public class Logicmgr {

}
62 changes: 43 additions & 19 deletions Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ public class Main{
public static void main(String[] args){
java.io.PrintWriter out = new java.io.PrintWriter(System.out);
Reader in = new Reader();
out.println("Enter mode:\n1. tile map editor\n2. test tile map and overlay");
out.println("Enter mode:\n1. tile map editor\n2. test tile map and overlay\n3. test tile map and overlay with TUI");
out.flush();
int mode = 0;
try{mode = in.nextInt();}catch(Exception e){e.printStackTrace();}
Expand All @@ -14,34 +14,58 @@ public static void main(String[] args){
}else if(mode==2){
Tilemapmgr.loadFromImage("test", "C:\\Users\\shunt\\apcs\\CSQuest\\tilemaps\\test.png");
RenderedMap map=Tilemapmgr.draw("test");
int x=0, y=0;
int x=8, y=8;
String input="";
OUTER:
while (true) {
map=Tilemapmgr.draw("test");
map.overlay(new RenderedMap(2, 4, 'X'), x, y);
map=Tilemapmgr.draw("test", x-2, y-2, 10, 10);
map.overlay(new RenderedMap(2, 4, 'X'), 4, 5);
out.println(map);
out.flush();
try{input=in.readLine();}catch(Exception e){e.printStackTrace();}
switch (input) {
case "w":
y--;
break;
case "s":
y++;
break;
case "a":
x--;
break;
case "d":
x++;
break;
case "q":
case "w" -> y--;
case "s" -> y++;
case "a" -> x--;
case "d" -> x++;
case "q" -> {
break OUTER;
default:
break;
}
default -> {
}
}
}
}else if(mode==3){
Tilemapmgr.loadFromImage("test", "C:\\Users\\shunt\\apcs\\CSQuest\\tilemaps\\test.png");
Tilemapmgr.activate();
Graphicsmgr gm = new Graphicsmgr(true);
gm.makePanel("Tilemap");
gm.makeTextArea("Tilemap");
gm.panel.setSize(800, 800);
gm.frame.setSize(800, 800);
gm.frame.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
gm.frame.setVisible(true);
gm.panel.setVisible(true);
gm.panel.setFocusable(true);
gm.panel.requestFocus();
gm.draw();
gm.panel.addKeyListener(new java.awt.event.KeyAdapter(){
int x = 8, y = 8;
@Override
public void keyPressed(java.awt.event.KeyEvent e){
RenderedMap map = Tilemapmgr.draw("test", x-2, y-2, 10, 10);
map.overlay(new RenderedMap(2, 4, 'X'), 4, 5);
((javax.swing.JTextArea)gm.getComponent("Tilemap")).setText(map.toString());
switch(e.getKeyCode()){
case java.awt.event.KeyEvent.VK_W -> y--;
case java.awt.event.KeyEvent.VK_S -> y++;
case java.awt.event.KeyEvent.VK_A -> x--;
case java.awt.event.KeyEvent.VK_D -> x++;
case java.awt.event.KeyEvent.VK_Q -> System.exit(0);
}
gm.draw();
}
});
}
out.flush();
}
Expand Down
25 changes: 25 additions & 0 deletions MapCharacter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
public class MapCharacter extends Character {
Location location;
public MapCharacter(String name) {
this.name = name;
location = new Location(null, new Position());
}
public MapCharacter(String name, Scene scene, Position position) {
this.name = name;
location = new Location(scene, position);
}
public MapCharacter(String name, Location location) {
this.name = name;
this.location = location;
}
public void setLocation(Location location) {
this.location = location;
}
public Location getLocation() {
return location;
}
public void setPosition(int x, int y) {
location.position.x = x;
location.position.y = y;
}
}
11 changes: 11 additions & 0 deletions Position.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
public class Position {
public int x;
public int y;
public Position(int x, int y) {
this.x = x;
this.y = y;
}
public Position() {
this(0, 0);
}
}
27 changes: 27 additions & 0 deletions Scene.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

import java.util.ArrayList;

public class Scene {
String name;
Tilemap tilemap;
ArrayList<MapCharacter> characters = new ArrayList<>();
public Scene(String name) {
this.name = name;
}
public Scene(String name, Tilemap tilemap) {
this.name = name;
this.tilemap = tilemap;
}
public void addCharacter(MapCharacter character) {
characters.add(character);
}
public void removeCharacter(MapCharacter character) {
characters.remove(character);
}
public void setTilemap(Tilemap tilemap) {
this.tilemap = tilemap;
}
public Tilemap getTilemap() {
return tilemap;
}
}
18 changes: 18 additions & 0 deletions Tilemapmgr.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
public class Tilemapmgr {
static HashMap<String,Tilemap> tilemaps = new HashMap<>();
public static final char[] tilechars = {' ','.','~','-','|','/'};
static boolean active = false;
public static void loadFromImage(String name, String imagePath){
try{
// Load the image
Expand Down Expand Up @@ -127,7 +128,24 @@ public static RenderedMap draw(String name){
}
return new RenderedMap(ret);
}
public static RenderedMap draw(String name, int x, int y, int width, int height){
char[][] ret = new char[height][width];
int mapwidth = tilemaps.get(name).tileids.length, mapheight = tilemaps.get(name).tileids[0].length;
Tilemap map = tilemaps.get(name);
for(int ox=0;ox<width;ox++){
for(int oy=0;oy<height;oy++){
ret[oy][ox] = y+oy<mapheight && x+ox<mapwidth && x+ox>=0 && y+oy>=0? tilechars[map.tileids[y+oy][x+ox]] : ' ';
}
}
return new RenderedMap(ret);
}
public static Tilemap getTilemap(String name){
return tilemaps.get(name);
}
public static void activate(){
active = true;
}
public static void deactivate(){
active = false;
}
}

0 comments on commit 564f7de

Please sign in to comment.