Kyrremann

Untitled

Dec 18th, 2013
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.43 KB | None | 0 0
  1. package no.minimon.snakeinspace;
  2.  
  3. import java.util.ArrayList;
  4.  
  5. import javax.crypto.spec.PSource;
  6.  
  7. import org.omg.CORBA.Current;
  8.  
  9. import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
  10. import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
  11. import com.badlogic.gdx.math.Vector2;
  12. import com.sun.org.apache.regexp.internal.recompile;
  13.  
  14. public class Snake {
  15.  
  16.     public static final float SPEED = 100;
  17.     public static final int SIZE = 10;
  18.  
  19.     private Vector2 position;
  20.     private ArrayList<Tail> tails;
  21.  
  22.     public Snake(Vector2 position) {
  23.         this.position = position;
  24.         tails = new ArrayList<Snake.Tail>();
  25.         for (int i = 0; i < 10; i++) {
  26.             tails.add(new Tail(new Vector2(0, 0), 0));
  27.         }
  28.     }
  29.  
  30.     public void turnLeft() {
  31.         tails.get(0).angle += 7f;
  32.     }
  33.    
  34.     public void turnRight() {
  35.         tails.get(0).angle -= 7f;
  36.     }
  37.  
  38.     public void update(float delta, ShapeRenderer renderer) {
  39.         /*if (angle > 360) {
  40.             angle -= 360;
  41.         } else if (angle < 0) {
  42.             angle += 360;
  43.         }*/
  44.        
  45.         position.x += ((float) Math.cos(Math.toRadians(tails.get(0).angle))) * delta * SPEED;
  46.         position.y += ((float) Math.sin(Math.toRadians(tails.get(0).angle))) * delta * SPEED;
  47.        
  48.         calculateTailPiece(0, position);
  49.         for (int i = 0; i < tails.size() - 1; i++) {
  50.             calculateTailPiece(i + 1, tails.get(i).position);
  51.         }
  52.  
  53.         for (Tail t : tails) {
  54.             drawSnake(t, renderer);
  55.         }
  56.     }
  57.  
  58.     public void calculateTailPiece(int index, Vector2 in) {
  59.         Vector2 next = this.tails.get(index).position;
  60.         float nextAngle = this.tails.get(index).angle;
  61.        
  62.         float dx = in.x - next.x;
  63.         float dy = in.y - next.y;
  64.         nextAngle = (float) Math.atan2(dy, dx);
  65.         next.x = in.x - ((float) Math.cos(Math.toRadians(nextAngle)) * SIZE);
  66.         next.y = in.y - ((float) Math.sin(Math.toRadians(nextAngle)) * SIZE);
  67.     }
  68.    
  69.     private void drawSnake(Tail tail, ShapeRenderer renderer) {
  70.         renderer.begin(ShapeType.Line);
  71.         renderer.identity();
  72.         renderer.setColor(tail.position.x, tail.position.y, tail.angle, 255);
  73.         renderer.translate(tail.position.x, tail.position.y, 0);
  74.         renderer.rotate(0, 0, 1, tail.angle);
  75.         renderer.triangle(0, -5, 0, 5, 10, 0);
  76.         renderer.end();
  77.     }
  78.    
  79.     private class Tail {
  80.        
  81.         public float angle;
  82.         public Vector2 position;
  83.        
  84.         public Tail(Vector2 position, float angle) {
  85.             this.position = position;
  86.             this.angle = angle;
  87.         }
  88.        
  89.         @Override
  90.         public String toString() {
  91.             return String.format("(%f,%f) %f", position.x, position.y, angle);
  92.         }
  93.     }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment