-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMovement.java
More file actions
167 lines (156 loc) · 3.75 KB
/
Movement.java
File metadata and controls
167 lines (156 loc) · 3.75 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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import java.util.Random;
public class Movement { //contains many of the movement functions. TODO: transfer more from player/monster.
static Random randGenerator=new Random();
public static int numpadToX(char num){ //returns the x direction of each keypad input. default is zero because a non-directional key doesn't allow movement.
switch(num){
case('1'):
return -1;
case('2'):
return 0;
case('3'):
return 1;
case('4'):
return -1;
case('6'):
return 1;
case('7'):
return -1;
case('8'):
return 0;
case('9'):
return 1;
default:
return 0;
}
}
public static int numpadToY(char num){ //returns the y direction of each keypad input.
switch(num){
case('1'):
return 1;
case('2'):
return 1;
case('3'):
return 1;
case('4'):
return 0;
case('6'):
return 0;
case('7'):
return -1;
case('8'):
return -1;
case('9'):
return -1;
default:
return 0;
}
}
public static char leftOf(char direction){ //determine the direction to the left of this one
switch(direction){ //TODO: figure out a better way to handle directions than these commands
case '1':
return '2';
case '2':
return'3';
case '3':
return '6';
case '4':
return '1';
case '6':
return '9';
case '7':
return '4';
case '8':
return '7';
case '9':
return '8';
default:
return '0';
}
}
public static char rightOf(char direction){ //determine the direction to the right of this one
switch(direction){
case '1':
return '4';
case '2':
return'1';
case '3':
return '2';
case '4':
return '7';
case '6':
return '3';
case '7':
return '8';
case '8':
return '9';
case '9':
return '6';
default:
return '0';
}
}
public static char determineDirection(int x1, int y1, int x2, int y2){ //figure out which direction something at x1, y1 is from something at x2, y2
if(y1==y2){ //4 or 6
if(x1>x2)
return '4'; //left
if(x1<x2)
return '6'; //right
}
else if(x1==x2){//8 or 2
if(y1>y2)
return '8'; //up
else if(y1<y2)
return '2'; //down
}
else if(x1>x2){
if(y1>y2)
return '7'; //up-left
else
return '1'; //down-left
}
else{
if(y1>y2)
return '9'; //up-right
else
return '3'; //down-right
}
return '5'; //only remaining possibility: monster is in same square as target
}
public static Tile nearestOpenTile(Monster movingMonster, char direction){
char left=direction;
char right=direction;
while(!canMove(movingMonster, direction)){
switch(randGenerator.nextInt(2)){ //at random, choose one of two actions:
case(0): //action 1: try moving left. otherwise, move right.
left=leftOf(left);
if(canMove(movingMonster, left))
return tileInDirection(movingMonster, left);
else{
right=rightOf(right);
if(canMove(movingMonster, right))
return tileInDirection(movingMonster, right);
}
case(1):
right=rightOf(right);
if(canMove(movingMonster, right))
return tileInDirection(movingMonster, right);
else{
left=leftOf(left);
if(canMove(movingMonster, left))
return tileInDirection(movingMonster, left);
}
if(left==direction||right==direction) //this indicates a full cirlce, meaning the monster is surrounded and cannot move.
return movingMonster.currentTile;
}
}
return tileInDirection(movingMonster, direction);
}
public static boolean canMove(Monster movingMonster, char direction){
return tileInDirection(movingMonster,direction).isPassable;
}
private static Tile tileInDirection(Monster movingMonster, char direction) {
int tileX=movingMonster.getXPos()+numpadToX(direction);
int tileY=movingMonster.getYPos()+numpadToY(direction);
return movingMonster.currentLevel.getTile(tileX, tileY);
}
}