OSDN Git Service

an unfinished bit set implementation
authorMario Zechner <contact@badlogicgames.com>
Wed, 27 Feb 2013 23:40:22 +0000 (00:40 +0100)
committerMario Zechner <contact@badlogicgames.com>
Wed, 27 Feb 2013 23:40:22 +0000 (00:40 +0100)
gdx/src/com/badlogic/gdx/utils/Bits.java [new file with mode: 0755]
tests/gdx-tests/src/com/badlogic/gdx/tests/superkoalio/SuperKoalio.java

diff --git a/gdx/src/com/badlogic/gdx/utils/Bits.java b/gdx/src/com/badlogic/gdx/utils/Bits.java
new file mode 100755 (executable)
index 0000000..a15bfc4
--- /dev/null
@@ -0,0 +1,75 @@
+\r
+package com.badlogic.gdx.utils;\r
+\r
+import java.util.Arrays;\r
+\r
+/** A bitset, without size limitation, allows comparison via bitwise operators to other bitfields.\r
+ * @author mzechner */\r
+public class Bits {\r
+       long[] bits = { 0 };\r
+\r
+       /**\r
+        * @param index the index of the bit\r
+        * @return whether the bit is set\r
+        * @throws ArrayIndexOutOfBoundsException if index < 0\r
+        */\r
+       public boolean get (int index) {\r
+               final int word = index >>> 6;\r
+               if(word >= bits.length) return false;\r
+               return (bits[word] & (1L << (index & 0x3F))) != 0L;\r
+       }\r
+\r
+       /**\r
+        * @param index the index of the bit to set\r
+        * @throws ArrayIndexOutOfBoundsException if index < 0\r
+        */\r
+       public void set (int index) {\r
+               final int word = index >>> 6;\r
+               checkCapacity(word);\r
+               bits[word] |= 1L << (index & 0x3F);\r
+       }\r
+       \r
+       /**\r
+        * @param index the index of the bit to flip\r
+        */\r
+       public void flip(int index) {\r
+               final int word = index >>> 6;\r
+               checkCapacity(word);\r
+               bits[word] ^= 1L << (index & 0x3F);\r
+       }\r
+       \r
+       private void checkCapacity(int len) {\r
+               if(len> bits.length) {\r
+                       long[] newBits = new long[len+1];\r
+                       System.arraycopy(bits, 0, newBits, 0, bits.length);\r
+                       bits = newBits;\r
+               }\r
+       }\r
+\r
+       /**\r
+        * @param index the index of the bit to clear\r
+        * @throws ArrayIndexOutOfBoundsException if index < 0\r
+        */\r
+       public void clear (int index) {\r
+               final int word = index >>> 6;\r
+               if(word >= bits.length) return;\r
+               bits[word] &= ~(1L << (index & 0x3F));\r
+       }\r
+\r
+       /**\r
+        * Clears the entire bitset\r
+        */\r
+       public void clear () {\r
+               int length = bits.length;\r
+               for (int i = 0; i < length; i++) {\r
+                       bits[i] = 0L;\r
+               }\r
+       }\r
+       \r
+       /**\r
+        * @return the number of bits currently stored, <b>not</b> the highset set bit!\r
+        */\r
+       public int numBits() {\r
+               return bits.length << 6;\r
+       }\r
+}\r
index e67cb72..be5ee00 100755 (executable)
@@ -132,22 +132,22 @@ public class SuperKoalio extends GdxTest {
        \r
        private Vector2 tmp = new Vector2();\r
        private void updateKoala(float deltaTime) {\r
-               koala.stateTime += deltaTime;\r
+               koala.stateTime += deltaTime;   \r
                \r
                // check input and apply to velocity & state\r
-               if(Gdx.input.isKeyPressed(Keys.SPACE) & koala.grounded) {\r
+               if((Gdx.input.isKeyPressed(Keys.SPACE) || isTouched(0.75f, 1)) && koala.grounded) {\r
                        koala.velocity.y += Koala.JUMP_VELOCITY;\r
                        koala.state = Koala.State.Jumping;\r
                        koala.grounded = false;\r
                }\r
                \r
-               if(Gdx.input.isKeyPressed(Keys.LEFT) || Gdx.input.isKeyPressed(Keys.A)) {\r
+               if(Gdx.input.isKeyPressed(Keys.LEFT) || Gdx.input.isKeyPressed(Keys.A) || isTouched(0, 0.25f)) {\r
                        koala.velocity.x = -Koala.MAX_VELOCITY;\r
                        if(koala.grounded) koala.state = Koala.State.Walking;\r
                        koala.facesRight = false;\r
                }\r
                \r
-               if(Gdx.input.isKeyPressed(Keys.RIGHT) || Gdx.input.isKeyPressed(Keys.D)) {\r
+               if(Gdx.input.isKeyPressed(Keys.RIGHT) || Gdx.input.isKeyPressed(Keys.D) || isTouched(0.25f, 0.5f)) {\r
                        koala.velocity.x = Koala.MAX_VELOCITY;\r
                        if(koala.grounded) koala.state = Koala.State.Walking;\r
                        koala.facesRight = true;\r
@@ -236,6 +236,18 @@ public class SuperKoalio extends GdxTest {
                koala.velocity.x *= Koala.DAMPING;\r
                \r
        }\r
+\r
+       private boolean isTouched(float startX, float endX) {\r
+               // check if any finge is touch the area between startX and endX\r
+               // startX/endX are given between 0 (left edge of the screen) and 1 (right edge of the screen)\r
+               for(int i = 0; i < 2; i++) {\r
+                       float x = Gdx.input.getX() / (float)Gdx.graphics.getWidth();\r
+                       if(Gdx.input.isTouched(i) && (x >= startX && x <= endX)) {\r
+                               return true;\r
+                       }\r
+               }\r
+               return false;\r
+       }\r
        \r
        private void getTiles(int startX, int startY, int endX, int endY, Array<Rectangle> tiles) {\r
                TiledMapTileLayer layer = (TiledMapTileLayer)map.getLayers().getLayer(1);\r