Moved all Sprites into 3 classes,where their initialization and change is handled

This commit is contained in:
Seraina 2022-05-14 12:00:32 +02:00
parent 778a5f224d
commit 76ab7535b5
3 changed files with 318 additions and 0 deletions

View File

@ -0,0 +1,215 @@
package ch.unibas.dmi.dbis.cs108.multiplayer.client.gui;
import java.net.URL;
import javafx.scene.image.Image;
/**
* This class contains all Sprites as images to be accessed by other classes, this way the loading only happens once and the handling
* of day to night sprites changes can be simplified
*/
public class Sprites {
private static Image bg;
private static Image shadow;
private static Image secondWagon;
private static Image emptyWagon;
private static Image[] rooms = new Image[6];
private static Image emptyWagonWall;
private static Image fullWagon;
private static Image loki;
private static Image[] wheels = new Image[26];
private static Image[] bells = new Image[17];
private static Image fg;
public static Image getBg() {
return bg;
}
public static Image getShadow() {
return shadow;
}
public static Image getSecondWagon() {
return secondWagon;
}
public static Image getEmptyWagon() {
return emptyWagon;
}
public static Image[] getRooms() {
return rooms;
}
public static Image getARoom(int position) {
return rooms[position];
}
public static Image getEmptyWagonWall() {
return emptyWagonWall;
}
public static Image getFullWagon() {
return fullWagon;
}
public static Image getLoki() {
return loki;
}
public static Image[] getWheels() {
return wheels;
}
public static Image[] getBells() {
return bells;
}
public static Image getFg() {
return fg;
}
/**
* Sets all Images of this class to the Day Version
*/
public static void setDaySprites(String[] roles, boolean[] kickedOff) {
try {
bg = SpritesDay.bg;
shadow = SpritesDay.shadow;
secondWagon = SpritesDay.secondWagon;
emptyWagon = SpritesDay.emptyWagon;
for (int i = 0; i < roles.length; i++) {
rooms[i] = getRoomDay(i, roles[i], kickedOff);
}
emptyWagonWall = SpritesDay.emptyWagonWall;
fullWagon = SpritesDay.fullWagon;
loki = SpritesDay.loki;
wheels = SpritesDay.wheels;
bells = SpritesDay.bells;
fg = SpritesDay.fg;
} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}
/**
* Sets all Images of this class to the Night version, takes into account which room is a ghost
* Room and which isn't
*
* @param roles a String containing the roles of the Passengers (g/h/s) from left to
*/
public static void setNightSprites(String[] roles, boolean[] kickedOff) {
bg = SpritesNight.bg;
shadow = SpritesNight.shadow;
secondWagon = SpritesNight.secondWagon;
emptyWagon = SpritesNight.emptyWagon;
for (int i = 0; i < roles.length; i++) {
rooms[i] = getRoomNight(i, roles[i], kickedOff);
}
emptyWagonWall = SpritesNight.emptyWagonWall;
fullWagon = SpritesNight.fullWagon;
loki = SpritesNight.loki;
wheels = SpritesNight.wheels;
bells = SpritesNight.bells;
fg = SpritesNight.fg;
}
/**
* Returns a room Image from SpritesDay, corresponding to the position and the role of the passenger in that room
* @param position the position of the room integer 0-5
* @param suffix the role of the passenger eiter (h/s)
* @return the Image if parameters are passable otherwise null (ie position = 6)
*/
public static Image getRoomDay(int position, String suffix, boolean[] kickedOff) {
switch (position) {
case 0:
if (kickedOff[0]) {
return SpritesDay.room0Spectator;
}
return SpritesDay.room0;
case 1:
if (kickedOff[1]) {
return SpritesDay.room1Spectator;
}
return SpritesDay.room1;
case 2:
if (kickedOff[2]) {
return SpritesDay.room2Spectator;
}
return SpritesDay.room2;
case 3:
if (kickedOff[3]) {
return SpritesDay.room3Spectator;
}
return SpritesDay.room3;
case 4:
if (kickedOff[4]) {
return SpritesDay.room4Spectator;
}
return SpritesDay.room4;
case 5:
if (kickedOff[5]) {
return SpritesDay.room5Spectator;
}
return SpritesDay.room5;
default:
return null;
}
}
/**
* Returns a room Image from SpritesNight, corresponding to the position and the role of the passenger in that room
* @param position the position of the room integer 0-5
* @param suffix the role of the passenger eiter (h/s/g)
* @return the Image if parameters are passable otherwise null (ie position = 6)
*/
public static Image getRoomNight(int position, String suffix, boolean[] kickedOff) {
switch (position) {
case 0:
if (kickedOff[0]) {
return SpritesNight.room0Spectator;
} else if ("g".equals(suffix)) {
return SpritesNight.room0LightsOn;
}
return SpritesNight.room0;
case 1:
if (kickedOff[1]) {
return SpritesNight.room1Spectator;
} else if ("g".equals(suffix)) {
return SpritesNight.room1LightsOn;
}
return SpritesNight.room1;
case 2:
if (kickedOff[2]) {
return SpritesNight.room2Spectator;
} else if ("g".equals(suffix)) {
return SpritesNight.room2LightsOn;
}
return SpritesNight.room2;
case 3:
if (kickedOff[3]) {
return SpritesNight.room3Spectator;
} else if ("g".equals(suffix)) {
return SpritesNight.room3LightsOn;
}
return SpritesNight.room3;
case 4:
if (kickedOff[4]) {
return SpritesNight.room4Spectator;
} else if ("g".equals(suffix)) {
return SpritesNight.room4LightsOn;
}
return SpritesNight.room4;
case 5:
if (kickedOff[5]) {
return SpritesNight.room5Spectator;
} else if ("g".equals(suffix)) {
return SpritesNight.room5LightsOn;
}
return SpritesNight.room5;
default:
return null;
}
}
}

View File

@ -0,0 +1,65 @@
package ch.unibas.dmi.dbis.cs108.multiplayer.client.gui;
import javafx.scene.image.Image;
public class SpritesDay {
private static final String path = "ch/unibas/dmi/dbis/cs108/multiplayer/client/gui/game/Day/";
public static final Image bg = new Image(path + "BG_small.jpg");
public static final Image shadow = new Image(path + "Shadow_Below_Train.png");
public static final Image secondWagon = new Image(path + "Second_Wagon.png");
public static final Image emptyWagon = new Image(path + "Empty_Wagon.png");
public static final Image room0 = new Image(path + "Room1.png");
public static final Image room1 = new Image(path + "Room2.png");
public static final Image room2 = new Image(path + "Room3.png");
public static final Image room3 = new Image(path + "Room4.png");
public static final Image room4 = new Image(path + "Room5.png");
public static final Image room5 = new Image(path + "Room6.png");
public static final Image room0Spectator = new Image(path + "Room1_Spectator.png");
public static final Image room1Spectator = new Image(path + "Room2_Spectator.png");
public static final Image room2Spectator = new Image(path + "Room3_Spectator.png");
public static final Image room3Spectator = new Image(path + "Room4_Spectator.png");
public static final Image room4Spectator = new Image(path + "Room5_Spectator.png");
public static final Image room5Spectator = new Image(path + "Room6_Spectator.png");
public static final Image emptyWagonWall = new Image(path + "Empty_Wagon_Wall.png");
public static final Image fullWagon = new Image(path + "Full_Wagon.png");
public static final Image loki = new Image(path + "Loki.png");
public static final Image[] wheels = new Image[26];
public static final Image[] bells = new Image[17];
public static final Image fg = new Image(path + "Foreground_small.png");
public static void setWheels() {
try {
for (int i = 1; i <= 26; i++) {
String url;
if (i < 10) {
url =
path + "Wheels/Image000" + i + ".png";
} else {
url = path + "Wheels/Image00" + i + ".png";
}
wheels[i-1] = new Image(url);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void setBells() {
try {
for (int i = 1; i <= 17; i++) {
String url;
if (i < 10) {
url =
path + "Bell/Image000" + i + ".png";
} else {
url = path + "Bell/Image00" + i + ".png";
}
bells[i-1] = new Image(url);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

View File

@ -0,0 +1,38 @@
package ch.unibas.dmi.dbis.cs108.multiplayer.client.gui;
import javafx.scene.image.Image;
public class SpritesNight {
private static final String path = "ch/unibas/dmi/dbis/cs108/multiplayer/client/gui/game/Night/";
public static final Image bg = new Image(path + "BG_small.jpg");
public static final Image shadow = new Image(path + "Shadow_Below_Train.png");
public static final Image secondWagon = new Image(path + "Second_Wagon.png");
public static final Image emptyWagon = new Image(path + "Empty_Wagon.png");
public static final Image room0 = new Image(path + "Room1_Lights_Off.png");
public static final Image room1 = new Image(path + "Room2_Lights_Off.png");
public static final Image room2 = new Image(path + "Room3_Lights_Off.png");
public static final Image room3 = new Image(path + "Room4_Lights_Off.png");
public static final Image room4 = new Image(path + "Room5_Lights_Off.png");
public static final Image room5 = new Image(path + "Room6_Lights_Off.png");
public static final Image room0LightsOn = new Image(path + "Room1_Lights_On.png");
public static final Image room1LightsOn = new Image(path + "Room2_Lights_On.png");
public static final Image room2LightsOn = new Image(path + "Room3_Lights_On.png");
public static final Image room3LightsOn = new Image(path + "Room4_Lights_On.png");
public static final Image room4LightsOn = new Image(path + "Room5_Lights_On.png");
public static final Image room5LightsOn = new Image(path + "Room6_Lights_On.png");
public static final Image room0Spectator = new Image(path + "Room1_Spectator.png");
public static final Image room1Spectator = new Image(path + "Room2_Spectator.png");
public static final Image room2Spectator = new Image(path + "Room3_Spectator.png");
public static final Image room3Spectator = new Image(path + "Room4_Spectator.png");
public static final Image room4Spectator = new Image(path + "Room5_Spectator.png");
public static final Image room5Spectator = new Image(path + "Room6_Spectator.png");
public static final Image emptyWagonWall = new Image(path + "Empty_Wagon_Wall.png");
public static final Image fullWagon = new Image(path + "Full_Wagon.png");
public static final Image loki = new Image(path + "Loki.png");
public static final Image[] wheels = SpritesDay.wheels;
public static final Image[] bells = SpritesDay.bells;
public static final Image fg = new Image(path + "Foreground_small.png");
}