/*
 * Copyright 2014 Matthew Collins
 *
 * 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 uk.co.thinkofdeath.thinkcraft.shared.vector;

public class Vector3 {

    float[] values = new float[3];

    public Vector3() {

    }

    public void apply(Matrix4 matrix) {
        float x = values[0];
        float y = values[1];
        float z = values[2];
        float w = matrix.get(3) * x + matrix.get(7) * y + matrix.get(11) * z + matrix.get(15);
        w = w == 0 ? 1 : w;

        values[0] = (matrix.get(0) * x + matrix.get(4) * y + matrix.get(8) * z + matrix.get(12)) / w;
        values[1] = (matrix.get(1) * x + matrix.get(5) * y + matrix.get(9) * z + matrix.get(13)) / w;
        values[2] = (matrix.get(2) * x + matrix.get(6) * y + matrix.get(10) * z + matrix.get(14)) / w;
    }

    public void set(float x, float y, float z) {
        values[0] = x;
        values[1] = y;
        values[2] = z;
    }

    public float getX() {
        return values[0];
    }

    public float getY() {
        return values[1];
    }

    public float getZ() {
        return values[2];
    }
}

@@ -651,65 +651,232 @@
 r;%0A%0A
-public class Vector3 %7B%0A%0A    float%5B%5D values = new float%5B3%5D
+import uk.co.thinkofdeath.thinkcraft.shared.platform.Platform;%0Aimport uk.co.thinkofdeath.thinkcraft.shared.platform.buffers.FloatBuffer;%0A%0Apublic class Vector3 %7B%0A%0A    private FloatBuffer values = Platform.createFloatBuffer(3)
 ;%0A%0A 
@@ -965,27 +965,31 @@
 t x = values
-%5B0%5D
+.get(0)
 ;%0A        fl
@@ -998,27 +998,31 @@
 t y = values
-%5B1%5D
+.get(1)
 ;%0A        fl
@@ -1031,27 +1031,31 @@
 t z = values
-%5B2%5D
+.get(2)
 ;%0A        fl
@@ -1182,21 +1182,23 @@
   values
-%5B0%5D =
+.set(0,
  (matrix
@@ -1266,24 +1266,25 @@
 get(12)) / w
+)
 ;%0A        va
@@ -1287,21 +1287,23 @@
   values
-%5B1%5D =
+.set(1,
  (matrix
@@ -1371,24 +1371,25 @@
 get(13)) / w
+)
 ;%0A        va
@@ -1392,21 +1392,23 @@
   values
-%5B2%5D =
+.set(2,
  (matrix
@@ -1481,16 +1481,17 @@
 14)) / w
+)
 ;%0A    %7D%0A
@@ -1558,15 +1558,18 @@
 lues
-%5B0%5D =
+.set(0,
  x
+)
 ;%0A  
@@ -1584,15 +1584,18 @@
 lues
-%5B1%5D =
+.set(1,
  y
+)
 ;%0A  
@@ -1610,15 +1610,18 @@
 lues
-%5B2%5D =
+.set(2,
  z
+)
 ;%0A  
@@ -1676,11 +1676,15 @@
 lues
-%5B0%5D
+.get(0)
 ;%0A  
@@ -1739,11 +1739,15 @@
 lues
-%5B1%5D
+.get(1)
 ;%0A  
@@ -1802,11 +1802,15 @@
 lues
-%5B2%5D
+.get(2)
 ;%0A  

Modify Vector3 to use FloatBuffers