Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package no.minimon.snakeinspace;
- import java.util.ArrayList;
- import javax.crypto.spec.PSource;
- import org.omg.CORBA.Current;
- import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
- import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
- import com.badlogic.gdx.math.Vector2;
- import com.sun.org.apache.regexp.internal.recompile;
- public class Snake {
- public static final float SPEED = 100;
- public static final int SIZE = 10;
- private Vector2 position;
- private ArrayList<Tail> tails;
- public Snake(Vector2 position) {
- this.position = position;
- tails = new ArrayList<Snake.Tail>();
- for (int i = 0; i < 10; i++) {
- tails.add(new Tail(new Vector2(0, 0), 0));
- }
- }
- public void turnLeft() {
- tails.get(0).angle += 7f;
- }
- public void turnRight() {
- tails.get(0).angle -= 7f;
- }
- public void update(float delta, ShapeRenderer renderer) {
- /*if (angle > 360) {
- angle -= 360;
- } else if (angle < 0) {
- angle += 360;
- }*/
- position.x += ((float) Math.cos(Math.toRadians(tails.get(0).angle))) * delta * SPEED;
- position.y += ((float) Math.sin(Math.toRadians(tails.get(0).angle))) * delta * SPEED;
- calculateTailPiece(0, position);
- for (int i = 0; i < tails.size() - 1; i++) {
- calculateTailPiece(i + 1, tails.get(i).position);
- }
- for (Tail t : tails) {
- drawSnake(t, renderer);
- }
- }
- public void calculateTailPiece(int index, Vector2 in) {
- Vector2 next = this.tails.get(index).position;
- float nextAngle = this.tails.get(index).angle;
- float dx = in.x - next.x;
- float dy = in.y - next.y;
- nextAngle = (float) Math.atan2(dy, dx);
- next.x = in.x - ((float) Math.cos(Math.toRadians(nextAngle)) * SIZE);
- next.y = in.y - ((float) Math.sin(Math.toRadians(nextAngle)) * SIZE);
- }
- private void drawSnake(Tail tail, ShapeRenderer renderer) {
- renderer.begin(ShapeType.Line);
- renderer.identity();
- renderer.setColor(tail.position.x, tail.position.y, tail.angle, 255);
- renderer.translate(tail.position.x, tail.position.y, 0);
- renderer.rotate(0, 0, 1, tail.angle);
- renderer.triangle(0, -5, 0, 5, 10, 0);
- renderer.end();
- }
- private class Tail {
- public float angle;
- public Vector2 position;
- public Tail(Vector2 position, float angle) {
- this.position = position;
- this.angle = angle;
- }
- @Override
- public String toString() {
- return String.format("(%f,%f) %f", position.x, position.y, angle);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment