FlappyTurtle/GameScreen.java
David96 4a1bbf2be0 Use proper Color class to animate everything
Don't need to care about not modifying Turtle.java anymore
2021-01-07 20:24:40 +01:00

146 lines
4.9 KiB
Java

import java.awt.Color;
import java.util.List;
import java.util.ArrayList;
class GameScreen implements LinearColorAnimator.AnimationListener
{
private static class Level
{
public Color birdColor;
public Color obstacleColor;
public Color backgroundColor;
public Level(Color bird, Color obst, Color bg)
{
birdColor = bird;
obstacleColor = obst;
backgroundColor = bg;
}
}
private final static ArrayList<Level> LEVELS = new ArrayList<Level>();
static
{
LEVELS.add(new Level(Color.CYAN, Color.BLACK, Color.WHITE));
LEVELS.add(new Level(Color.WHITE, Color.WHITE, Color.BLACK));
LEVELS.add(new Level(Color.ORANGE, Color.ORANGE, Color.RED));
LEVELS.add(new Level(Color.CYAN, Color.CYAN, Color.BLACK));
};
// Time in seconds until new level is reached
// You might wanna set that to 3 or so to enjoy more rapid color changes
private final static int LEVEL_TIME = 10;
private final List<Drawable> _drawables;
private final Bird _bird;
private final World _world;
private final LinearColorAnimator _backgroundAnimator, _birdAnimator, _obstacleAnimator;
private double _totalTime;
private int _try;
private int _level;
public GameScreen()
{
_drawables = new ArrayList<Drawable>();
_backgroundAnimator = new LinearColorAnimator(Color.BLACK, LEVELS.get(0).backgroundColor, 1, this);
_birdAnimator = new LinearColorAnimator(LEVELS.get(0).birdColor, LEVELS.get(0).birdColor, 1, this);
_obstacleAnimator = new LinearColorAnimator(LEVELS.get(0).obstacleColor, LEVELS.get(0).obstacleColor, 1, this);
_backgroundAnimator.start();
_bird = new Bird(this, LEVELS.get(0).birdColor);
_world = new World(this);
_world.setObstacleColor(LEVELS.get(0).obstacleColor);
_totalTime = 0;
_try = 1;
}
private void animateBack(LinearColorAnimator animator, Color target)
{
if (animator.getCurrentColor().getRGB() != target.getRGB())
{
animator.setStart(animator.getCurrentColor());
animator.setEnd(target);
animator.start();
}
}
/*
* Elapsed time is expected to be in ms
*/
public void update(double elapsed)
{
_bird.update(elapsed);
_world.update(elapsed);
_backgroundAnimator.update(elapsed);
_birdAnimator.update(elapsed);
_obstacleAnimator.update(elapsed);
_totalTime += elapsed;
System.out.print("\r" + _try + " - Time survived: " + (int)(_totalTime * 1e-3) + "s, " +
"speed: " + (int)_world.getSpeed() + "px/s");
if (_totalTime / (LEVEL_TIME * 1e3) > _level + 1)
{
++_level;
if (_level < LEVELS.size())
{
_backgroundAnimator.setStart(LEVELS.get(_level-1).backgroundColor);
_backgroundAnimator.setEnd(LEVELS.get(_level).backgroundColor);
_backgroundAnimator.start();
_birdAnimator.setStart(LEVELS.get(_level-1).birdColor);
_birdAnimator.setEnd(LEVELS.get(_level).birdColor);
_birdAnimator.start();
_obstacleAnimator.setStart(LEVELS.get(_level-1).obstacleColor);
_obstacleAnimator.setEnd(LEVELS.get(_level).obstacleColor);
_obstacleAnimator.start();
}
}
Rectangle birdRect = _bird.getRect();
if (_world.checkCollision(_bird.getRect())
|| birdRect.getY() > TurtleWelt.HEIGHT
|| birdRect.getY() + birdRect.getHeight() < 0)
{
System.out.print("\n");
++_try;
_totalTime = _level = 0;
_bird.reset();
_world.reset();
_bird.setColor(LEVELS.get(0).birdColor);
// If the background is currently not white, smoothly animate it back
animateBack(_backgroundAnimator, LEVELS.get(0).backgroundColor);
animateBack(_birdAnimator, LEVELS.get(0).birdColor);
animateBack(_obstacleAnimator, LEVELS.get(0).obstacleColor);
}
}
public void onAnimationFinished() {}
public void draw(Turtle turtle, double elapsed)
{
Color bg = _backgroundAnimator.getCurrentColor();
TurtleWelt.globaleWelt.bildschirmEinfaerben(bg.getRed(), bg.getGreen(), bg.getBlue());
if (!_birdAnimator.hasFinished())
{
_bird.setColor(_birdAnimator.getCurrentColor());
}
if (!_obstacleAnimator.hasFinished())
{
_world.setObstacleColor(_obstacleAnimator.getCurrentColor());
}
for (Drawable d : _drawables)
{
d.draw(turtle, elapsed);
}
}
public void addDrawable(Drawable d)
{
_drawables.add(d);
}
public void jump()
{
_bird.jump();
}
}