修订版 | 773ac24449fb2b8b88723bf9bf2b887bf27bf623 (tree) |
---|---|
时间 | 2021-12-03 07:04:58 |
作者 | sebastian_bugiu |
Commiter | sebastian_bugiu |
Various improvements. Animation texture region split seems to be working.
@@ -0,0 +1,45 @@ | ||
1 | +package com.headwayent.spacerocket; | |
2 | + | |
3 | +import com.badlogic.gdx.graphics.g2d.TextureRegion; | |
4 | + | |
5 | +public class Utility { | |
6 | + | |
7 | + public static TextureRegion[][] splitFlipped(TextureRegion region, int tileWidth, int tileHeight) { | |
8 | + int x = region.getRegionX(); | |
9 | + int y = Math.round(region.getV2() * region.getTexture().getHeight()); | |
10 | + int width = region.getRegionWidth(); | |
11 | + int height = region.getRegionHeight(); | |
12 | + | |
13 | + int rows = height / tileHeight; | |
14 | + int cols = width / tileWidth; | |
15 | + | |
16 | + int startX = x; | |
17 | + TextureRegion[][] tiles = new TextureRegion[rows][cols]; | |
18 | + for (int row = 0; row < rows; row++, y += tileHeight) { | |
19 | + x = startX; | |
20 | + for (int col = 0; col < cols; col++, x += tileWidth) { | |
21 | + tiles[row][col] = new TextureRegion(region.getTexture(), x, y, tileWidth, tileHeight); | |
22 | + tiles[row][col].flip(false, true); | |
23 | + } | |
24 | + } | |
25 | + | |
26 | + return tiles; | |
27 | + } | |
28 | + | |
29 | + public static void printStackTrace() { | |
30 | + System.out.println("Printing stack trace:"); | |
31 | + StackTraceElement[] elements = Thread.currentThread().getStackTrace(); | |
32 | + for (int i = 1; i < elements.length; i++) { | |
33 | + StackTraceElement s = elements[i]; | |
34 | + System.out.println("\tat " + s.getClassName() + "." + s.getMethodName() + "(" + s.getFileName() + ":" + s.getLineNumber() + ")"); | |
35 | + } | |
36 | + } | |
37 | + | |
38 | + public static boolean hasTimePassed(long beginTime, long waitTime) { | |
39 | + return System.currentTimeMillis() - beginTime > waitTime; | |
40 | + } | |
41 | + | |
42 | + public static long currentTimeMillis() { | |
43 | + return System.currentTimeMillis(); | |
44 | + } | |
45 | +} |
@@ -9,6 +9,7 @@ | ||
9 | 9 | import com.badlogic.gdx.graphics.g2d.TextureAtlas; |
10 | 10 | import com.badlogic.gdx.graphics.g2d.TextureRegion; |
11 | 11 | import com.headwayent.spacerocket.SpaceRocket; |
12 | +import com.headwayent.spacerocket.Utility; | |
12 | 13 | |
13 | 14 | public class ExtendedSprite extends Sprite { |
14 | 15 |
@@ -41,18 +42,20 @@ | ||
41 | 42 | super(textureRegion); |
42 | 43 | this.numFrames = frames; |
43 | 44 | if (frames > 1) { |
44 | - textureRegion.setRegionWidth(textureRegion.getRegionWidth() / frames); | |
45 | - setRegion(textureRegion); | |
46 | - setSize(textureRegion.getRegionWidth(), textureRegion.getRegionHeight()); | |
47 | - TextureRegion[][] split = TextureRegion.split(textureRegion.getTexture(), textureRegion.getTexture().getWidth() / frames, 1); | |
45 | + TextureRegion[][] split = Utility.splitFlipped(textureRegion, textureRegion.getRegionWidth() / frames, textureRegion.getRegionHeight()); | |
46 | + if (split[0].length != frames) { | |
47 | + throw new IllegalArgumentException(frames + " frames split len: " + split[0].length); | |
48 | + } | |
49 | + setRegion(split[0][0]); | |
50 | + setSize(split[0][0].getRegionWidth(), split[0][0].getRegionHeight()); | |
48 | 51 | animation = new Animation<>(0.25f, split[0]); |
49 | -// setRegion(split[0][0]); | |
50 | 52 | } |
53 | + setOrigin(0, 0); | |
51 | 54 | setBounds(leftBound, rightBound, upperBound, bottomBound); |
52 | 55 | this.name = name; |
53 | 56 | setVisible(false); |
54 | 57 | } |
55 | - | |
58 | + | |
56 | 59 | public void setBounds(float leftBound, float rightBound, float upperBound, float bottomBound) { |
57 | 60 | this.leftBound = leftBound; |
58 | 61 | this.rightBound = rightBound; |
@@ -81,12 +84,14 @@ | ||
81 | 84 | } |
82 | 85 | |
83 | 86 | public void reset() { |
87 | + System.out.println("Resetting " + name); | |
84 | 88 | setVisible(false); |
85 | 89 | if (animation != null) { |
86 | 90 | animationStateTime = 0; |
87 | 91 | lastFrameReached = false; |
88 | 92 | } |
89 | 93 | if (pool != null) { |
94 | + System.out.println("Readding " + name + " to pool " + pool); | |
90 | 95 | pool.add(this); |
91 | 96 | } |
92 | 97 | } |
@@ -216,6 +221,8 @@ | ||
216 | 221 | |
217 | 222 | public void setVisible(boolean visible) { |
218 | 223 | this.visible = visible; |
224 | + System.out.println("visibility: " + visible + " name: " + name); | |
225 | +// Utility.printStackTrace(); | |
219 | 226 | } |
220 | 227 | |
221 | 228 | public boolean isHiddenAfterLastFrame() { |
@@ -9,8 +9,13 @@ | ||
9 | 9 | if (sprite.isVisible()) { |
10 | 10 | throw new IllegalStateException(sprite.getName() + " is still visible"); |
11 | 11 | } |
12 | - sprite.setPool(this); | |
13 | - spriteList.add(sprite); | |
12 | +// if (spriteList.contains(sprite)) { | |
13 | +// throw new IllegalArgumentException(sprite.getName() + " is already in the list"); | |
14 | +// } | |
15 | + if (!spriteList.contains(sprite)) { | |
16 | + sprite.setPool(this); | |
17 | + spriteList.add(sprite); | |
18 | + } | |
14 | 19 | } |
15 | 20 | |
16 | 21 | public T removeOne() { |
@@ -9,7 +9,6 @@ | ||
9 | 9 | import com.headwayent.spacerocket.SpaceRocket; |
10 | 10 | |
11 | 11 | import java.util.Random; |
12 | -import java.util.concurrent.atomic.AtomicBoolean; | |
13 | 12 | |
14 | 13 | public class GraphicsManager extends LayerManager { |
15 | 14 |
@@ -884,11 +883,9 @@ | ||
884 | 883 | |
885 | 884 | if (explode) { |
886 | 885 | Vector2 p = spaceRocket.getSPPos(); |
887 | - for (int j = 0; j < explosion.length; ++j) { | |
888 | - if (explosion[j].isVisible() == false) { | |
889 | - explosion[j].launch(p.x, p.y); | |
890 | - break; | |
891 | - } | |
886 | + ExplosionSprite explosionSprite = getExplosionPool().removeOne(); | |
887 | + if (explosionSprite != null) { | |
888 | + explosionSprite.launch(p.x, p.y); | |
892 | 889 | } |
893 | 890 | } |
894 | 891 | if (spaceCanvas.getStatus().livesRemaining == 0) { |
@@ -1144,7 +1141,7 @@ | ||
1144 | 1141 | spaceRocket.changeFrame(SpaceRocketSprite.TRIPLE_FIRE_FRAME); |
1145 | 1142 | break; |
1146 | 1143 | case 4: //Invincibility |
1147 | - spaceRocket.EnableInvincibility(); | |
1144 | + spaceRocket.enableInvincibility(); | |
1148 | 1145 | break; |
1149 | 1146 | default: |
1150 | 1147 | System.out.println("Powerup Type error " + type); |
@@ -1179,7 +1176,7 @@ | ||
1179 | 1176 | otherSpaceRocket.changeFrame(SpaceRocketSprite.TRIPLE_FIRE_FRAME); |
1180 | 1177 | break; |
1181 | 1178 | case 3: |
1182 | - otherSpaceRocket.EnableInvincibility(); | |
1179 | + otherSpaceRocket.enableInvincibility(); | |
1183 | 1180 | break; |
1184 | 1181 | default: |
1185 | 1182 | break; |
@@ -1531,7 +1528,6 @@ | ||
1531 | 1528 | flamesSprite[1].setVisible(false); |
1532 | 1529 | } |
1533 | 1530 | if ((otherSpaceRocket.isInvincibilityEnabled()) && (otherSpaceRocket.isVisible())) { |
1534 | - otherSpaceRocket.DecrementInvincibilityTicks(); | |
1535 | 1531 | shieldSprite[1].setPosition(otherSpaceRocket.getX(), otherSpaceRocket.getY()); |
1536 | 1532 | shieldSprite[1].setVisible(true); |
1537 | 1533 | // shieldSprite[1].nextFrame(); |
@@ -1597,12 +1593,10 @@ | ||
1597 | 1593 | for (int j = 0; j < enemyrocketSprite.length; ++j) { |
1598 | 1594 | if ((rocket[i].isVisible()) && (enemyrocketSprite[j].isVisible())) { |
1599 | 1595 | if (rocket[i].checkCollision(enemyrocketSprite[j])) { |
1600 | - for (int k = 0; k < explosion.length; ++k) { | |
1601 | - if (explosion[k].isVisible() == false) { | |
1602 | - explosion[k].launch(rocket[i].getX(), | |
1603 | - rocket[i].getY()); | |
1604 | - break; | |
1605 | - } | |
1596 | + ExplosionSprite explosionSprite = getExplosionPool().removeOne(); | |
1597 | + if (explosionSprite != null) { | |
1598 | + explosionSprite.launch(rocket[i].getX(), | |
1599 | + rocket[i].getY()); | |
1606 | 1600 | } |
1607 | 1601 | if (i < rocket.length - 1) { |
1608 | 1602 | ++i; |
@@ -4,10 +4,10 @@ | ||
4 | 4 | |
5 | 5 | import com.badlogic.gdx.graphics.g2d.TextureAtlas; |
6 | 6 | import com.badlogic.gdx.graphics.g2d.TextureRegion; |
7 | +import com.headwayent.spacerocket.SpaceRocket; | |
7 | 8 | |
8 | 9 | public class RocketSprite extends ExtendedSprite { |
9 | 10 | |
10 | - private Random rand; | |
11 | 11 | private boolean upDirection; |
12 | 12 | private boolean doubleDamage; |
13 | 13 | public static final int _SR_SPEED = -12; |
@@ -23,8 +23,6 @@ | ||
23 | 23 | super(textureRegion, name, |
24 | 24 | leftBound, rightBound, upperBound, bottomBound, numFrames, |
25 | 25 | true); |
26 | - rand = new Random(System.currentTimeMillis()); | |
27 | - setVisible(false); | |
28 | 26 | this.upDirection = upDirection; |
29 | 27 | SR_SPEED = (ExtendedCanvas.getScreenWidth() > GraphicsManager.MIN_WIDTH || |
30 | 28 | ExtendedCanvas.getScreenHeight() > ExtendedCanvas.getScreenHeight()) ? _SR_SPEED : _SR_SPEED_SM; |
@@ -51,29 +49,6 @@ | ||
51 | 49 | return ret; |
52 | 50 | } |
53 | 51 | |
54 | - /* public void SetPosition(int x, int y) { | |
55 | - XPos = x; | |
56 | - YPos = y; | |
57 | - } | |
58 | - | |
59 | - public int GetXPos() { | |
60 | - return XPos; | |
61 | - } | |
62 | - | |
63 | - public int GetYPos() { | |
64 | - return YPos; | |
65 | - }*/ | |
66 | - /* public void SetVisible(boolean b) { | |
67 | - setVisible(b); | |
68 | - }*/ | |
69 | - /* public void reset() { | |
70 | - setVisible(false); | |
71 | - released = true; | |
72 | - }*/ | |
73 | - /* public boolean isReleased() { | |
74 | - return released; | |
75 | - }*/ | |
76 | - | |
77 | 52 | @Override |
78 | 53 | public void launch(float x, float y) { |
79 | 54 | // this.upDirection = upDirection; |
@@ -83,7 +58,7 @@ | ||
83 | 58 | if (upDirection) { |
84 | 59 | speedY = SR_SPEED; |
85 | 60 | } else { |
86 | - speedY = rand.nextInt(ENEMY_SPEED) + (ENEMY_SPEED >> 2); | |
61 | + speedY = SpaceRocket.getRandom().nextInt(ENEMY_SPEED) + (ENEMY_SPEED >> 2); | |
87 | 62 | } |
88 | 63 | setSpeed(0, speedY); |
89 | 64 | // released = true; |
@@ -6,13 +6,15 @@ | ||
6 | 6 | import com.badlogic.gdx.math.Vector2; |
7 | 7 | import com.headwayent.spacerocket.Preferences; |
8 | 8 | import com.headwayent.spacerocket.SpaceRocket; |
9 | +import com.headwayent.spacerocket.Utility; | |
9 | 10 | |
10 | 11 | import java.util.concurrent.atomic.AtomicBoolean; |
11 | 12 | |
12 | 13 | |
13 | 14 | public class SpaceRocketSprite extends ExtendedSprite { |
14 | 15 | private float initialXPos, initialYPos;// XPos, YPos; |
15 | - private int invincibilityTicks; | |
16 | + private long invincibilityTime; | |
17 | + private long currentInvincibilityTime; | |
16 | 18 | private static final int FIRE_DELAY = 500; |
17 | 19 | public static int SR_ACC_SPEED; |
18 | 20 | public static int SR_MAX_SPEED; |
@@ -36,7 +38,7 @@ | ||
36 | 38 | public static final int TRIPLE_FIRE_FRAME = 2; |
37 | 39 | public static final int INVINCIBILITY_FRAME = 0; |
38 | 40 | private static final int NUM_FRAMES = 3; |
39 | - private static final int INVINCIBILITY_TICKS = 90; | |
41 | + private static final long INVINCIBILITY_TICKS = 5000; | |
40 | 42 | private static final int _DOUBLE_DAMAGE_SPEED = -7; |
41 | 43 | private static final int _DOUBLE_DAMAGE_SPEED_SM = -4; |
42 | 44 | private static int DOUBLE_DAMAGE_SPEED; |
@@ -150,7 +152,7 @@ | ||
150 | 152 | |
151 | 153 | s.resetExplode(); |
152 | 154 | } |
153 | - if ((invincibilityTicks == 0)) { | |
155 | + if (Utility.hasTimePassed(currentInvincibilityTime, invincibilityTime)) { | |
154 | 156 | // if(gfxMgmt.getStatus().LivesRemaining - 1 > 0) { |
155 | 157 | spPos.x = this.getX(); |
156 | 158 | spPos.y = this.getY(); |
@@ -190,8 +192,13 @@ | ||
190 | 192 | return ret; |
191 | 193 | } |
192 | 194 | |
193 | - public void EnableInvincibility() { | |
194 | - invincibilityTicks = INVINCIBILITY_TICKS; | |
195 | + public void enableInvincibility() { | |
196 | + enableInvincibilityInternal(INVINCIBILITY_TICKS); | |
197 | + } | |
198 | + | |
199 | + private void enableInvincibilityInternal(long invincibilityTicks) { | |
200 | + invincibilityTime = invincibilityTicks; | |
201 | + currentInvincibilityTime = Utility.currentTimeMillis(); | |
195 | 202 | } |
196 | 203 | |
197 | 204 | public void reset() { |
@@ -201,7 +208,7 @@ | ||
201 | 208 | speedX = 0; |
202 | 209 | speedY = 0; |
203 | 210 | currentDelay = 0; |
204 | - invincibilityTicks = 30; | |
211 | + enableInvincibilityInternal(2000); | |
205 | 212 | lives = NUM_LIVES; |
206 | 213 | changeFrame(STD_FRAME); |
207 | 214 | shouldReset = true; |
@@ -264,11 +271,7 @@ | ||
264 | 271 | } |
265 | 272 | |
266 | 273 | public boolean isInvincibilityEnabled() { |
267 | - if (invincibilityTicks > 0) { | |
268 | - return true; | |
269 | - } else { | |
270 | - return false; | |
271 | - } | |
274 | + return !Utility.hasTimePassed(currentInvincibilityTime, invincibilityTime); | |
272 | 275 | } |
273 | 276 | |
274 | 277 | synchronized public void move(boolean useDelta) { |
@@ -285,12 +288,6 @@ | ||
285 | 288 | } |
286 | 289 | } |
287 | 290 | |
288 | - synchronized public void DecrementInvincibilityTicks() { | |
289 | - if (invincibilityTicks > 0) { | |
290 | - --invincibilityTicks; | |
291 | - } | |
292 | - } | |
293 | - | |
294 | 291 | synchronized public void launchRocket() { |
295 | 292 | if ((System.currentTimeMillis() - currentDelay) > FIRE_DELAY) { |
296 | 293 | currentDelay = System.currentTimeMillis(); |
@@ -425,9 +422,6 @@ | ||
425 | 422 | } |
426 | 423 | |
427 | 424 | void advance(int left, int up, int shoot) { |
428 | - if (invincibilityTicks > 0) { | |
429 | - --invincibilityTicks; | |
430 | - } | |
431 | 425 | synchronized (this) { |
432 | 426 | if (left == -1) { |
433 | 427 | if (speedX - SR_ACC_SPEED < -SR_MAX_SPEED) { |