Optimized Ticking and Rendering #5
@ -5,6 +5,8 @@ import java.awt.Graphics2D;
|
||||
delvh
commented
Review
```suggestion
* @since Snake 1.2
```
delvh
commented
Review
```suggestion
* @since Snake 1.2
```
delvh
commented
Review
```suggestion
* @since Snake 1.2
```
delvh
commented
Review
```suggestion
* @since Snake 1.2
```
delvh
commented
Review
```suggestion
* @since Snake 1.2
```
delvh
commented
Review
```suggestion
* @since Snake 1.2
```
delvh
commented
Review
```suggestion
* @since Snake 1.2
```
delvh
commented
Review
```suggestion
* @since Snake 1.2
```
|
||||
import java.awt.Rectangle;
|
||||
|
||||
/**
|
||||
* Represents a food item.
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
* <p>
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
* Project: <strong>Snake</strong><br>
|
||||
* File: <strong>Food.java</strong><br>
|
||||
* Created: <strong>01.07.2020</strong><br>
|
||||
@ -18,23 +20,35 @@ public final class Food implements Updateable {
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
private final int lengthBonus;
|
||||
private final Rectangle bounds;
|
||||
|
||||
/**
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
* Constructs a food item.
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
*
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
* @param color the color of the food item
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
* @param lengthBonus the length added to the snake when the food item is eaten
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
* @param bounds the bounds of the food item
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
* @since Snake 1.1
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
*/
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
public Food(Color color, int lengthBonus, Rectangle bounds) {
|
||||
this.color = color;
|
||||
this.lengthBonus = lengthBonus;
|
||||
this.bounds = bounds;
|
||||
}
|
||||
|
||||
public void checkCollision(Snake snake) {
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
if (bounds.intersects(snake.getHead())) {}
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
}
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
@Override
|
||||
public void render(Graphics2D g) {
|
||||
g.setColor(color);
|
||||
g.fill(bounds);
|
||||
}
|
||||
|
||||
/**
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
* @return the length added to the snake when the food item is eaten
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
* @since Snake 1.1
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
*/
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
public int getLengthBonus() { return lengthBonus; }
|
||||
|
||||
/**
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
* @return the bounds of the food item
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
* @since Snake 1.1
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
*/
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
public Rectangle getBounds() { return bounds; }
|
||||
}
|
||||
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
@ -55,8 +55,8 @@ public final class FoodFactory {
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
FOOD_LENGTH_BONUSES[seed],
|
||||
new Rectangle(random.nextInt(width - 100) + 50,
|
||||
random.nextInt(height - 100) + 50,
|
||||
seed * 10,
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
seed * 10
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
10 + seed * 5,
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
10 + seed * 5
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
@ -19,6 +19,13 @@ public final class Handler implements Updateable {
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
|
||||
private volatile Food food;
|
||||
|
||||
/**
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
* Constructs a handler.
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
*
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
* @param snake the snake
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
* @param foodFactory the food factory
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
* @since Snake 1.1
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
*/
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
public Handler(Snake snake, FoodFactory foodFactory) {
|
||||
this.snake = snake;
|
||||
this.foodFactory = foodFactory;
|
||||
@ -29,6 +36,7 @@ public final class Handler implements Updateable {
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
public void tick() {
|
||||
snake.tick();
|
||||
food.tick();
|
||||
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
// Check for food collision
|
||||
if (snake.getHead().intersects(food.getBounds())) {
|
||||
snake.addLength(food.getLengthBonus());
|
||||
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
@ -60,7 +60,7 @@ public class Viewport extends Canvas {
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
long time = System.currentTimeMillis();
|
||||
double dt = (time - lastTime) * 1E-3;
|
||||
lastTime = time;
|
||||
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
// TODO: Delta time adjustment
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
gameRoot.tick();
|
||||
render();
|
||||
}
|
||||
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
Loading…
Reference in New Issue
Block a user