|
| 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.Objects; |
| 40 | +import java.util.Random; |
| 41 | + |
| 42 | +import org.slf4j.*; |
| 43 | + |
| 44 | +import com.simsilica.es.*; |
| 45 | +import com.simsilica.mathd.*; |
| 46 | +import com.simsilica.sim.*; |
| 47 | + |
| 48 | +import sigem.es.*; |
| 49 | + |
| 50 | +/** |
| 51 | + * Checks for any missile hits to create the hit effect and destroy |
| 52 | + * the missile entity. It's up to other listeners to deal with the damage. |
| 53 | + * |
| 54 | + * @author Paul Speed |
| 55 | + */ |
| 56 | +public class MissileHitListener extends AbstractGameSystem |
| 57 | + implements ContactListener { |
| 58 | + |
| 59 | + static Logger log = LoggerFactory.getLogger(MissileHitListener.class); |
| 60 | + |
| 61 | + private EntityData ed; |
| 62 | + private GameEntities gameEntities; |
| 63 | + private Random rand = new Random(0); |
| 64 | + |
| 65 | + public MissileHitListener() { |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + protected void initialize() { |
| 70 | + this.ed = getSystem(EntityData.class, true); |
| 71 | + this.gameEntities = getSystem(GameEntities.class, true); |
| 72 | + |
| 73 | + getSystem(CollisionSystem.class).addContactListener(this); |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + protected void terminate() { |
| 78 | + getSystem(CollisionSystem.class).removeContactListener(this); |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public void newContact( Contact c ) { |
| 83 | + |
| 84 | + if( c.energy == 0 ) { |
| 85 | + // An early listener already said this wasn't a real contact |
| 86 | + return; |
| 87 | + } |
| 88 | + |
| 89 | + ObjectType type1 = ed.getComponent(c.b1.bodyId, ObjectType.class); |
| 90 | + ObjectType type2 = ed.getComponent(c.b2.bodyId, ObjectType.class); |
| 91 | + |
| 92 | + // Entities may have already been removed |
| 93 | + String t1 = type1 == null ? null : type1.getTypeName(ed); |
| 94 | + String t2 = type2 == null ? null : type2.getTypeName(ed); |
| 95 | + |
| 96 | + if( Objects.equals(t1, t2) ) { |
| 97 | + // Missiles or not, we don't care if they are the same... even |
| 98 | + // missiles can pass each other by. |
| 99 | + return; |
| 100 | + } |
| 101 | + |
| 102 | + if( ObjectType.TYPE_MISSILE.equals(t1) ) { |
| 103 | + // Blow up b2 |
| 104 | + missileHit(c.b1, c); |
| 105 | + } else if( ObjectType.TYPE_MISSILE.equals(t2) ) { |
| 106 | + // Blow up b1 |
| 107 | + missileHit(c.b2, c); |
| 108 | + } |
| 109 | + |
| 110 | + } |
| 111 | + |
| 112 | + protected void missileHit( Body missile, Contact contact ) { |
| 113 | + ed.removeEntity(missile.bodyId); |
| 114 | + gameEntities.createExplosion(missile.pos, 1); |
| 115 | + } |
| 116 | +} |
0 commit comments