All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.leonis.subra.game.engine.BallsVelocityDeducer Maven / Gradle / Ivy

The newest version!
package io.leonis.subra.game.engine;

import io.leonis.subra.game.data.*;
import io.leonis.zosma.game.engine.Deducer;
import java.util.*;
import java.util.stream.Collectors;
import org.nd4j.linalg.ops.transforms.Transforms;
import org.reactivestreams.Publisher;
import reactor.core.publisher.Flux;

/**
 * The Class BallsVelocityDeducer.
 *
 * This class represents a {@link Deducer} which calculates velocities of {@link Ball}.
 *
 * @param  The type of state carrying a {@link Set} of {@link Ball}.
 * @author Rimon Oz
 */
public class BallsVelocityDeducer
    implements Deducer> {
  @Override
  public Publisher> apply(final Publisher iPublisher) {
    return Flux.from(iPublisher)
        .scan(Collections.emptySet(), (previousGame, currentGame) ->
            currentGame.getBalls().stream()
                .map(currentBall ->
                    previousGame.stream()
                        .reduce((closerBall, newBall) ->
                            Transforms.euclideanDistance(newBall.getXY(), currentBall.getXY())
                                > Transforms.euclideanDistance(
                                closerBall.getXY(), currentBall.getXY())
                                ? newBall
                                : closerBall)
                        .map(closestBall -> new MovingBall.State(currentBall, closestBall))
                        .orElse(
                            new MovingBall.State(
                                currentBall.getTimestamp(),
                                currentBall.getX(),
                                currentBall.getY(),
                                currentBall.getZ(),
                                0d,
                                0d,
                                0d)))
                .collect(Collectors.toSet()));
  }
}