Tiles and Multiple texts

This commit is contained in:
Makussu 2024-06-04 16:15:37 +02:00
parent 76e30bef7b
commit d3283cd0f6
12 changed files with 200 additions and 93 deletions

View File

@ -1,14 +1,18 @@
import processing.core.PApplet; import processing.core.PApplet;
import java.util.ArrayList;
import java.util.List;
/** /**
* The speech bubble that can be shown by all Talkable characters * The speech bubble that can be shown by all Talkable characters
*/ */
public class Bubble { public class Bubble {
PApplet window; Fenster window;
String text; List<String> text;
boolean opened; boolean opened;
int index = 0;
Bubble(PApplet window, String text) { Bubble(Fenster window, List<String> text) {
this.window = window; this.window = window;
this.text = text; this.text = text;
this.opened = false; this.opened = false;
@ -18,10 +22,13 @@ public class Bubble {
} }
public void close() { public void close() {
this.opened = false; this.opened = false;
index = 0;
} }
public boolean toggle() { public boolean toggle() {
if (this.opened) { if (this.opened && this.index < text.size()-1) {
this.index = this.index + 1;
} else if (this.opened && this.index < text.size()){
close(); close();
} else { } else {
open(); open();
@ -29,16 +36,16 @@ public class Bubble {
return opened; return opened;
} }
public void content(String text) { public void content(List<String> text) {
this.text = text; this.text = text;
} }
public void draw(float x, float y) { public void draw(float x, float y) {
if (opened) { if (opened) {
window.fill(255); window.fill(255);
window.rect(x, y, 300, 50); window.rect(x * window.tilewidth/2, y * window.tilewidth/2, 300, 50);
window.fill(0); window.fill(0);
window.text(text, x, y, 300, 50); window.text(text.get(index), x * window.tilewidth/2, y * window.tilewidth/2, 300, 50);
} }
} }
} }

View File

@ -8,8 +8,8 @@ public class Cell {
Cell(float x, float y, float width, Character character) { Cell(float x, float y, float width, Character character) {
this.width = width; this.width = width;
this.x = x * width/2; this.x = x;
this.y = y * width/2; this.y = y;
this.character = character; this.character = character;
} }
public boolean hasChar() { public boolean hasChar() {
@ -21,7 +21,7 @@ public class Cell {
public void draw() { public void draw() {
if (this.hasChar()) { if (this.hasChar()) {
character.draw(this.x, this.y); character.draw(x, y);
} }
} }
} }

View File

@ -4,27 +4,27 @@ import processing.core.PApplet;
* Class from which we define all other Entities which are seen in the game. * Class from which we define all other Entities which are seen in the game.
*/ */
public class Character { public class Character {
PApplet window; Fenster window;
String type; String type;
int color; int color;
boolean collidable; boolean collidable;
float x, y; float x, y;
Character (PApplet window, String type) { Character (Fenster window, String type) {
this.window = window; this.window = window;
this.type = type; this.type = type;
this.collidable = true; this.collidable = true;
this.color = 0; this.color = 0;
} }
Character (PApplet window, String type, int color) { Character (Fenster window, String type, int color) {
this.window = window; this.window = window;
this.type = type; this.type = type;
this.color = color; this.color = color;
this.collidable = true; this.collidable = true;
} }
Character (PApplet window, String type, int color, boolean collidable) { Character (Fenster window, String type, int color, boolean collidable) {
this.window = window; this.window = window;
this.type = type; this.type = type;
this.color = color; this.color = color;
@ -38,7 +38,7 @@ public class Character {
this.color = another.color; this.color = another.color;
} }
public Character(PApplet window) { public Character(Fenster window) {
this.window = window; this.window = window;
} }
@ -46,6 +46,6 @@ public class Character {
this.x = x; this.x = x;
this.y = y; this.y = y;
window.fill(color); window.fill(color);
window.text(type, x, y); window.text(type, x * window.tilewidth/2, y * window.tilewidth/2);
} }
} }

7
src/Dialog.java Normal file
View File

@ -0,0 +1,7 @@
public class Dialog implements Interactable {
Fenster window;
Bubble bubbel;
Talkable character;
public void interact() {}
}

View File

@ -7,7 +7,7 @@ public class Enemy extends Character implements Health, Interactable {
int health; int health;
int maxHealth = 20; int maxHealth = 20;
Enemy(PApplet window) { Enemy(Fenster window) {
super(window); super(window);
this.type = "Z"; this.type = "Z";
this.health = maxHealth; this.health = maxHealth;
@ -30,7 +30,7 @@ public class Enemy extends Character implements Health, Interactable {
} }
} }
public void interact(Player player) { public void interact() {
player.damageEnemy(5, this); window.player.damageEnemy(5, this);
} }
} }

View File

@ -6,43 +6,31 @@ import java.util.ArrayList;
public class Fenster extends PApplet { public class Fenster extends PApplet {
Cell[][] tileMap;
Character spot = new Character(this, ".", 0, false);
Character empty = new Character(this, " ");
Character tree = new Character(this, "A", color(10, 200, 10), true);
Character wall = new Character(this, "#", color(132,86,60), true);
Talkable old_dude = new Talkable(this, "T", color(20,20,200), true, new Bubble(this, "Hello youngster, what are you doing here?"));
Player player = new Player(this); Player player = new Player(this);
Tiles tiles = new Tiles(this, 40.0f);
Character old_player_cell; Character old_player_cell;
int old_player_x; int old_player_x;
int old_player_y; int old_player_y;
float tilewidth = 40.0f;
// STATES
boolean inFight = false;
boolean inGame = true;
boolean inMenu = false;
boolean inDialog = false;
UI ui = new UI(this); UI ui = new UI(this);
@Override @Override
public void settings() { public void settings() {
size(800, 800); size(800, 800);
tileMap = new Cell[100][100];
for(int i = 0; i < tileMap.length; i++) {
for(int j = 0; j < tileMap[0].length; j++) {
tileMap[i][j] = new Cell(j, i, 40.0f, new Character(spot));//put whatever number you want in
//here and it will insert it instead of 0's
}
}
tileMap[20][14].character = new Character(tree);
tileMap[25][0].character = new Character(wall);
tileMap[25][1].character = new Character(wall);
tileMap[25][2].character = new Character(wall);
tileMap[25][3].character = new Character(wall);
tileMap[25][4].character = new Character(wall);
tileMap[25][5].character = new Character(wall);
tileMap[30][30].character = old_dude; tiles.settings();
tileMap[10][10].character = new Enemy(this);
//this.text("lol", 0, 0); //this.text("lol", 0, 0);
//this.rect(4.0f, 4.0f, 4.0f, 4.0f); //this.rect(4.0f, 4.0f, 4.0f, 4.0f);
} }
@ -59,20 +47,15 @@ public class Fenster extends PApplet {
public void draw() { public void draw() {
background(255); background(255);
tiles.draw();
// Handle the Cell which the player was on last round // Handle the Cell which the player was on last round
if (tileMap[player.y][player.x].character != player) { if (tiles.get(player.x, player.y) != player) {
old_player_cell = tileMap[player.y][player.x].character; old_player_cell = tiles.get(player.x, player.y);
old_player_x = player.x; old_player_x = player.x;
old_player_y = player.y; old_player_y = player.y;
} }
tileMap[player.y][player.x].character = player;
for(int i = 0; i < tileMap.length; i++) { tiles.set(player.x, player.y, player);
for(int j = 0; j < tileMap[0].length; j++) {
tileMap[i][j].draw();//put whatever number you want in
//here and it will insert it instead of 0's
}
}
ui.draw(); ui.draw();
@ -80,44 +63,44 @@ public class Fenster extends PApplet {
} }
public void keyPressed(processing.event.KeyEvent event) { public void keyPressed(processing.event.KeyEvent event) {
this.keyPressed(); if (inGame) {
if (keyCode == this.UP) { if (keyCode == this.UP) {
Cell newTile = tileMap[player.y -1][player.x]; Cell newTile = tiles.get_cell(player.x, player.y - 1);
if (!newTile.character.collidable) { if (!newTile.character.collidable) {
player.y = player.y - 1; player.y = player.y - 1;
tileMap[old_player_y][old_player_x].character = old_player_cell; tiles.set(old_player_x, old_player_y, old_player_cell);
} else if (newTile.character instanceof Interactable) { } else if (newTile.character instanceof Interactable) {
((Interactable) newTile.character).interact(player); ((Interactable) newTile.character).interact();
} }
} }
if (keyCode == this.LEFT) { if (keyCode == this.LEFT) {
Cell newTile = tileMap[player.y][player.x - 1]; Cell newTile = tiles.get_cell(player.x - 1, player.y);
if (!newTile.character.collidable) { if (!newTile.character.collidable) {
player.x = player.x - 1; player.x = player.x - 1;
tileMap[old_player_y][old_player_x].character = old_player_cell; tiles.set(old_player_x, old_player_y, old_player_cell);
} else if (newTile.character instanceof Interactable) { } else if (newTile.character instanceof Interactable) {
((Interactable) newTile.character).interact(player); ((Interactable) newTile.character).interact();
} }
} }
if (keyCode == this.DOWN) { if (keyCode == this.DOWN) {
Cell newTile = tileMap[player.y +1][player.x]; Cell newTile = tiles.get_cell(player.x, player.y + 1);
if (!newTile.character.collidable) { if (!newTile.character.collidable) {
player.y = player.y + 1; player.y = player.y + 1;
tileMap[old_player_y][old_player_x].character = old_player_cell; tiles.set(old_player_x, old_player_y, old_player_cell);
} else if (newTile.character instanceof Interactable) { } else if (newTile.character instanceof Interactable) {
((Interactable) newTile.character).interact(player); ((Interactable) newTile.character).interact();
} }
} }
if (keyCode == this.RIGHT) { if (keyCode == this.RIGHT) {
Cell newTile = tileMap[player.y][player.x + 1]; Cell newTile = tiles.get_cell(player.x + 1, player.y);
if (!newTile.character.collidable) { if (!newTile.character.collidable) {
player.x = player.x + 1; player.x = player.x + 1;
tileMap[old_player_y][old_player_x].character = old_player_cell; tiles.set(old_player_x, old_player_y, old_player_cell);
} else if (newTile.character instanceof Interactable) { } else if (newTile.character instanceof Interactable) {
((Interactable) newTile.character).interact(player); ((Interactable) newTile.character).interact();
} }
} }
}
} }
} }

10
src/Fight.java Normal file
View File

@ -0,0 +1,10 @@
import java.util.List;
public class Fight extends Bubble {
Fight(Fenster window, List<String> text) {
super(window, text);
this.opened = false;
}
}

View File

@ -3,5 +3,5 @@ public interface Interactable {
* <p>This is the implementation of interact we use to interact with all entities</p> * <p>This is the implementation of interact we use to interact with all entities</p>
* @param player The main player which always interacts * @param player The main player which always interacts
*/ */
public void interact(Player player); public void interact();
} }

View File

@ -8,7 +8,7 @@ public class Player extends Character implements Health {
int health; int health;
int maxHealth = 20; int maxHealth = 20;
Player(PApplet window) { Player(Fenster window) {
super(window); super(window);
this.type = "@"; this.type = "@";
this.x = 4; this.x = 4;

View File

@ -7,7 +7,7 @@ public class Talkable extends Character implements Interactable{
Bubble bubble; Bubble bubble;
Talkable (PApplet window, String type, int color, boolean collidable, Bubble bubble) { Talkable (Fenster window, String type, int color, boolean collidable, Bubble bubble) {
super(window, type, color, collidable); super(window, type, color, collidable);
this.bubble = bubble; this.bubble = bubble;
} }
@ -15,19 +15,22 @@ public class Talkable extends Character implements Interactable{
super(another); super(another);
} }
public Talkable(PApplet window) { public Talkable(Fenster window) {
super(window); super(window);
} }
public void interact(Player player) { public void interact() {
this.bubble.toggle(); this.bubble.toggle();
} }
public void draw(float x, float y) { public void draw(float x, float y) {
this.x = x;
this.y = y;
window.fill(color); window.fill(color);
window.text(type, x, y); window.text(type, x * window.tilewidth/2, y * window.tilewidth/2);
this.bubble.draw(x, y); this.bubble.draw(x, y);
if (Math.abs(window.player.x - x) > 1 || Math.abs(window.player.y - y) > 1) {
this.bubble.close();
}
} }
} }

92
src/Tiles.java Normal file
View File

@ -0,0 +1,92 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Tiles {
Fenster window;
Cell[][] tileMap;
float tilewidth;
Talkable old_dude;
Tiles(Fenster window, float tilewidth) {
this.window = window;
this.tilewidth = tilewidth;
}
void settings() {
Character spot = new Character(window, ".", 0, false);
Character empty = new Character(window, " ");
Character tree = new Character(window, "A", window.color(10, 200, 10), true);
Character wall = new Character(window, "#", window.color(132,86,60), true);
old_dude = new Talkable(window, "T", window.color(20,20,200), true, new Bubble(window, Arrays.asList("Hey Junge!", "Was machst du hier unten?", "Du solltest hier nicht sein...")));
tileMap = new Cell[100][100];
for(int i = 0; i < tileMap.length; i++) {
for(int j = 0; j < tileMap[0].length; j++) {
tileMap[i][j] = new Cell(j, i, tilewidth, new Character(spot));//put whatever number you want in
//here and it will insert it instead of 0's
}
}
set(20, 14, new Character(tree));
set(25, 0, new Character(wall));
set(25, 1, new Character(wall));
set(25, 2, new Character(wall));
set(25, 3, new Character(wall));
set(25, 4, new Character(wall));
set(25, 5, new Character(wall));
set(25, 6, new Character(wall));
set(25, 7, new Character(wall));
set(25, 8, new Character(wall));
set(25, 9, new Character(wall));
set(25, 10, new Character(wall));
set(24, 0, new Character(wall));
set(23, 0, new Character(wall));
set(22, 0, new Character(wall));
set(21, 0, new Character(wall));
set(20, 0, new Character(wall));
set(20, 1, new Character(wall));
set(20, 2, new Character(wall));
tileMap[30][30].character = old_dude;
tileMap[10][10].character = new Enemy(window);
}
/**
* This will draw all cells and also redraw all talkable cells, to the bubble is displayed on top.
*/
void draw() {
for(int i = 0; i < tileMap.length; i++) {
for(int j = 0; j < tileMap[0].length; j++) {
tileMap[i][j].draw();
}
}
ArrayList<Cell> talkables = get(old_dude);
for (Cell i : talkables) {
i.draw();
}
}
public ArrayList<Cell> get(Character type) {
ArrayList<Cell> returns = new ArrayList<Cell>();
for(int i = 0; i < tileMap.length; i++) {
for(int j = 0; j < tileMap[0].length; j++) {
if (tileMap[i][j].character.getClass().equals(type.getClass())) {
returns.add(tileMap[i][j]);
}
}
}
return returns;
}
public Character get(int x,int y) {
return tileMap[y][x].character;
}
public Cell get_cell(int x, int y) {
return tileMap[y][x];
}
public Character set(int x, int y, Character character) {
tileMap[y][x].character = character;
return tileMap[y][x].character;
}
}

View File

@ -12,7 +12,12 @@ public class UI {
window.rect(x, 775, 300, 50); window.rect(x, 775, 300, 50);
} }
private void healthBar() {
window.text("", 20, 20);
}
public void draw() { public void draw() {
bottomBar(400, 700); bottomBar(400, 700);
healthBar();
} }
} }