From 130152aba0c3b97da073454d1e0c0b0cc69ec43b Mon Sep 17 00:00:00 2001 From: Jan Philipp Timme Date: Tue, 9 Dec 2014 15:01:35 +0100 Subject: [PATCH] Feature: Levelname wird bei Levelbeginn eingeblendet. --- .../spacescooter/gui/LevelHeadline.java | 56 +++++++++++++++++++ .../spacescooter/level/Level.java | 5 +- 2 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 src/de/teamteamteam/spacescooter/gui/LevelHeadline.java diff --git a/src/de/teamteamteam/spacescooter/gui/LevelHeadline.java b/src/de/teamteamteam/spacescooter/gui/LevelHeadline.java new file mode 100644 index 0000000..fff32f5 --- /dev/null +++ b/src/de/teamteamteam/spacescooter/gui/LevelHeadline.java @@ -0,0 +1,56 @@ +package de.teamteamteam.spacescooter.gui; + +import java.awt.Color; +import java.awt.Font; +import java.awt.Graphics2D; + +import de.teamteamteam.spacescooter.brain.GameConfig; +import de.teamteamteam.spacescooter.entity.Entity; + +/** + * A headline that is being displayed at the beginning of each level, + * disappearing by fading out after 1-2 seconds. + */ +public class LevelHeadline extends Entity { + + + private int lifeTime; + private int lifeTimeCounter; + + private String text; + + public LevelHeadline(int x, int y, String text) { + super(x, y); + this.text = text; + this.lifeTime = 150; + this.lifeTimeCounter = this.lifeTime; + } + + /** + * Make the headline stay for a while, then let it disappear. + */ + @Override + public void update() { + if(this.lifeTimeCounter > 0) { + this.lifeTimeCounter--; + } + if(lifeTime == 0) { + this.remove(); + } + } + + /** + * Draw the Headline + */ + public void paint(Graphics2D g) { + g.setFont(new Font("Monospace", 0, 60)); + float alpha = (float) (this.lifeTimeCounter / (this.lifeTime * 1.0)); + int textWidth = g.getFontMetrics().stringWidth(this.text); + int textHeight = g.getFontMetrics().getHeight(); + int x = (GameConfig.gameScreenWidth/2 - textWidth/2); + int y = (int) (GameConfig.gameScreenHeight/2 - textHeight); + g.setColor(new Color(1.0F, 1.0F, 1.0F, alpha)); + g.drawString(this.text, x, y); + } + +} diff --git a/src/de/teamteamteam/spacescooter/level/Level.java b/src/de/teamteamteam/spacescooter/level/Level.java index e1d05a9..e952002 100644 --- a/src/de/teamteamteam/spacescooter/level/Level.java +++ b/src/de/teamteamteam/spacescooter/level/Level.java @@ -17,6 +17,7 @@ import de.teamteamteam.spacescooter.entity.obstacle.Obstacle; import de.teamteamteam.spacescooter.entity.obstacle.StoneOne; import de.teamteamteam.spacescooter.entity.obstacle.StoneThree; import de.teamteamteam.spacescooter.entity.obstacle.StoneTwo; +import de.teamteamteam.spacescooter.gui.LevelHeadline; import de.teamteamteam.spacescooter.screen.GameScreen; import de.teamteamteam.spacescooter.screen.Screen; import de.teamteamteam.spacescooter.sound.SoundSystem; @@ -83,12 +84,14 @@ public final class Level { } /** - * Initialize the level based on the LevelConfig attributes. + * Initialize the level based on the LevelConfig attributes, + * show the level name in a LevelHeadline. */ public void doBuildUp() { this.spawnEntityByAvailableName(Entity.availableNames.valueOf(this.config.background), 0, 50); GameScreen.setPlayer(new Player(200, 300)); this.backgroundMusic = SoundSystem.playSound(this.config.backgroundMusic); + new LevelHeadline(100,100, this.config.name); } /**