-
Notifications
You must be signed in to change notification settings - Fork 0
/
Scroll.java
81 lines (67 loc) · 2.65 KB
/
Scroll.java
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*
* Names: Arya Afsheen & Vivian Peng
* Date: Wednesday, January 18, 2023
* Class: ICS3U7
* Teacher: Ms. Strelkovska
* Work Description: This is the Scroll class. It contains values and methods necessary for scroll movement and collision.
*/
import javax.swing.*;
import java.awt.*;
public class Scroll extends Player {
private ImageIcon scrollIcon;
private Obstacle randomObstacle;
private int playerDirection;
public Scroll(int x, int y, int width, int height) {
super(x, y, width, height);
scrollIcon = new ImageIcon("Images/scroll.png");
randomObstacle = Game.getRandomObstacle();
while(Game.isOccupiedObstacle(randomObstacle)) { // Avoids having multiple Players (either bugs or scrolls) on one Obstacle
randomObstacle = Game.getRandomObstacle();
}
Game.addOccupiedObstacle(randomObstacle);
setY((int)randomObstacle.getY() - height - 5); // Leaves a 5 pixel gap for cleanliness
setX((int)randomObstacle.getX() + ((int)(randomObstacle.getWidth() - scrollIcon.getIconWidth())));
playerDirection = 1; // Scroll spawns moving right (direction #1)
}
public void setX(int x) {
this.x = x; // Sets x
}
public void setY(int y) {
this.y = y; // Gets x
}
private boolean canMoveUp(){ // Checks if player is within upwards bounds
if(y <= ((int)randomObstacle.getY() - height - 5)) { // Leaves gap of 5 pixels for cleanliness
return true;
}
return false;
}
private boolean canMoveDown(){ // Checks if player is within downwards bounds
if(y >= ((int)randomObstacle.getY() - height - 10)) { // Leaves gap of 10 pixels for cleanliness
return true;
}
return false;
}
public void updatePosition() { // Overrides Player updatePosition method to make smaller steps
if(playerDirection == 1) {
if (canMoveUp()) { // Indicates the scroll has not reached the up-most point on the obstacle
y++;
playerDirection = 1;
}
else { // Indicates the scroll has reached the up-most point on the obstacle
playerDirection = 2;
}
}
else if(playerDirection == 2){
if (canMoveDown()) { // Indicates the scroll has not reached the down-most point on the obstacle
y--;
playerDirection = 2;
}
else { // Indicates the scroll has reached the down-most point on the obstacle
playerDirection = 1;
}
}
}
public void drawScroll(Graphics g){
g.drawImage(scrollIcon.getImage(), x, y, width, height, null); // Draws scroll
}
}