Rev 997 | Details | Compare with Previous | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 837 | chris | 1 | package com.gebauz.burutaru.game; |
| 2 | |||
| 930 | chris | 3 | import java.util.Vector; |
| 4 | |||
| 868 | chris | 5 | import com.badlogic.gdx.graphics.Texture.TextureFilter; |
| 837 | chris | 6 | import com.gebauz.bauzoid.game.Game; |
| 7 | import com.gebauz.bauzoid.game.GameObject; |
||
| 838 | chris | 8 | import com.gebauz.bauzoid.game.entities.EntityManager; |
| 867 | chris | 9 | import com.gebauz.bauzoid.graphics.sprite.Sprite; |
| 956 | chris | 10 | import com.gebauz.bauzoid.graphics.sprite.SpriteInstance; |
| 11 | import com.gebauz.bauzoid.graphics.sprite.SpriteParameters; |
||
| 906 | chris | 12 | import com.gebauz.bauzoid.math.Matrix4; |
| 887 | chris | 13 | import com.gebauz.bauzoid.math.Vector2; |
| 950 | chris | 14 | import com.gebauz.bauzoid.math.collision.AABoundingBox; |
| 906 | chris | 15 | import com.gebauz.bauzoid.math.collision.Shape; |
| 867 | chris | 16 | import com.gebauz.burutaru.GameConsts; |
| 967 | chris | 17 | import com.gebauz.burutaru.game.entities.BeamShots; |
| 853 | chris | 18 | import com.gebauz.burutaru.game.entities.BlobEnemies; |
| 893 | chris | 19 | import com.gebauz.burutaru.game.entities.CarrotEnemies; |
| 930 | chris | 20 | import com.gebauz.burutaru.game.entities.Enemy; |
| 866 | chris | 21 | import com.gebauz.burutaru.game.entities.Explosions; |
| 993 | chris | 22 | import com.gebauz.burutaru.game.entities.ICollidable; |
| 961 | chris | 23 | import com.gebauz.burutaru.game.entities.ImpactExplosions; |
| 848 | chris | 24 | import com.gebauz.burutaru.game.entities.PlasmaShots; |
| 956 | chris | 25 | import com.gebauz.burutaru.game.entities.PowerUpBomb; |
| 886 | chris | 26 | import com.gebauz.burutaru.game.entities.PowerUpMissile; |
| 877 | chris | 27 | import com.gebauz.burutaru.game.entities.PowerUps; |
| 932 | chris | 28 | import com.gebauz.burutaru.game.entities.Projectile; |
| 838 | chris | 29 | import com.gebauz.burutaru.game.entities.Ship; |
| 894 | chris | 30 | import com.gebauz.burutaru.game.entities.StarField; |
| 867 | chris | 31 | import com.gebauz.burutaru.game.entities.VirtualPad; |
| 983 | chris | 32 | import com.gebauz.burutaru.game.level.Level; |
| 837 | chris | 33 | |
| 34 | public class GameLogic extends GameObject |
||
| 35 | { |
||
| 36 | |||
| 37 | |||
| 38 | // Constants======================================================================================== |
||
| 867 | chris | 39 | |
| 40 | |||
| 879 | chris | 41 | public static final float SPAWN_INTERVAL_MIN = 0.2f; |
| 965 | chris | 42 | public static final float SPAWN_INTERVAL_MAX = 1.5f; |
| 837 | chris | 43 | |
| 44 | // Embedded Types=================================================================================== |
||
| 45 | |||
| 46 | // Fields=========================================================================================== |
||
| 838 | chris | 47 | |
| 48 | private EntityManager mEntities = new EntityManager(); |
||
| 49 | |||
| 50 | private Ship mShip = null; |
||
| 51 | private ShipController mShipController = null; |
||
| 879 | chris | 52 | private ShipParameters mShipParameters = null; |
| 848 | chris | 53 | |
| 54 | private PlasmaShots mPlasmaShots = null; |
||
| 967 | chris | 55 | private BeamShots mBeamShots = null; |
| 853 | chris | 56 | |
| 57 | private BlobEnemies mBlobEnemies = null; |
||
| 893 | chris | 58 | private CarrotEnemies mCarrotEnemies = null; |
| 866 | chris | 59 | |
| 60 | private Explosions mExplosions = null; |
||
| 867 | chris | 61 | |
| 62 | private VirtualPad mVirtualPad = null; |
||
| 877 | chris | 63 | |
| 64 | private PowerUps mPowerUps = null; |
||
| 886 | chris | 65 | private PowerUpMissile mPowerUpMissile = null; |
| 956 | chris | 66 | private PowerUpBomb mPowerUpBomb = null; |
| 961 | chris | 67 | private ImpactExplosions mImpactExplosions = null; |
| 894 | chris | 68 | |
| 69 | private StarField mStarField = null; |
||
| 983 | chris | 70 | |
| 71 | private Level mLevel = null; |
||
| 837 | chris | 72 | |
| 867 | chris | 73 | // TEMP: |
| 74 | private Sprite mBackground = null; |
||
| 75 | private float mSpawnTimer = 0.0f; |
||
| 894 | chris | 76 | |
| 77 | private float mGlobalScrollX = 0.0f; |
||
| 78 | private float mGlobalScrollY = 0.0f; |
||
| 867 | chris | 79 | |
| 837 | chris | 80 | // Methods========================================================================================== |
| 81 | |||
| 82 | public GameLogic(Game game) |
||
| 83 | { |
||
| 84 | super(game); |
||
| 867 | chris | 85 | |
| 86 | // TEMP: |
||
| 87 | mBackground = new Sprite(getGraphics(), "data/textures/background.png"); |
||
| 837 | chris | 88 | } |
| 89 | |||
| 839 | chris | 90 | public void createEntities() |
| 838 | chris | 91 | { |
| 92 | mShip = mEntities.addEntity(new Ship(this)); |
||
| 848 | chris | 93 | mPlasmaShots = mEntities.addEntity(new PlasmaShots(this)); |
| 967 | chris | 94 | mBeamShots = mEntities.addEntity(new BeamShots(this)); |
| 853 | chris | 95 | mBlobEnemies = mEntities.addEntity(new BlobEnemies(this)); |
| 893 | chris | 96 | mCarrotEnemies = mEntities.addEntity(new CarrotEnemies(this)); |
| 866 | chris | 97 | mExplosions = mEntities.addEntity(new Explosions(this)); |
| 867 | chris | 98 | mVirtualPad = mEntities.addEntity(new VirtualPad(this)); |
| 877 | chris | 99 | mPowerUps = mEntities.addEntity(new PowerUps(this)); |
| 886 | chris | 100 | mPowerUpMissile = mEntities.addEntity(new PowerUpMissile(this)); |
| 956 | chris | 101 | mPowerUpBomb = mEntities.addEntity(new PowerUpBomb(this)); |
| 961 | chris | 102 | mImpactExplosions = mEntities.addEntity(new ImpactExplosions(this)); |
| 894 | chris | 103 | mStarField = mEntities.addEntity(new StarField(this)); |
| 983 | chris | 104 | |
| 105 | mLevel = mEntities.addEntity(new Level(this)); |
||
| 839 | chris | 106 | } |
| 107 | |||
| 108 | public void initAsync() |
||
| 109 | { |
||
| 110 | createEntities(); |
||
| 838 | chris | 111 | |
| 839 | chris | 112 | mEntities.initAsync(); |
| 867 | chris | 113 | |
| 114 | // TEMP: |
||
| 115 | mBackground.initAsync(); |
||
| 839 | chris | 116 | } |
| 117 | |||
| 118 | public void init() |
||
| 119 | { |
||
| 120 | mEntities.init(); |
||
| 121 | |||
| 867 | chris | 122 | // TEMP: |
| 123 | mBackground.init(); |
||
| 868 | chris | 124 | mBackground.getTexture().setFilter(TextureFilter.Linear, TextureFilter.Linear); |
| 867 | chris | 125 | |
| 879 | chris | 126 | mShipController = new ShipController(this); |
| 127 | mShipParameters = new ShipParameters(this); |
||
| 906 | chris | 128 | |
| 953 | chris | 129 | //mCarrotEnemies.spawn(300.0f, 200.0f); |
| 837 | chris | 130 | } |
| 131 | |||
| 132 | public void exit() |
||
| 838 | chris | 133 | { |
| 134 | mShipController = null; |
||
| 879 | chris | 135 | mShipParameters = null; |
| 838 | chris | 136 | |
| 137 | mEntities.exit(); |
||
| 837 | chris | 138 | } |
| 139 | |||
| 140 | public void update(float deltaTime) |
||
| 838 | chris | 141 | { |
| 949 | chris | 142 | mGlobalScrollX = -75.0f; |
| 950 | chris | 143 | //mGlobalScrollY = -15.0f; |
| 894 | chris | 144 | |
| 838 | chris | 145 | mShipController.handleInput(deltaTime); |
| 146 | |||
| 147 | mShip.update(deltaTime); |
||
| 848 | chris | 148 | mPlasmaShots.update(deltaTime); |
| 967 | chris | 149 | mBeamShots.update(deltaTime); |
| 853 | chris | 150 | mBlobEnemies.update(deltaTime); |
| 893 | chris | 151 | mCarrotEnemies.update(deltaTime); |
| 866 | chris | 152 | mExplosions.update(deltaTime); |
| 885 | chris | 153 | mPowerUps.update(deltaTime); |
| 886 | chris | 154 | mPowerUpMissile.update(deltaTime); |
| 956 | chris | 155 | mPowerUpBomb.update(deltaTime); |
| 961 | chris | 156 | mImpactExplosions.update(deltaTime); |
| 867 | chris | 157 | |
| 894 | chris | 158 | mStarField.update(deltaTime); |
| 159 | |||
| 983 | chris | 160 | mLevel.update(deltaTime); |
| 161 | |||
| 977 | chris | 162 | checkShipHit(); |
| 163 | |||
| 867 | chris | 164 | // TEMP: |
| 944 | chris | 165 | mSpawnTimer -= deltaTime; |
| 867 | chris | 166 | if (mSpawnTimer < 0.0f) |
| 167 | { |
||
| 893 | chris | 168 | int which = getGame().getRandomInt(0, 1); |
| 971 | chris | 169 | if (which == 0) |
| 893 | chris | 170 | mBlobEnemies.spawn(GameConsts.VIRTUAL_SCREEN_WIDTH + 100.0f, getGame().getRandomFloat(100.0f, GameConsts.VIRTUAL_SCREEN_HEIGHT - 100.0f)); |
| 965 | chris | 171 | else if (which == 1) |
| 971 | chris | 172 | mCarrotEnemies.spawn(GameConsts.VIRTUAL_SCREEN_WIDTH + 100.0f, getGame().getRandomFloat(100.0f, GameConsts.VIRTUAL_SCREEN_HEIGHT - 100.0f)); |
| 969 | chris | 173 | |
| 971 | chris | 174 | //mCarrotEnemies.spawn(GameConsts.VIRTUAL_SCREEN_WIDTH + 100.0f, 200); |
| 893 | chris | 175 | |
| 867 | chris | 176 | mSpawnTimer = getGame().getRandomFloat(SPAWN_INTERVAL_MIN, SPAWN_INTERVAL_MAX); |
| 944 | chris | 177 | } |
| 837 | chris | 178 | } |
| 179 | |||
| 180 | public void render() |
||
| 838 | chris | 181 | { |
| 867 | chris | 182 | // TEMP: |
| 900 | chris | 183 | mBackground.param.x = 0; |
| 184 | mBackground.param.y = 0; |
||
| 185 | mBackground.param.w = GameConsts.VIRTUAL_SCREEN_WIDTH; |
||
| 186 | mBackground.param.h = GameConsts.VIRTUAL_SCREEN_HEIGHT; |
||
| 187 | mBackground.param.pivotX = 0; |
||
| 188 | mBackground.param.pivotY = 0; |
||
| 867 | chris | 189 | mBackground.render(); |
| 190 | |||
| 983 | chris | 191 | |
| 192 | // render level |
||
| 193 | // TODO: might be required to render it in separated passes (foreground/background) |
||
| 194 | mLevel.renderBackground(); |
||
| 195 | |||
| 894 | chris | 196 | // render background |
| 197 | mStarField.render(); |
||
| 198 | |||
| 886 | chris | 199 | // render powerups |
| 878 | chris | 200 | mPowerUps.render(); |
| 201 | |||
| 886 | chris | 202 | // render weapons |
| 848 | chris | 203 | mPlasmaShots.render(); |
| 967 | chris | 204 | mBeamShots.render(); |
| 886 | chris | 205 | mPowerUpMissile.render(); |
| 956 | chris | 206 | mPowerUpBomb.render(); |
| 886 | chris | 207 | |
| 208 | // render enemies |
||
| 867 | chris | 209 | mBlobEnemies.render(); |
| 893 | chris | 210 | mCarrotEnemies.render(); |
| 878 | chris | 211 | |
| 886 | chris | 212 | // render ships |
| 838 | chris | 213 | mShip.render(); |
| 886 | chris | 214 | |
| 215 | // render effects |
||
| 866 | chris | 216 | mExplosions.render(); |
| 867 | chris | 217 | |
| 961 | chris | 218 | mImpactExplosions.render(); |
| 219 | |||
| 983 | chris | 220 | mLevel.renderForeground(); |
| 221 | |||
| 886 | chris | 222 | // render UI |
| 867 | chris | 223 | mVirtualPad.render(); |
| 837 | chris | 224 | } |
| 225 | |||
| 977 | chris | 226 | public boolean checkShipHit() |
| 227 | { |
||
| 228 | // TODO: maybe use more allocation-efficient data structures |
||
| 993 | chris | 229 | Vector<ICollidable> collidables = getCollidableList(); |
| 977 | chris | 230 | |
| 231 | AABoundingBox b = mShip.getBounds(); |
||
| 232 | |||
| 233 | boolean found = false; |
||
| 993 | chris | 234 | for (int i = 0; i < collidables.size(); i++) |
| 977 | chris | 235 | { |
| 993 | chris | 236 | ICollidable collidableObject = collidables.get(i); |
| 977 | chris | 237 | |
| 238 | // check if the two things are in closer proximity |
||
| 993 | chris | 239 | AABoundingBox a = collidableObject.getBounds(); |
| 977 | chris | 240 | |
| 997 | chris | 241 | if (a == null) |
| 242 | continue; |
||
| 243 | |||
| 977 | chris | 244 | if (!a.intersetcs(b)) |
| 245 | { |
||
| 246 | // even bounding boxes do not intersect, so no need to test further |
||
| 247 | continue; |
||
| 248 | } |
||
| 249 | |||
| 250 | // detail check |
||
| 998 | chris | 251 | if (collidableObject.checkShipHit(mShip)) |
| 977 | chris | 252 | { |
| 993 | chris | 253 | mShip.processHit(collidableObject); |
| 977 | chris | 254 | found = true; |
| 255 | } |
||
| 256 | } |
||
| 257 | |||
| 991 | chris | 258 | // check static level element collisions |
| 996 | chris | 259 | // get level's ICollidables |
| 260 | // hmm.. |
||
| 261 | |||
| 262 | // check static level element collisions |
||
| 993 | chris | 263 | /*if (mLevel.checkShipHit()) |
| 264 | found = true;*/ |
||
| 991 | chris | 265 | |
| 977 | chris | 266 | return found; |
| 267 | } |
||
| 268 | |||
| 932 | chris | 269 | public boolean checkProjectileHit(Projectile projectile) |
| 270 | { |
||
| 930 | chris | 271 | // TODO: maybe use more allocation-efficient data structures |
| 993 | chris | 272 | Vector<ICollidable> collidables = getCollidableList(); |
| 950 | chris | 273 | |
| 274 | AABoundingBox b = projectile.getBounds(); |
||
| 930 | chris | 275 | |
| 936 | chris | 276 | boolean found = false; |
| 993 | chris | 277 | for (int i = 0; i < collidables.size(); i++) |
| 930 | chris | 278 | { |
| 993 | chris | 279 | ICollidable collidableObject = collidables.get(i); |
| 950 | chris | 280 | |
| 281 | // check if the two things are in closer proximity |
||
| 993 | chris | 282 | AABoundingBox a = collidableObject.getBounds(); |
| 950 | chris | 283 | |
| 994 | chris | 284 | if (a == null) |
| 285 | continue; |
||
| 286 | |||
| 950 | chris | 287 | if (!a.intersetcs(b)) |
| 288 | { |
||
| 289 | // even bounding boxes do not intersect, so no need to test further |
||
| 290 | continue; |
||
| 291 | } |
||
| 953 | chris | 292 | |
| 950 | chris | 293 | // detail check |
| 993 | chris | 294 | if (collidableObject.checkHit(projectile.getShape(), projectile.getShapeTransform())) |
| 936 | chris | 295 | { |
| 993 | chris | 296 | projectile.processHit(collidableObject); |
| 944 | chris | 297 | found = true; |
| 936 | chris | 298 | } |
| 930 | chris | 299 | } |
| 300 | |||
| 991 | chris | 301 | // check static level element collisions |
| 993 | chris | 302 | /*if (mLevel.checkProjectileHit(projectile)) |
| 303 | found = true;*/ |
||
| 991 | chris | 304 | |
| 944 | chris | 305 | return found; |
| 936 | chris | 306 | } |
| 993 | chris | 307 | |
| 887 | chris | 308 | public boolean getNearestEnemyPosition(float x, float y, Vector2 result) |
| 309 | { |
||
| 993 | chris | 310 | ICollidable nearestEnemy = getNearestEnemy(x, y); |
| 893 | chris | 311 | |
| 990 | chris | 312 | if (nearestEnemy == null) |
| 313 | return false; |
||
| 314 | |||
| 315 | result.x = nearestEnemy.getX(); |
||
| 316 | result.y = nearestEnemy.getY(); |
||
| 317 | |||
| 318 | return true; |
||
| 887 | chris | 319 | } |
| 320 | |||
| 990 | chris | 321 | public Enemy getNearestEnemy(float x, float y) |
| 322 | { |
||
| 323 | Enemy foundEnemy = null; |
||
| 324 | |||
| 325 | float distanceSqr = Float.MAX_VALUE; |
||
| 326 | |||
| 327 | Vector<Enemy> enemies = getEnemyList(); |
||
| 328 | for (int i = 0; i < enemies.size(); i++) |
||
| 329 | { |
||
| 330 | Enemy enemy = enemies.get(i); |
||
| 331 | if (enemy.isAlive()) |
||
| 332 | { |
||
| 333 | float diffX = enemy.getX() - x; |
||
| 334 | float diffY = enemy.getX() - y; |
||
| 335 | |||
| 336 | float currentDistanceSqr = diffX*diffX + diffY*diffY; |
||
| 337 | if (currentDistanceSqr < distanceSqr) |
||
| 338 | { |
||
| 339 | foundEnemy = enemy; |
||
| 340 | distanceSqr = currentDistanceSqr; |
||
| 341 | } |
||
| 342 | } |
||
| 343 | } |
||
| 344 | |||
| 345 | return foundEnemy; |
||
| 346 | } |
||
| 347 | |||
| 993 | chris | 348 | public Vector<ICollidable> getCollidableList() |
| 349 | { |
||
| 350 | Vector<ICollidable> collidables = new Vector<ICollidable>(); |
||
| 351 | mCarrotEnemies.fillCollidableList(collidables); |
||
| 352 | mBlobEnemies.fillCollidableList(collidables); |
||
| 994 | chris | 353 | mLevel.fillCollidableList(collidables); |
| 993 | chris | 354 | return collidables; |
| 355 | } |
||
| 356 | |||
| 990 | chris | 357 | public Vector<Enemy> getEnemyList() |
| 358 | { |
||
| 359 | Vector<Enemy> enemies = new Vector<Enemy>(); |
||
| 360 | mCarrotEnemies.fillEnemyList(enemies); |
||
| 361 | mBlobEnemies.fillEnemyList(enemies); |
||
| 362 | return enemies; |
||
| 363 | } |
||
| 364 | |||
| 956 | chris | 365 | public boolean isOutOfScreen(SpriteParameters param) |
| 366 | { |
||
| 367 | return ((param.x > (GameConsts.VIRTUAL_SCREEN_WIDTH + param.w)) || |
||
| 368 | (param.x < (-param.w)) || |
||
| 369 | (param.y > (GameConsts.VIRTUAL_SCREEN_HEIGHT + param.h)) || |
||
| 370 | (param.y < (-param.h))); |
||
| 371 | } |
||
| 372 | |||
| 837 | chris | 373 | // Getters/Setters================================================================================== |
| 374 | |||
| 838 | chris | 375 | public final Ship getShip() { return mShip; } |
| 851 | chris | 376 | public final PlasmaShots getPlasmaShots() { return mPlasmaShots; } |
| 967 | chris | 377 | public final BeamShots getBeamShots() { return mBeamShots; } |
| 866 | chris | 378 | public final Explosions getExplosions() { return mExplosions; } |
| 867 | chris | 379 | public final VirtualPad getVirtualPad() { return mVirtualPad; } |
| 877 | chris | 380 | public final PowerUps getPowerUps() { return mPowerUps; } |
| 886 | chris | 381 | public final PowerUpMissile getPowerUpMissile() { return mPowerUpMissile; } |
| 956 | chris | 382 | public final PowerUpBomb getPowerUpBomb() { return mPowerUpBomb; } |
| 961 | chris | 383 | public final ImpactExplosions getImpactExplosions() { return mImpactExplosions; } |
| 879 | chris | 384 | public final ShipParameters getShipParameters() { return mShipParameters; } |
| 838 | chris | 385 | |
| 894 | chris | 386 | public final float getGlobalScrollX() { return mGlobalScrollX; } |
| 387 | public final float getGlobalScrollY() { return mGlobalScrollY; } |
||
| 886 | chris | 388 | |
| 837 | chris | 389 | } |
| 838 | chris | 390 | |
| 391 |