Rev 976 | Details | Compare with Previous | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 959 | chris | 1 | package com.gebauz.burutaru.game.entities; |
| 2 | |||
| 3 | import java.util.Vector; |
||
| 4 | |||
| 976 | chris | 5 | import com.badlogic.gdx.audio.Sound; |
| 960 | chris | 6 | import com.gebauz.bauzoid.graphics.sprite.AtlasSprite; |
| 7 | import com.gebauz.bauzoid.graphics.sprite.AtlasSpriteFrame; |
||
| 8 | import com.gebauz.bauzoid.graphics.sprite.AtlasSpriteInstance; |
||
| 9 | import com.gebauz.bauzoid.graphics.sprite.SpriteInstance; |
||
| 961 | chris | 10 | import com.gebauz.bauzoid.math.MathUtil; |
| 959 | chris | 11 | import com.gebauz.bauzoid.math.Matrix4; |
| 12 | import com.gebauz.bauzoid.math.collision.AABoundingBox; |
||
| 13 | import com.gebauz.bauzoid.math.collision.Shape; |
||
| 960 | chris | 14 | import com.gebauz.bauzoid.math.collision.ShapeUtil; |
| 959 | chris | 15 | import com.gebauz.burutaru.game.GameLogic; |
| 16 | |||
| 17 | public class ImpactExplosions extends Entity |
||
| 18 | { |
||
| 19 | |||
| 20 | // Constants======================================================================================== |
||
| 21 | |||
| 965 | chris | 22 | public static final int EXPLO_DAMAGE = 5; |
| 961 | chris | 23 | public static final float EXPLO_SIZE = 40; |
| 24 | |||
| 25 | public static final float EXPLO_ANIM_DURATION = 0.5f; |
||
| 959 | chris | 26 | |
| 27 | // Embedded Types=================================================================================== |
||
| 28 | |||
| 29 | public class Instance extends Projectile |
||
| 30 | { |
||
| 960 | chris | 31 | public AtlasSpriteInstance spriteInstance = null; |
| 961 | chris | 32 | |
| 33 | public float lifeTime = 0.0f; |
||
| 34 | public float size = EXPLO_SIZE; |
||
| 959 | chris | 35 | |
| 961 | chris | 36 | public Instance(float x, float y, float size) |
| 959 | chris | 37 | { |
| 38 | super(EXPLO_DAMAGE); |
||
| 960 | chris | 39 | |
| 40 | spriteInstance = new AtlasSpriteInstance(getGraphics(), mExploSprite, mExploFrames, mExploShape); |
||
| 961 | chris | 41 | |
| 42 | spriteInstance.param.x = x; |
||
| 43 | spriteInstance.param.y = y; |
||
| 44 | spriteInstance.param.w = size; |
||
| 45 | spriteInstance.param.h = size; |
||
| 46 | spriteInstance.param.centerPivot(); |
||
| 47 | |||
| 48 | lifeTime = 0.0f; |
||
| 959 | chris | 49 | } |
| 50 | |||
| 51 | public void update(float deltaTime) |
||
| 52 | { |
||
| 961 | chris | 53 | spriteInstance.param.x += getGameLogic().getGlobalScrollX() * deltaTime; |
| 54 | spriteInstance.param.y += getGameLogic().getGlobalScrollY() * deltaTime; |
||
| 959 | chris | 55 | |
| 961 | chris | 56 | lifeTime += deltaTime; |
| 57 | |||
| 965 | chris | 58 | getGameLogic().checkProjectileHit(this); |
| 59 | |||
| 961 | chris | 60 | if (lifeTime > EXPLO_ANIM_DURATION) |
| 61 | { |
||
| 62 | setAlive(false); |
||
| 63 | } |
||
| 959 | chris | 64 | } |
| 65 | |||
| 66 | public void render() |
||
| 67 | { |
||
| 961 | chris | 68 | float t = (lifeTime % EXPLO_ANIM_DURATION) / EXPLO_ANIM_DURATION; |
| 69 | int frame = MathUtil.clamp((int)(t * (float)mExploFrames.length), 0, mExploFrames.length-1); |
||
| 959 | chris | 70 | |
| 961 | chris | 71 | spriteInstance.renderFrame(frame); |
| 959 | chris | 72 | } |
| 73 | |||
| 74 | @Override |
||
| 993 | chris | 75 | public void processHit(ICollidable hitObject) |
| 959 | chris | 76 | { |
| 962 | chris | 77 | // spawn impact effect |
| 993 | chris | 78 | hitObject.hit(getDamage()); |
| 959 | chris | 79 | } |
| 80 | |||
| 81 | @Override |
||
| 82 | public AABoundingBox getBounds() |
||
| 83 | { |
||
| 960 | chris | 84 | return spriteInstance.param.getBoundingBox(); |
| 959 | chris | 85 | } |
| 86 | |||
| 87 | @Override |
||
| 88 | public Shape getShape() |
||
| 89 | { |
||
| 960 | chris | 90 | return spriteInstance.getShape(); |
| 959 | chris | 91 | } |
| 92 | |||
| 93 | @Override |
||
| 94 | public Matrix4 getShapeTransform() |
||
| 95 | { |
||
| 960 | chris | 96 | return spriteInstance.param.getTransform(); |
| 959 | chris | 97 | } |
| 98 | |||
| 99 | } |
||
| 100 | |||
| 101 | // Fields=========================================================================================== |
||
| 102 | |||
| 960 | chris | 103 | private AtlasSprite mExploSprite = null; |
| 104 | private AtlasSpriteFrame mExploFrames[] = null; |
||
| 105 | private Shape mExploShape = null; |
||
| 106 | |||
| 959 | chris | 107 | private Vector<Instance> mExplosions = new Vector<Instance>(); |
| 976 | chris | 108 | |
| 109 | private Sound mImpactSound = null; |
||
| 959 | chris | 110 | |
| 111 | // Methods========================================================================================== |
||
| 112 | |||
| 113 | public ImpactExplosions(GameLogic gameLogic) |
||
| 114 | { |
||
| 115 | super(gameLogic); |
||
| 960 | chris | 116 | mExploSprite = new AtlasSprite(getGraphics(), "data/textures/explosion.png", "data/textures/explosion.txt"); |
| 959 | chris | 117 | } |
| 118 | |||
| 119 | @Override |
||
| 120 | public void initAsync() |
||
| 121 | { |
||
| 960 | chris | 122 | mExploSprite.initAsync(); |
| 959 | chris | 123 | } |
| 124 | |||
| 125 | @Override |
||
| 126 | public void init() |
||
| 127 | { |
||
| 960 | chris | 128 | mExploSprite.init(); |
| 129 | mExploFrames = mExploSprite.createSpriteInstances(); |
||
| 130 | mExploShape = ShapeUtil.createShapeFromFile("data/textures/explosion.shape"); |
||
| 976 | chris | 131 | |
| 132 | mImpactSound = getAudio().newManagedSound("data/sounds/impact_explo.wav"); |
||
| 959 | chris | 133 | } |
| 134 | |||
| 135 | @Override |
||
| 136 | public void exit() |
||
| 137 | { |
||
| 960 | chris | 138 | if (mExploSprite != null) |
| 139 | { |
||
| 140 | mExploSprite.dispose(); |
||
| 141 | mExploSprite = null; |
||
| 142 | } |
||
| 976 | chris | 143 | |
| 144 | if (mImpactSound != null) |
||
| 145 | { |
||
| 146 | getAudio().removeManagedSound(mImpactSound); |
||
| 147 | mImpactSound = null; |
||
| 148 | } |
||
| 959 | chris | 149 | } |
| 150 | |||
| 151 | @Override |
||
| 152 | public void update(float deltaTime) |
||
| 153 | { |
||
| 154 | int i = 0; |
||
| 155 | while (i < mExplosions.size()) |
||
| 156 | { |
||
| 157 | mExplosions.get(i).update(deltaTime); |
||
| 158 | if (!mExplosions.get(i).isAlive()) |
||
| 159 | { |
||
| 160 | mExplosions.remove(i); |
||
| 161 | continue; |
||
| 162 | } |
||
| 163 | ++i; |
||
| 164 | } |
||
| 165 | } |
||
| 166 | |||
| 167 | @Override |
||
| 168 | public void render() |
||
| 169 | { |
||
| 170 | for (int i = 0; i < mExplosions.size(); i++) |
||
| 171 | { |
||
| 172 | mExplosions.get(i).render(); |
||
| 173 | } |
||
| 174 | } |
||
| 175 | |||
| 961 | chris | 176 | public void spawn(float x, float y, float size) |
| 959 | chris | 177 | { |
| 961 | chris | 178 | mExplosions.add(new Instance(x, y, size)); |
| 976 | chris | 179 | mImpactSound.play(); |
| 959 | chris | 180 | } |
| 181 | |||
| 182 | // Getters/Setters================================================================================== |
||
| 183 | |||
| 184 | } |
||
| 185 | |||
| 186 |