|
| 1 | +/* |
| 2 | + * $Id$ |
| 3 | + * |
| 4 | + * Copyright (c) 2019, Simsilica, LLC |
| 5 | + * All rights reserved. |
| 6 | + * |
| 7 | + * Redistribution and use in source and binary forms, with or without |
| 8 | + * modification, are permitted provided that the following conditions |
| 9 | + * are met: |
| 10 | + * |
| 11 | + * 1. Redistributions of source code must retain the above copyright |
| 12 | + * notice, this list of conditions and the following disclaimer. |
| 13 | + * |
| 14 | + * 2. Redistributions in binary form must reproduce the above copyright |
| 15 | + * notice, this list of conditions and the following disclaimer in |
| 16 | + * the documentation and/or other materials provided with the |
| 17 | + * distribution. |
| 18 | + * |
| 19 | + * 3. Neither the name of the copyright holder nor the names of its |
| 20 | + * contributors may be used to endorse or promote products derived |
| 21 | + * from this software without specific prior written permission. |
| 22 | + * |
| 23 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 24 | + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 25 | + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 26 | + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 27 | + * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, |
| 28 | + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 29 | + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 30 | + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 31 | + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 32 | + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 33 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
| 34 | + * OF THE POSSIBILITY OF SUCH DAMAGE. |
| 35 | + */ |
| 36 | + |
| 37 | +package sigem.sim; |
| 38 | + |
| 39 | +import java.util.Random; |
| 40 | + |
| 41 | +import org.slf4j.*; |
| 42 | + |
| 43 | +import com.simsilica.es.*; |
| 44 | +import com.simsilica.mathd.*; |
| 45 | +import com.simsilica.sim.*; |
| 46 | + |
| 47 | +import sigem.es.*; |
| 48 | + |
| 49 | +/** |
| 50 | + * Checks for missile-asteroid hits or high energy asteroid |
| 51 | + * hits and implements asteroid damage. |
| 52 | + * |
| 53 | + * @author Paul Speed |
| 54 | + */ |
| 55 | +public class AsteroidHitListener extends AbstractGameSystem |
| 56 | + implements ContactListener { |
| 57 | + |
| 58 | + static Logger log = LoggerFactory.getLogger(AsteroidHitListener.class); |
| 59 | + |
| 60 | + private EntityData ed; |
| 61 | + private GameEntities gameEntities; |
| 62 | + private Random rand = new Random(0); |
| 63 | + |
| 64 | + public AsteroidHitListener() { |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + protected void initialize() { |
| 69 | + this.ed = getSystem(EntityData.class, true); |
| 70 | + this.gameEntities = getSystem(GameEntities.class, true); |
| 71 | + |
| 72 | + getSystem(CollisionSystem.class).addContactListener(this); |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + protected void terminate() { |
| 77 | + getSystem(CollisionSystem.class).removeContactListener(this); |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public void newContact( Contact c ) { |
| 82 | + ObjectType type1 = ed.getComponent(c.b1.bodyId, ObjectType.class); |
| 83 | + ObjectType type2 = ed.getComponent(c.b2.bodyId, ObjectType.class); |
| 84 | + |
| 85 | + String t1 = type1.getTypeName(ed); |
| 86 | + String t2 = type2.getTypeName(ed); |
| 87 | + |
| 88 | + if( !ObjectType.TYPE_ASTEROID.equals(t1) && !ObjectType.TYPE_ASTEROID.equals(t2) ) { |
| 89 | + // Neither side is an asteroid... nothing to do |
| 90 | + return; |
| 91 | + } |
| 92 | + |
| 93 | + // If either side is a chunk then we'll negate the energy |
| 94 | + if( ObjectType.TYPE_ASTEROID_CHUNK.equals(t1) || ObjectType.TYPE_ASTEROID_CHUNK.equals(t2) ) { |
| 95 | + c.energy = 0; |
| 96 | + return; |
| 97 | + } |
| 98 | + |
| 99 | + log.info("contact:" + c + " types:" + t1 + ", " + t2); |
| 100 | + |
| 101 | + if( ObjectType.TYPE_MISSILE.equals(t1) ) { |
| 102 | + // Blow up b2 |
| 103 | + missileHit(c.b1, c.b2, c); |
| 104 | + } else if( ObjectType.TYPE_MISSILE.equals(t2) ) { |
| 105 | + // Blow up b1 |
| 106 | + missileHit(c.b2, c.b1, c); |
| 107 | + } |
| 108 | + |
| 109 | + } |
| 110 | + |
| 111 | + protected void missileHit( Body missile, Body asteroid, Contact contact ) { |
| 112 | + EntityId shooter = ed.getComponent(missile.bodyId, CreatedBy.class).getCreatorId(); |
| 113 | + log.info("Shooter:" + shooter + " name:" + ed.getComponent(shooter, Name.class)); |
| 114 | + |
| 115 | + ed.removeEntity(missile.bodyId); |
| 116 | + gameEntities.createExplosion(missile.pos, 1); |
| 117 | + |
| 118 | + // Create some asteroid debris |
| 119 | + int count = rand.nextInt(3) + 3; |
| 120 | + for( int i = 0; i < count; i++ ) { |
| 121 | + double x = rand.nextDouble() * 2 - 1; |
| 122 | + double z = rand.nextDouble() * 2 - 1; |
| 123 | + Vec3d dir = new Vec3d(x, 0, z); |
| 124 | + double size = 0.2 + rand.nextDouble() * 0.4; |
| 125 | + |
| 126 | + EntityId chunk = gameEntities.createAsteroidChunk(missile.pos, dir, |
| 127 | + new Vec3d(rand.nextDouble() + 1, rand.nextDouble(), 0), |
| 128 | + size); |
| 129 | + } |
| 130 | + |
| 131 | + if( asteroid.radius <= 1 ) { |
| 132 | + // Just gone |
| 133 | + ed.removeEntity(asteroid.bodyId); |
| 134 | + return; |
| 135 | + } |
| 136 | + |
| 137 | + // Else we need to break it up |
| 138 | + |
| 139 | + } |
| 140 | +} |
0 commit comments