-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAnimation.java
More file actions
114 lines (89 loc) · 2.82 KB
/
Copy pathAnimation.java
File metadata and controls
114 lines (89 loc) · 2.82 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
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* The Animation Class is controlled by AnimationController.
*
* In some of my examples, you may find both baked together into a single class.
*
* The Animation Class is mostly a container for images, can be based on 1D or 2D
* array of images (for one-direction or multi-direction Animations) and also includes
* an enumerator for Direction to keep direction variables clean and manageable.
*
* @author Jordan Cohen
* @since 2017
* @version (unsure)
*/
public class Animation {
private GreenfootImage[][] directionalImages;
private GreenfootImage[] nonDirectionalImages;
private boolean directional;
private int directions;
/**
* Constructor for directional animations - should be a 2d array of GreenfootImage with
* 4 directions (dimension 1) and at least one image per direction (dimension 2)
*
* @param images 2d array of images as described above
* @param terminal true if this animation is not intended to repeat
*/
public Animation (GreenfootImage[][] images){
this.directional = true;
directionalImages = images;
directions = images.length;
}
public Animation (GreenfootImage[] images){
this.directional = false;
nonDirectionalImages = images;
directions = 1;
}
public void setImages (GreenfootImage[][] images){
directionalImages = images;
}
public void setImages (GreenfootImage[] images){
nonDirectionalImages = images;
}
public boolean isDirectional (){
return this.directional;
}
public GreenfootImage getOneImage (Direction d, int frame){
return directionalImages[d.getDirection()][frame];
}
public GreenfootImage getOneImage(int frame){
return nonDirectionalImages[frame];
}
public GreenfootImage[][] getDirectionalImages (){
return directionalImages;
}
public GreenfootImage[] getNonDirectionalImages (){
return nonDirectionalImages;
}
}
// ENUM to keep direction related code clean.
enum Direction {
RIGHT(0),
LEFT(1),
UP(2),
DOWN(3);
private final int dirCode;
private Direction (int dirCode){
this.dirCode = dirCode;
}
public int getDirection (){
return this.dirCode;
}
public static Direction fromInteger(int x) {
switch(x) {
case 0:
return RIGHT;
case 1:
return LEFT;
case 2:
return UP;
case 3:
return DOWN;
}
return null;
}
public static Direction randomDirection (){
return fromInteger (Greenfoot.getRandomNumber(4));
}
public final static int size = Direction.values().length;
}