Beispiel #1
0
  @Override
  public void start() {
    super.start();

    environment =
        new Continuous2D(1.0, config.getEnvironmentWidth(), config.getEnvironmentHeight());
    drawProxy = new DrawProxy(environment.getWidth(), environment.getHeight());
    environment.setObjectLocation(drawProxy, new Double2D());

    physicsWorld = new World(new Vec2());
    placementArea =
        new PlacementArea((float) environment.getWidth(), (float) environment.getHeight());
    placementArea.setSeed(config.getSimulationSeed());
    schedule.reset();
    System.gc();

    physicsWorld.setContactListener(contactListener);

    // Create ALL the objects
    createWalls();
    createTargetArea();
    robotFactory.placeInstances(
        placementArea.new ForType<>(), physicsWorld, config.getTargetAreaPlacement());
    config.getResourceFactory().placeInstances(placementArea.new ForType<>(), physicsWorld);

    // Now actually add the objects that have been placed to the world and schedule
    for (PhysicalObject object : placementArea.getPlacedObjects()) {
      drawProxy.registerDrawable(object.getPortrayal());
      schedule.scheduleRepeating(object);
    }

    schedule.scheduleRepeating(
        simState -> physicsWorld.step(TIME_STEP, VELOCITY_ITERATIONS, POSITION_ITERATIONS));
  }
  public void init(GameContainer container) throws SlickException {
    if (container instanceof AppGameContainer) {
      app = (AppGameContainer) container;
    }

    over = false;

    input = container.getInput();

    Vec2 gravity = new Vec2(0.0f, 0.0f);
    world = new World(gravity, true);

    player1 = new ShipBuild(player1Ship, world, tr);
    player2 = new ShipCPU(player2Ship, world, tr, level); // thats the time in ms for shooting

    float jX = tr.width / tr.xscale;
    float jY = tr.height / tr.yscale;

    float max = jY / 2 - 15;
    float min = -jY / 2 + 15;

    asteroids = new AsteroidSpawner(world, tr);
    asteroids.genAsteroids(jX / 2 - 40, max - 25);

    float p1Y = (float) (Math.random() * (max - min) + min);
    float p2Y = (float) (Math.random() * (max - min) + min);
    player1.player.setTransform(new Vec2(-jX / 2 + 15, p1Y), 3 * (float) Math.PI / 2);
    player2.player.setTransform(new Vec2(jX / 2 - 15, p2Y), (float) Math.PI / 2);

    player1.inputForward = Input.KEY_W; // E;
    player1.inputLeft = Input.KEY_A; // W;
    player1.inputRight = Input.KEY_D; // R;
    player1.inputShoot = Input.KEY_SPACE; // A;
    player1.inputBackward = Input.KEY_S;

    cDetect = new CollideTest(player1, player2, world);

    world.setContactListener(cDetect);
  }
Beispiel #3
0
  private void addWorldContactListener(World world) {
    world.setContactListener(
        new ContactListener() {
          public void result(ContactResult point) {
            //				System.out.println("Result contact");
          }
          // @Override
          public void remove(ContactPoint point) {
            //				System.out.println("remove contact");
          }
          // @Override
          public void persist(ContactPoint point) {
            //				System.out.println("persist contact");
          }
          // @Override
          public void add(ContactPoint point) {
            //				/*
            Shape shape1 = point.shape1;
            Shape shape2 = point.shape2;
            final Body body1 = shape1.getBody();
            final Body body2 = shape2.getBody();
            Object userData1 = body1.getUserData();
            Object userData2 = body2.getUserData();

            if (userData1 instanceof IPhysicsComponent
                && userData2 instanceof IPhysicsComponent) { // Check for ball/star collision
              IPhysicsComponent physObj1 = (IPhysicsComponent) userData1;
              IPhysicsComponent physObj2 = (IPhysicsComponent) userData2;
              //					System.out.println("Collided: " + mt4jObj1 + " with " + mt4jObj2);
              if (physObj1 instanceof MTComponent && physObj2 instanceof MTComponent) {
                MTComponent comp1 = (MTComponent) physObj1;
                MTComponent comp2 = (MTComponent) physObj2;

                // Check if one of the components is the BALL
                MTComponent ball = isHit("ball", comp1, comp2);
                final MTComponent theBall = ball;

                // Check if one of the components is the GOAL
                MTComponent goal1 = isHit("goal1", comp1, comp2);
                MTComponent goal2 = isHit("goal2", comp1, comp2);

                // Check if a puck was involved
                MTComponent bluePuck = isHit("blue", comp1, comp2);
                MTComponent redPuck = isHit("red", comp1, comp2);

                // Check if a border was hit
                MTComponent border = null;
                if (comp1.getName() != null && comp1.getName().startsWith("border")) {
                  border = comp1;
                } else if (comp2.getName() != null && comp2.getName().startsWith("border")) {
                  border = comp2;
                }

                if (ball != null) {
                  // CHECK IF BALL HIT A PADDLE
                  if (enableSound && (bluePuck != null || redPuck != null)) {
                    //								System.out.println("PUCK HIT BALL!");
                    /*
                    triggerSound(paddleHit);
                    */
                  }

                  // Check if BALL HIT A GOAL
                  if (goal1 != null || goal2 != null) {
                    // BALL HIT A GOAL
                    if (goal1 != null) {
                      System.out.println("GOAL FOR PLAYER 2!");
                      scorePlayer2++;
                    } else if (goal2 != null) {
                      System.out.println("GOAL FOR PLAYER 1!");
                      scorePlayer1++;
                    }

                    // Update scores
                    updateScores();
                    // Play goal sound
                    //								triggerSound(goalHit);

                    if (scorePlayer1 >= 15 || scorePlayer2 >= 15) {
                      reset();
                    } else {

                      // Reset ball
                      if (theBall.getUserData("resetted")
                          == null) { // To make sure that we call destroy only once
                        theBall.setUserData("resetted", true);
                        app.invokeLater(
                            new Runnable() {
                              public void run() {
                                IPhysicsComponent a = (IPhysicsComponent) theBall;
                                a.getBody()
                                    .setXForm(
                                        new Vec2(
                                            getMTApplication().width / 2f / scale,
                                            getMTApplication().height / 2f / scale),
                                        a.getBody().getAngle());
                                //											a.getBody().setLinearVelocity(new Vec2(0,0));
                                a.getBody()
                                    .setLinearVelocity(
                                        new Vec2(
                                            ToolsMath.getRandom(-8, 8),
                                            ToolsMath.getRandom(-8, 8)));
                                a.getBody().setAngularVelocity(0);
                                theBall.setUserData("resetted", null);
                              }
                            });
                      }
                    }
                  }

                  // If ball hit border Play sound
                  if (enableSound && border != null) {
                    /*
                    triggerSound(wallHit);
                    */
                  }
                }
              }
            } else { // if at lest one if the colliding bodies' userdata is not a physics shape

            }
            //				*/
          }
        });
  }