Interactable

This commit is contained in:
Makussu 2024-06-04 10:42:45 +02:00
parent 72125f40f0
commit 76e30bef7b
8 changed files with 120 additions and 13 deletions

44
src/Bubble.java Normal file
View File

@ -0,0 +1,44 @@
import processing.core.PApplet;
/**
* The speech bubble that can be shown by all Talkable characters
*/
public class Bubble {
PApplet window;
String text;
boolean opened;
Bubble(PApplet window, String text) {
this.window = window;
this.text = text;
this.opened = false;
}
public void open() {
this.opened = true;
}
public void close() {
this.opened = false;
}
public boolean toggle() {
if (this.opened) {
close();
} else {
open();
}
return opened;
}
public void content(String text) {
this.text = text;
}
public void draw(float x, float y) {
if (opened) {
window.fill(255);
window.rect(x, y, 300, 50);
window.fill(0);
window.text(text, x, y, 300, 50);
}
}
}

View File

@ -1,3 +1,6 @@
/**
* Cell which can hold one Character and draws the character at a certain location.
*/
public class Cell { public class Cell {
float width; float width;
float x, y; float x, y;

View File

@ -1,15 +1,19 @@
import processing.core.PApplet; import processing.core.PApplet;
/**
* Class from which we define all other Entities which are seen in the game.
*/
public class Character { public class Character {
PApplet window; PApplet window;
String type; String type;
int color; int color;
boolean collidable; boolean collidable;
float x, y;
Character (PApplet window, String type) { Character (PApplet window, String type) {
this.window = window; this.window = window;
this.type = type; this.type = type;
this.collidable = false; this.collidable = true;
this.color = 0; this.color = 0;
} }
@ -17,7 +21,7 @@ public class Character {
this.window = window; this.window = window;
this.type = type; this.type = type;
this.color = color; this.color = color;
this.collidable = false; this.collidable = true;
} }
Character (PApplet window, String type, int color, boolean collidable) { Character (PApplet window, String type, int color, boolean collidable) {
@ -39,6 +43,8 @@ public class Character {
} }
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, y);
} }

View File

@ -1,6 +1,9 @@
import processing.core.PApplet; import processing.core.PApplet;
public class Enemy extends Character implements Health { /**
* Enemy which is Interactable in the way, that we can damage him. He also has health so he is also healable.
*/
public class Enemy extends Character implements Health, Interactable {
int health; int health;
int maxHealth = 20; int maxHealth = 20;
@ -26,4 +29,8 @@ public class Enemy extends Character implements Health {
health = maxHealth; health = maxHealth;
} }
} }
public void interact(Player player) {
player.damageEnemy(5, this);
}
} }

View File

@ -7,16 +7,19 @@ import java.util.ArrayList;
public class Fenster extends PApplet { public class Fenster extends PApplet {
Cell[][] tileMap; Cell[][] tileMap;
Character spot = new Character(this, "."); Character spot = new Character(this, ".", 0, false);
Character empty = new Character(this, " "); Character empty = new Character(this, " ");
Character tree = new Character(this, "A", color(10, 200, 10), true); Character tree = new Character(this, "A", color(10, 200, 10), true);
Character wall = new Character(this, "#", color(132,86,60), 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);
Character old_player_cell; Character old_player_cell;
int old_player_x; int old_player_x;
int old_player_y; int old_player_y;
UI ui = new UI(this); UI ui = new UI(this);
@Override @Override
public void settings() { public void settings() {
@ -37,8 +40,9 @@ public class Fenster extends PApplet {
tileMap[25][4].character = new Character(wall); tileMap[25][4].character = new Character(wall);
tileMap[25][5].character = new Character(wall); tileMap[25][5].character = new Character(wall);
tileMap[10][10].character = new Enemy(this); tileMap[30][30].character = old_dude;
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);
} }
@ -82,8 +86,8 @@ public class Fenster extends PApplet {
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; tileMap[old_player_y][old_player_x].character = old_player_cell;
} else if (newTile.character instanceof Enemy) { } else if (newTile.character instanceof Interactable) {
player.damageEnemy(5, (Enemy) newTile.character); ((Interactable) newTile.character).interact(player);
} }
} }
if (keyCode == this.LEFT) { if (keyCode == this.LEFT) {
@ -91,8 +95,8 @@ public class Fenster extends PApplet {
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; tileMap[old_player_y][old_player_x].character = old_player_cell;
} else if (newTile.character instanceof Enemy) { } else if (newTile.character instanceof Interactable) {
player.damageEnemy(5, (Enemy) newTile.character); ((Interactable) newTile.character).interact(player);
} }
} }
if (keyCode == this.DOWN) { if (keyCode == this.DOWN) {
@ -100,8 +104,8 @@ public class Fenster extends PApplet {
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; tileMap[old_player_y][old_player_x].character = old_player_cell;
} else if (newTile.character instanceof Enemy) { } else if (newTile.character instanceof Interactable) {
player.damageEnemy(5, (Enemy) newTile.character); ((Interactable) newTile.character).interact(player);
} }
} }
if (keyCode == this.RIGHT) { if (keyCode == this.RIGHT) {
@ -109,8 +113,8 @@ public class Fenster extends PApplet {
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; tileMap[old_player_y][old_player_x].character = old_player_cell;
} else if (newTile.character instanceof Enemy) { } else if (newTile.character instanceof Interactable) {
player.damageEnemy(5, (Enemy) newTile.character); ((Interactable) newTile.character).interact(player);
} }
} }

7
src/Interactable.java Normal file
View File

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

View File

@ -1,5 +1,8 @@
import processing.core.PApplet; import processing.core.PApplet;
/**
* This is the main player of the game we can move around with and interact.
*/
public class Player extends Character implements Health { public class Player extends Character implements Health {
int x, y; int x, y;
int health; int health;

33
src/Talkable.java Normal file
View File

@ -0,0 +1,33 @@
import processing.core.PApplet;
/**
* A Character we can talk to
*/
public class Talkable extends Character implements Interactable{
Bubble bubble;
Talkable (PApplet window, String type, int color, boolean collidable, Bubble bubble) {
super(window, type, color, collidable);
this.bubble = bubble;
}
Talkable(Talkable another) {
super(another);
}
public Talkable(PApplet window) {
super(window);
}
public void interact(Player player) {
this.bubble.toggle();
}
public void draw(float x, float y) {
this.x = x;
this.y = y;
window.fill(color);
window.text(type, x, y);
this.bubble.draw(x, y);
}
}