-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathSnakeHead.java
More file actions
62 lines (52 loc) · 1.92 KB
/
SnakeHead.java
File metadata and controls
62 lines (52 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package com.codecool.snake.entities.snakes;
import com.codecool.snake.entities.GameEntity;
import com.codecool.snake.Globals;
import com.codecool.snake.Utils;
import com.codecool.snake.entities.Interactable;
import com.codecool.snake.entities.enemies.Enemy;
import com.codecool.snake.entities.powerups.SimplePowerUp;
import javafx.geometry.Point2D;
public class SnakeHead extends GameEntity implements SnakePart, Interactable {
private static final float turnRate = 2;
private Snake snake;
public SnakeHead(Snake snake, Point2D position) {
this.snake = snake;
setImage(Globals.getInstance().getImage("SnakeHead"));
setPosition(position);
}
public void updateRotation(SnakeControl turnDirection, float speed) {
double headRotation = getRotate();
if (turnDirection.equals(SnakeControl.TURN_LEFT)) {
headRotation = headRotation - turnRate;
}
if (turnDirection.equals(SnakeControl.TURN_RIGHT)) {
headRotation = headRotation + turnRate;
}
// set rotation and position
setRotate(headRotation);
Point2D heading = Utils.directionToVector(headRotation, speed);
setX(getX() + heading.getX());
setY(getY() + heading.getY());
}
@Override
public void apply(GameEntity entity) {
if (entity instanceof Enemy) {
snake.changeHealth(((Enemy) entity).getDamage());
}
if (entity instanceof SimplePowerUp) {
snake.addParts(4);
}
}
@Override
public void updatePosition(Point2D newPosition) {
setPosition(newPosition);
}
@Override
public Point2D getPosition() {
return new Point2D(getX(), getY());
}
@Override
public String getMessage() {
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
}
}