-
Notifications
You must be signed in to change notification settings - Fork 0
/
Spaceship
179 lines (135 loc) · 5.09 KB
/
Spaceship
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
168
169
170
171
172
173
174
175
176
177
178
179
package android.example.spaceinvproject;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.RectF;
public class Spaceship {
private RectF rect;
private Bullet bullet;
private int screenX;
private int screenY;
private float height;
private float length;
private float x;
private float y;
private Bitmap bitmap;
private Bitmap bitmapup;
private Bitmap bitmapdown;
private Bitmap bitmapleft;
private Bitmap bitmapright;
public Bitmap currentBitmap;
//need to know x, y coords of current location
public Bitmap bitmapbull;
private Bitmap bitmapback; //to add background
public final int STOPPED = 0;
public final int LEFT = 1;
public final int RIGHT = 2;
public final int UP = 3;
public final int DOWN = 4;
private int SpaceShipMoving = STOPPED;
private int SpaceShipSpeed;
private int movementState;
public Spaceship(Context context, int screenWidth, int screenHeight, Bullet bullet1) {
//set up spacecraft in current game environment
rect = new RectF();
screenX = screenWidth;
screenY = screenHeight;
length = (float) screenX / 10;
height = (float) screenY / 10;
x = (float) screenX / 2;
y = (float) screenY / 2;
SpaceShipSpeed = 350;
bullet = bullet1;
bitmapbull = BitmapFactory.decodeResource(context.getResources(), R.drawable.bullet);
bitmapbull = Bitmap.createScaledBitmap(bitmapbull, (int) (length), (int) (height), false);
bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.friendright); //Stretches bitmap to appropriate size for screen res
bitmap = Bitmap.createScaledBitmap(bitmap, (int) (length), (int) (height), false);
bitmapup = BitmapFactory.decodeResource(context.getResources(), R.drawable.friendup);
bitmapup = Bitmap.createScaledBitmap(bitmapup, (int) (length), (int) (height), false);
bitmapright = BitmapFactory.decodeResource(context.getResources(), R.drawable.friendright);
bitmapright = Bitmap.createScaledBitmap(bitmapright, (int) (length), (int) (height), false);
bitmapleft = BitmapFactory.decodeResource(context.getResources(), R.drawable.friendleft);
bitmapleft = Bitmap.createScaledBitmap(bitmapleft, (int) (length), (int) (height), false);
bitmapdown = BitmapFactory.decodeResource(context.getResources(), R.drawable.frienddown);
bitmapdown = Bitmap.createScaledBitmap(bitmapdown, (int) (length), (int) (height), false);
//display background, scaled to screen x and y size
bitmapback = BitmapFactory.decodeResource(context.getResources(), R.drawable.background);
bitmapback = Bitmap.createScaledBitmap(bitmapback, (int) (screenX), (int) (screenY), false);
currentBitmap = bitmapup;
}
public void setMovementState(int state) {
//sets movement of spaceship
SpaceShipMoving = state;
}
public void update(long fps) {
//required to be called from update method of application
if (SpaceShipMoving == LEFT) {
x = x - (float) SpaceShipSpeed / fps;
currentBitmap = bitmapleft;
if ((x + length) <= 0)
x = screenX;
}
if (SpaceShipMoving == RIGHT) {
x = x + (float) SpaceShipSpeed / fps;
currentBitmap = bitmapright;
if (x >= screenX)
x = 0 - length;
}
if (SpaceShipMoving == UP) {
y = y - (float) SpaceShipSpeed / fps;
currentBitmap = bitmapup;
if (y + height <= 0)
y = screenY;
}
if (SpaceShipMoving == DOWN) {
y = y + (float) SpaceShipSpeed / fps;
currentBitmap = bitmapdown;
if (y >= screenY)
y = 0 - height;
}
//if(bullet.heading == UP){
// y = y - spaceship.speed / fps
//}
//will update the movement of the bullet if active
if (bullet.getStatus()) {
bullet.update(fps);
}
rect.top = y;
rect.bottom = y + height;
rect.left = x;
rect.right = x + length;
}
public RectF getRect() {
//required to get position for collision detection
return rect;
}
public Bitmap getBitmap() {
//required to get current bitmap
return bitmap;
}
public Bitmap getBackgroundBitmap() {
return bitmapback;
}
public float getX() {
//get x coord
return x;
}
public float getY() {
//get y coord
return y;
}
public float getLength() {
//get length of image
return length;
}
public float getHeight() {
//get height of image
return height;
}
public boolean fireBullet() {
//finds current position of spaceship
float spaceshipX = getX();
float spaceshipY = getY();
return bullet.shoot(spaceshipX + length/2, spaceshipY + height/2, SpaceShipMoving);
}
}