/*
 * Copyright 2018 MovingBlocks
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.destinationsol.game.screens;

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Fixture;
import com.badlogic.gdx.physics.box2d.RayCastCallback;
import org.destinationsol.common.SolMath;
import org.destinationsol.game.Hero;
import org.destinationsol.game.SolGame;
import org.destinationsol.game.SolObject;

public class CollisionWarnDrawer extends WarnDrawer {
    private final CollisionRayCastCallback warnCallback = new CollisionRayCastCallback();
    private Hero hero;

    CollisionWarnDrawer() {
        super("Object Near");
    }

    public boolean shouldWarn(SolGame game) {
        hero = game.getHero();
        if (hero.isTranscendent()) {
            return false;
        }
        Vector2 position = hero.getPosition();
        Vector2 speed = hero.getSpeed();
        float acc = hero.getAcceleration();
        float speedLen = speed.len();
        float speedAngle = SolMath.angle(speed);
        if (acc <= 0 || speedLen < 2 * acc) {
            return false;
        }
        // t = v/a;
        // s = att/2 = vv/a/2;
        float breakWay = speedLen * speedLen / acc / 2;
        breakWay += 2 * speedLen;
        Vector2 finalPos = SolMath.getVec(0, 0);
        SolMath.fromAl(finalPos, speedAngle, breakWay);
        finalPos.add(position);
        warnCallback.show = false;
        game.getObjectManager().getWorld().rayCast(warnCallback, position, finalPos);
        SolMath.free(finalPos);
        return warnCallback.show;
    }

    private class CollisionRayCastCallback implements RayCastCallback {
        private boolean show;

        //TODO code from era when hero was SolShip - does this still work? (what is it supposed to do?)
        @Override
        public float reportRayFixture(Fixture fixture, Vector2 point, Vector2 normal, float fraction) {
            SolObject o = (SolObject) fixture.getBody().getUserData();
            if (hero == o) {
                return -1;
            }
            show = true;
            return 0;
        }
    }
}

@@ -1380,21 +1380,24 @@
 Vector2 
-speed
+velocity
  = hero.
@@ -1475,19 +1475,19 @@
 peed
-Len = speed
+ = velocity
 .len
@@ -1531,21 +1531,24 @@
 h.angle(
-speed
+velocity
 );%0A     
@@ -1571,19 +1571,16 @@
 %7C%7C speed
-Len
  %3C 2 * a
@@ -1706,19 +1706,16 @@
 peed
-Len
  * speed
 Len 
@@ -1710,19 +1710,16 @@
  * speed
-Len
  / acc /
@@ -1755,11 +1755,8 @@
 peed
-Len
 ;%0A  

Refactor velocity in game.screens pkg