-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModel.java
148 lines (135 loc) · 3.64 KB
/
Model.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/**
* Model: Contains all the state and logic
* Does not contain anything about images or graphics, must ask view for that
*
* has methods to
* detect collision with boundaries
* decide next direction
* [x] provide direction
* provide location
**/
public class Model {
static int xIncr = 4;
static int yIncr = 4;
static int xloc = 0;
static int yloc = 0;
static int frameWidth;
static int frameHeight;
static int charWidth;
static int charHeight;
boolean right;
boolean down;
boolean xMoving;
boolean yMoving;
Direction dir = Direction.SOUTHEAST;
Firedir firedir = Firedir.FIRESOUTHWEST;
Jumpdir jumpdir = Jumpdir.JUMPSOUTHWEST;
public Model(int width, int height, int imgWidth, int imgHeight) {
this.frameWidth = width;
this.frameHeight = height;
this.charWidth = imgWidth;
this.charHeight = imgHeight;
System.out.println("initialized Model");
}
public void updateLocationAndDirection(boolean lBtn, boolean rBtn, boolean uBtn, boolean dBtn, boolean firing, boolean jumping) {
try {
Thread.sleep(30);
} catch (InterruptedException e) {
e.printStackTrace();
}
if(frameWidth - xloc - charWidth < 0 && rBtn){
right = false;
xMoving = false;
}
else if(xloc < 0 && lBtn){
right = true;
xMoving = false;
}
else{
right = rBtn ? true : false;
xMoving = rBtn || lBtn ? true : false;
}
if(frameHeight - yloc - charHeight < 0 && dBtn){
down = false;
yMoving = false;
}
else if(yloc < 0 && uBtn){
down = true;
yMoving = false;
}
else{
down = dBtn ? true : false;
yMoving = dBtn || uBtn ? true : false;
}
if(!firing && !jumping){
if(xMoving)
xloc += right ? xIncr : -xIncr;
if(yMoving)
yloc += down ? yIncr : -yIncr;
}
}
//returns ordinal value of the direction according to the Direction enum
public Direction getDirect() {
if(xMoving && yMoving){
if(right && down)
dir = Direction.SOUTHEAST;
else if(right && !down)
dir = Direction.NORTHEAST;
else if(!right && down)
dir = Direction.SOUTHWEST;
else
dir = Direction.NORTHWEST;
}
else if(xMoving){
dir = right ? Direction.EAST : Direction.WEST;
}
else if(yMoving){
dir = down ? Direction.SOUTH : Direction.NORTH;
}
return dir;
}
public Jumpdir getJumpDirect() {
if(xMoving && yMoving){
if(right && down)
jumpdir = Jumpdir.JUMPSOUTHEAST;
else if(right && !down)
jumpdir = Jumpdir.JUMPNORTHEAST;
else if(!right && down)
jumpdir = Jumpdir.JUMPSOUTHWEST;
else
jumpdir = Jumpdir.JUMPNORTHWEST;
}
else if(xMoving){
jumpdir = right ? Jumpdir.JUMPEAST : Jumpdir.JUMPWEST;
}
else if(yMoving){
jumpdir = down ? Jumpdir.JUMPSOUTH : Jumpdir.JUMPNORTH;
}
return jumpdir;
}
public Firedir getFireDirect() {
if(xMoving && yMoving){
if(right && down)
firedir = Firedir.FIRESOUTHEAST;
else if(right && !down)
firedir = Firedir.FIRENORTHEAST;
else if(!right && down)
firedir = Firedir.FIRESOUTHWEST;
else
firedir = Firedir.FIRENORTHWEST;
}
else if(xMoving){
firedir = right ? Firedir.FIREEAST : Firedir.FIREWEST;
}
else if(yMoving){
firedir = down ? Firedir.FIRESOUTH : Firedir.FIRENORTH;
}
return firedir;
}
public int getX() {
return this.xloc;
}
public int getY() {
return this.yloc;
}
}