Rev 900 | Details | Compare with Previous | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 894 | chris | 1 | package com.gebauz.burutaru.game.entities; |
| 2 | |||
| 3 | import com.gebauz.bauzoid.graphics.sprite.Sprite; |
||
| 4 | import com.gebauz.burutaru.GameConsts; |
||
| 5 | import com.gebauz.burutaru.game.GameLogic; |
||
| 6 | |||
| 7 | public class StarField extends Entity |
||
| 8 | { |
||
| 9 | |||
| 10 | // Constants======================================================================================== |
||
| 11 | |||
| 12 | public static final int MAX_STARS = 28; |
||
| 13 | |||
| 14 | public static final float MIN_SPEED = 10; |
||
| 15 | public static final float MAX_SPEED = 400; |
||
| 16 | |||
| 17 | public static final float MIN_SIZE = 2; |
||
| 18 | public static final float MAX_SIZE = 8; |
||
| 19 | |||
| 20 | // Embedded Types=================================================================================== |
||
| 21 | |||
| 22 | public class Particle |
||
| 23 | { |
||
| 24 | public float x = 0; |
||
| 25 | public float y = 0; |
||
| 26 | public float w = 0; |
||
| 27 | public float h = 0; |
||
| 28 | public float speed = 0; |
||
| 29 | public boolean isAlive = false; |
||
| 30 | |||
| 31 | public Particle() |
||
| 32 | { |
||
| 33 | x = getGame().getRandomFloat(0, GameConsts.VIRTUAL_SCREEN_WIDTH+150); |
||
| 34 | y = getGame().getRandomFloat(0, GameConsts.VIRTUAL_SCREEN_HEIGHT); |
||
| 35 | w = getGame().getRandomFloat(MIN_SIZE, MAX_SIZE); |
||
| 36 | h = w/2; |
||
| 37 | speed = getGame().getRandomFloat(MIN_SPEED, MAX_SPEED); |
||
| 38 | isAlive = true; |
||
| 39 | } |
||
| 40 | |||
| 41 | public void spawn() |
||
| 42 | { |
||
| 43 | x = GameConsts.VIRTUAL_SCREEN_WIDTH + getGame().getRandomFloat(10, 150); |
||
| 44 | y = getGame().getRandomFloat(0, GameConsts.VIRTUAL_SCREEN_HEIGHT); |
||
| 45 | w = getGame().getRandomFloat(MIN_SIZE, MAX_SIZE); |
||
| 46 | h = w/2; |
||
| 47 | speed = getGame().getRandomFloat(MIN_SPEED, MAX_SPEED); |
||
| 48 | isAlive = true; |
||
| 49 | } |
||
| 50 | |||
| 51 | public void update(float deltaTime) |
||
| 52 | { |
||
| 53 | x -= speed * deltaTime; |
||
| 950 | chris | 54 | |
| 55 | x += getGameLogic().getGlobalScrollX() * deltaTime; |
||
| 56 | y += getGameLogic().getGlobalScrollY() * deltaTime; |
||
| 57 | |||
| 894 | chris | 58 | if (x < -w) |
| 59 | isAlive = false; |
||
| 60 | } |
||
| 61 | |||
| 62 | public void render() |
||
| 63 | { |
||
| 900 | chris | 64 | mStarSprite.param.x = x; |
| 65 | mStarSprite.param.y = y; |
||
| 66 | mStarSprite.param.w = w; |
||
| 67 | mStarSprite.param.h = h; |
||
| 894 | chris | 68 | mStarSprite.centerPivot(); |
| 69 | mStarSprite.render(); |
||
| 70 | } |
||
| 71 | } |
||
| 72 | |||
| 73 | // Fields=========================================================================================== |
||
| 74 | |||
| 75 | private Particle mParticles[] = new Particle[MAX_STARS]; |
||
| 76 | private Sprite mStarSprite = null; |
||
| 77 | |||
| 78 | // Methods========================================================================================== |
||
| 79 | |||
| 80 | public StarField(GameLogic gameLogic) |
||
| 81 | { |
||
| 82 | super(gameLogic); |
||
| 83 | mStarSprite = new Sprite(getGraphics(), "data/textures/star.png"); |
||
| 84 | } |
||
| 85 | |||
| 86 | @Override |
||
| 87 | public void initAsync() |
||
| 88 | { |
||
| 89 | mStarSprite.initAsync(); |
||
| 90 | |||
| 91 | } |
||
| 92 | |||
| 93 | @Override |
||
| 94 | public void init() |
||
| 95 | { |
||
| 96 | mStarSprite.init(); |
||
| 97 | |||
| 98 | for (int i = 0; i < mParticles.length; i++) |
||
| 99 | { |
||
| 100 | mParticles[i] = new Particle(); |
||
| 101 | } |
||
| 102 | } |
||
| 103 | |||
| 104 | @Override |
||
| 105 | public void exit() |
||
| 106 | { |
||
| 107 | if (mStarSprite != null) |
||
| 108 | { |
||
| 109 | mStarSprite.dispose(); |
||
| 110 | mStarSprite = null; |
||
| 111 | } |
||
| 112 | } |
||
| 113 | |||
| 114 | @Override |
||
| 115 | public void update(float deltaTime) |
||
| 116 | { |
||
| 117 | for (int i = 0; i < mParticles.length; i++) |
||
| 118 | { |
||
| 119 | if (mParticles[i].isAlive) |
||
| 120 | mParticles[i].update(deltaTime); |
||
| 121 | |||
| 122 | if (!mParticles[i].isAlive) |
||
| 123 | spawn(i); |
||
| 124 | } |
||
| 125 | } |
||
| 126 | |||
| 127 | @Override |
||
| 128 | public void render() |
||
| 129 | { |
||
| 130 | for (int i = 0; i < mParticles.length; i++) |
||
| 131 | { |
||
| 132 | if (mParticles[i].isAlive) |
||
| 133 | mParticles[i].render(); |
||
| 134 | } |
||
| 135 | } |
||
| 136 | |||
| 137 | public void spawn(int index) |
||
| 138 | { |
||
| 139 | if (!mParticles[index].isAlive) |
||
| 140 | { |
||
| 141 | mParticles[index].spawn(); |
||
| 142 | } |
||
| 143 | } |
||
| 144 | |||
| 145 | // Getters/Setters================================================================================== |
||
| 146 | |||
| 147 | } |