-
Notifications
You must be signed in to change notification settings - Fork 0
/
ScoreDisplayerCombo.java
97 lines (84 loc) · 2.53 KB
/
ScoreDisplayerCombo.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import greenfoot.Actor;
import greenfoot.GreenfootImage;
import greenfoot.GreenfootSound;
import java.util.LinkedList;
/**
* This class displays the combo below the KeyHitScore. Combo is defined
* as the count of continuous hit.
*
* @author Team APCSA 2019
* @author Yijie Gui
* @since 2019-05-29 08:38
*/
@SuppressWarnings("WeakerAccess")
public class ScoreDisplayerCombo extends Actor
{
/** Sound effect for combo break */
private static final GreenfootSound COMBO_BREAK = new GreenfootSound("combobreak.aiff");
/** Last combo */
private int lastCombo = 0;
/**
* Construct a combo displayer object
*/
public ScoreDisplayerCombo()
{
setImage((GreenfootImage) null);
}
/**
* Initialize position.
*/
public void init()
{
// Calculate position
int keyLen = Constants.GRAPHIC_TOTAL_LENGTH / Constants.NUM_COLS;
int x = Constants.GRAPHIC_COL_OFFSET + Constants.GRAPHIC_TOTAL_LENGTH / 2 - keyLen / 2;
int y = (int) Math.round(Constants.GRAPHIC_NOTE_LANDING * (2.0 / 3.0)) + Images.KEY_HIT_SCORE[0].getHeight() / 2;
// Set location
setLocation(x, y);
}
/**
* Update image
*
* @param combo Combo number
*/
public void update(int combo)
{
lastCombo = combo;
// Display combo only start at 3
if (combo < 3)
{
setImage((GreenfootImage) null);
return;
}
// Get digits
LinkedList<Integer> stack = NumberDisplayer.toDigits(combo);
// Get total width and max height
int totalWidth = 0;
int maxHeight = 0;
for (Integer digit : stack)
{
totalWidth += Images.KEY_COMBO_NUMBERS[digit].getWidth();
maxHeight = Math.max(maxHeight, Images.KEY_COMBO_NUMBERS[digit].getHeight());
}
// Create image
GreenfootImage image = new GreenfootImage(totalWidth, maxHeight);
setImage(image);
// Draw numbers
int pointer = 0;
for (Integer digit : stack)
{
GreenfootImage digitImg = Images.KEY_COMBO_NUMBERS[digit];
image.drawImage(digitImg, pointer, maxHeight - digitImg.getHeight());
pointer += digitImg.getWidth();
}
}
/**
* This method is called when player misses a note.
* Precondition: This should be called before update()
*/
public void miss()
{
// Play a sound if there's a combo before missing
if (lastCombo >= 3) COMBO_BREAK.play();
}
}