private void handleCollision(EntityManager em, CollisionEvent ce) { if (em.hasComponent(ce.obstacle, Obstacle.class)) world.getEventManager().sendEvent(ce); if (em.hasComponent(ce.obstacle, Trigger.class) && em.hasComponent(ce.collider, Hero.class)) world.getEventManager().sendEvent(new TriggerEvent(ce.collider, ce.obstacle)); if (em.hasComponent(ce.obstacle, Health.class) && em.hasComponent(ce.collider, Damage.class)) world.getEventManager().sendEvent(new DamageEvent(ce.collider, ce.obstacle)); }
@Override public void run() { EntityManager em = world.getEntityManager(); for (CoreEntity e1 : em.getEntity(Collider.class)) { Point circle1pos = em.getComponent(e1, Position.class).position; CollisionCircle circle1 = em.getComponent(e1, CollisionCircle.class); for (CoreEntity e2 : em.getEntityAll(CollisionCircle.class)) { if (e1 == e2) continue; CollisionCircle circle2 = em.getComponent(e2, CollisionCircle.class); Point col = circle2.getClosest(circle1pos); boolean inside = circle2.isInside(circle1pos); if (circle1pos.dist(col) < circle1.getRadius() || inside) handleCollision(em, new CollisionEvent(e1, e2, col, inside)); } for (CoreEntity e2 : em.getEntityAll(CollisionPolygon.class)) { CollisionPolygon poly = em.getComponent(e2, CollisionPolygon.class); Point col = poly.getClosest(circle1pos); boolean inside = poly.isInside(circle1pos); if (circle1pos.dist(col) < circle1.getRadius() || inside) handleCollision(em, new CollisionEvent(e1, e2, col, inside)); } } }
@Override public void run() { EntityManager em = world.getEntityManager(); Point mouse = null; while (Mouse.next()) mouse = new Point(Mouse.getEventX(), Mouse.getEventY()); if (mouse == null) return; for (CoreEntity e : em.getEntity(Button.class)) { CollisionPolygon poly = em.getComponent(e, CollisionPolygon.class); Button button = em.getComponent(e, Button.class); if (poly.isInside(mouse)) { button.active = true; if (!Mouse.getEventButtonState()) continue; if (button.type == "Menu") { world.setState(StateEnum.GAME_MENU); } else if (button.type == "Exit") { world.setState(StateEnum.EXIT); } else if (button.type == "Play") { world.setStateAndClear(StateEnum.LEVEL1); Level1State.init(world); } else if (button.type == "Resume") { world.setState(StateEnum.LEVEL1); } else if (button.type == "Restart") { world.setStateAndClear(StateEnum.LEVEL1); Level1State.init(world); } else if (button.type == "Light") { world.getSystemManager().toggleSystem(LightSystem.class); } else if (button.type == "Pause") { world.getSystemManager().toggleSystem(PhysicsSystem.class); world.getSystemManager().toggleSystem(PlayerInputSystem.class); world.getSystemManager().toggleSystem(TimerSystem.class); world.getSystemManager().toggleSystem(EmitterSystem.class); } else System.out.println("Button " + button.type + " not recognised"); } else button.active = false; } }