Last active
May 12, 2023 13:54
-
-
Save ansisec/e496f460aafafc465e6dbc4774d9ca97 to your computer and use it in GitHub Desktop.
Aula 15
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Ficheiros Auxiliares | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class OrganismsLanternaUI implements IGameEngineEvolve { | |
EnvironmentManager environment; | |
Screen screen; | |
public OrganismsLanternaUI(EnvironmentManager environment) throws IOException { | |
this.environment = environment; | |
screen = new DefaultTerminalFactory().createScreen(); | |
screen.setCursorPosition(null); | |
show(); | |
} | |
@Override | |
public void evolve(IGameEngine gameEngine, long currentTime) { | |
try { | |
show(); | |
KeyStroke key = screen.pollInput(); | |
if (environment.onlyOneSpecies() || | |
( key != null && | |
(key.getKeyType() == KeyType.Escape || | |
(key.getKeyType() == KeyType.Character && | |
key.getCharacter().equals('q')))) | |
){ | |
gameEngine.stop(); | |
screen.close(); | |
} | |
} catch (IOException e) { } | |
} | |
private void show() throws IOException { | |
char[][] env = environment.getEnvironment(); | |
screen.startScreen(); | |
for (int y = 0; y < env.length; y++) { | |
for (int x = 0; x < env[0].length; x++) { | |
TextColor tc = switch(env[y][x]) { | |
case Virus.SYMBOL -> TextColor.ANSI.WHITE; | |
case Evolver.SYMBOL -> TextColor.ANSI.YELLOW; | |
default -> TextColor.ANSI.BLACK; | |
}; | |
TextColor bc = switch(env[y][x]) { | |
case Virus.SYMBOL -> TextColor.ANSI.RED; | |
case Evolver.SYMBOL -> TextColor.ANSI.BLUE; | |
default -> TextColor.ANSI.WHITE; | |
}; | |
screen.setCharacter(x,y, TextCharacter.fromCharacter(env[y][x],tc,bc)[0]); | |
} | |
} | |
screen.refresh(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment