feat: magics

This commit is contained in:
Mahdi Dibaiee
2018-04-17 10:44:11 +04:30
parent f2e93fa79b
commit 7be2c8bd6a
18 changed files with 444 additions and 68 deletions

View File

@@ -1,6 +1,7 @@
apply plugin: "java"
sourceCompatibility = 1.6
targetCompatibility = 1.8
sourceCompatibility = 1.8
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'
sourceSets.main.java.srcDirs = [ "src/" ]

View File

@@ -1,28 +0,0 @@
package com.mdibaiee.supersnake;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.physics.box2d.Shape;
import com.badlogic.gdx.utils.Array;
public class Ball extends Point {
private Color color = Colors.ball;
public int radius = 15;
public int size = radius * 2;
public Ball(float x, float y) {
super(x, y);
}
public void draw(ShapeRenderer shapeRenderer) {
shapeRenderer.begin(ShapeRenderer.ShapeType.Filled);
shapeRenderer.setColor(color);
shapeRenderer.circle(x, y, radius);
shapeRenderer.end();
}
}

View File

@@ -6,4 +6,5 @@ public class Colors {
static public Color background = new Color(0.20f, 0.28f, 0.37f, 1);
static public Color snake = new Color(0, 0.9f, 0.7f, 1);
static public Color ball = new Color(1, 0.4f, 0, 1);
static public Color red = new Color(1, 0.34f, 0, 1);
}

View File

@@ -0,0 +1,75 @@
package com.mdibaiee.supersnake;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
public abstract class Magic extends SizedPoint {
SpriteBatch batch;
private Texture image;
public boolean active = false;
public int frames = 0;
public int drawn = 0;
public int lifetime = 20 * 60;
public int srcX = 0;
public int srcY = 0;
public int srcWidth = 0;
public int srcHeight = 0;
public float rotation = 0;
public float scale = 1;
private float drawn_width;
private float drawn_height;
public Magic(float x, float y, String image_url) {
super(x, y, 40);
image = new Texture(Gdx.files.internal(image_url));
batch = new SpriteBatch();
srcWidth = image.getWidth();
srcHeight = image.getHeight();
float ratio = (float) srcHeight / (float) srcWidth;
drawn_width = size;
drawn_height = size * ratio;
size = Math.max(size, size * ratio);
}
public boolean draw() {
drawn += 1;
float width = image.getWidth();
float height = image.getHeight();
batch.begin();
batch.draw(image, x, y,
size / 2, size / 2,
drawn_width, drawn_height,
scale, scale,
rotation,
srcX, srcY,
srcWidth, srcHeight,
false, false);
batch.end();
lifetime -= 1;
if (lifetime <= 0) {
return true;
}
return false;
}
abstract public void action(Snake snake);
// this method will be called after action at each iteration until it returns true
// when it does return true ,the object is disposed
abstract public boolean iter();
}

View File

@@ -0,0 +1,36 @@
package com.mdibaiee.supersnake.Magics;
import com.mdibaiee.supersnake.Magic;
import com.mdibaiee.supersnake.Snake;
public class Growth extends Magic {
private Snake snake;
private int seconds = 30;
public Growth(float x, float y) {
super(x, y, "blue.png");
}
public void action(Snake snake) {
this.snake = snake;
snake.size += 10;
}
public boolean draw() {
double cycle = Math.sin(((drawn * 6) % 360) * Math.PI / 360);
scale = (float) (1 + cycle * 0.3);
return super.draw();
}
public boolean iter() {
frames++;
if (frames > seconds * 60) {
snake.size -= 10;
return true;
}
return false;
}
}

View File

@@ -0,0 +1,30 @@
package com.mdibaiee.supersnake.Magics;
import com.mdibaiee.supersnake.Colors;
import com.mdibaiee.supersnake.Magic;
import com.mdibaiee.supersnake.Snake;
public class Skull extends Magic {
private Snake snake;
private int seconds = 15;
public Skull(float x, float y) {
super(x, y, "skull.png");
}
public void action(Snake snake) {
this.snake = snake;
snake.color = Colors.red;
}
public boolean iter() {
frames++;
if (frames > seconds * 60) {
snake.color = Colors.snake;
return true;
}
return false;
}
}

View File

@@ -0,0 +1,44 @@
package com.mdibaiee.supersnake.Magics;
import com.mdibaiee.supersnake.Magic;
import com.mdibaiee.supersnake.Snake;
public class SpeedBoost extends Magic {
private Snake snake;
private int seconds = 30;
private float original_x;
private float original_y;
public SpeedBoost(float x, float y) {
super(x, y, "green.png");
original_x = x;
original_y = y;
}
public void action(Snake snake) {
this.snake = snake;
snake.speed += 5;
}
public boolean draw() {
double rx = Math.random() * 5;
double ry = Math.random() * 5;
x = (float) (original_x + rx);
y = (float) (original_y + ry);
return super.draw();
}
public boolean iter() {
frames++;
if (frames > seconds * 60) {
snake.speed -= 5;
return true;
}
return false;
}
}

View File

@@ -0,0 +1,27 @@
package com.mdibaiee.supersnake.Magics;
import com.badlogic.gdx.Gdx;
import com.mdibaiee.supersnake.Magic;
import com.mdibaiee.supersnake.Snake;
public class StarPoint extends Magic {
public StarPoint(float x, float y) {
super(x, y, "star.png");
}
public boolean draw() {
rotation = drawn % 360;
return super.draw();
}
public void action(Snake snake) {
snake.point++;
snake.addTail();
}
public boolean iter() {
return true;
}
}

View File

@@ -39,6 +39,6 @@ public class Point {
}
public double distance(Point p) {
return Math.sqrt(Math.pow(this.x - p.x, 2) + Math.pow(this.y - p.y, 2));
return Math.sqrt(Math.pow(x - p.x, 2) + Math.pow(y - p.y, 2));
}
}

View File

@@ -0,0 +1,27 @@
package com.mdibaiee.supersnake;
public class SizedPoint extends Point {
public float size;
public SizedPoint(float x, float y, float size) {
super(x, y);
this.size = size;
}
public float centerX() {
return x + size / 2;
}
public float centerY() {
return y + size / 2;
}
public double distance(SizedPoint other) {
return Math.sqrt(Math.pow(centerX() - other.centerX(), 2)
+ Math.pow(centerY() - other.centerY(), 2));
}
public boolean collides(SizedPoint other) {
return distance(other) < (size / 2 + other.size / 2);
}
}

View File

@@ -10,10 +10,10 @@ import com.badlogic.gdx.physics.box2d.Shape;
import com.badlogic.gdx.utils.Array;
class Tail extends DirectedPoint {
public int length;
public float length;
public boolean breaking;
public Tail(float x, float y, Direction direction, int length) {
public Tail(float x, float y, Direction direction, float length) {
super(x, y, direction);
this.length = length;
}
@@ -25,12 +25,13 @@ class Tail extends DirectedPoint {
}
public class Snake extends DirectedPoint {
private Color color = Colors.snake;
public Color color = Colors.snake;
public int size = 10;
public int point = 0;
private Array<Tail> tail = new Array<Tail>();
public int speed = 2;
public float speed = 2;
public int lives = 3;
public Snake(float x, float y, int length) {
@@ -82,26 +83,14 @@ public class Snake extends DirectedPoint {
return 0;
}
private Direction getNewDirection(DirectedPoint point, DirectedPoint last) {
if (point.direction == last.direction) return point.direction;
float expectedX = point.x + cx(last.direction) * size;
float expectedY = point.y + cy(last.direction) * size;
if (Math.abs(last.x - expectedX) <= speed &&
Math.abs(last.y - expectedY) <= speed) {
return last.direction;
}
return point.direction;
}
public void move() {
float ox = x;
float oy = y;
boolean cycled = this.move(x + cx(direction) * speed,
y + cy(direction) * speed);
Gdx.app.log("Snake", "(" + ox + ", " + oy + ") > " + "(" + x + ", " + y + ")");
if (cycled) {
tail.insert(0, new Tail(ox, oy, direction, 0));
tail.insert(0, new Tail(x, y, direction, true));
@@ -114,8 +103,10 @@ public class Snake extends DirectedPoint {
last.move(last.x + cx(last.direction) * speed, last.y + cy(last.direction) * speed);
last.length -= speed;
if (last.length <= 0) {
while (tail.peek().length <= 0) {
float diff = tail.peek().length;
tail.removeIndex(tail.size - 1);
tail.peek().length += diff;
}
}
@@ -125,6 +116,11 @@ public class Snake extends DirectedPoint {
last.x -= cx(last.direction) * 5;
last.y -= cy(last.direction) * 5;
speed += 1;
speed += 0.5;
}
public boolean head_collision(SizedPoint p) {
SizedPoint s = new SizedPoint(x - size / 2, y - size / 2, size);
return s.collides(p);
}
}

View File

@@ -1,7 +1,5 @@
package com.mdibaiee.supersnake;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.mdibaiee.supersnake.Colors;
import com.mdibaiee.supersnake.Magics.*;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
@@ -9,7 +7,6 @@ import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.utils.Array;
@@ -17,15 +14,13 @@ public class SuperSnake extends ApplicationAdapter {
public int WIDTH = 800;
public int HEIGHT = 480;
int points = 0;
SpriteBatch batch;
ShapeRenderer shapeRenderer;
Texture img;
Snake snake;
BitmapFont font;
private Array<Ball> balls = new Array<Ball>();
private Array<Magic> magics = new Array<Magic>();
@Override
public void create () {
@@ -35,6 +30,8 @@ public class SuperSnake extends ApplicationAdapter {
WIDTH = Gdx.graphics.getBackBufferWidth();
HEIGHT = Gdx.graphics.getBackBufferHeight();
Gdx.app.log("Snake", "VIEWPORT " + WIDTH + ", " + HEIGHT);
shapeRenderer = new ShapeRenderer();
snake = new Snake(WIDTH / 2, HEIGHT / 2, 50);
@@ -64,25 +61,53 @@ public class SuperSnake extends ApplicationAdapter {
}
}
if (balls.size < 1) {
balls.add(new Ball((float) Math.random() * WIDTH, (float) Math.random() * HEIGHT));
int drawn_magics = 0;
for (Magic m: magics) {
if (!m.active) drawn_magics++;
}
if (drawn_magics < 3) {
double r = Math.random() * 100;
float mx = (float) Math.random() * WIDTH;
float my = (float) Math.random() * HEIGHT;
Magic newMagic;
if (r < 70) {
newMagic = new StarPoint(mx, my);
} else if (r < 80) {
newMagic = new SpeedBoost(mx, my);
} else if (r < 90) {
newMagic = new Growth(mx, my);
} else {
newMagic = new Skull(mx, my);
}
newMagic = new SpeedBoost(mx, my);
magics.add(newMagic);
}
snake.draw(shapeRenderer);
snake.move();
for(Ball b: balls) {
b.draw(shapeRenderer);
}
if (snake.distance(balls.first()) < balls.first().size) {
balls.removeIndex(0);
points += 1;
snake.addTail();
for(Magic m: magics) {
if (m.active) {
if (m.iter()) {
magics.removeValue(m, true);
}
} else {
if (m.draw()) {
magics.removeValue(m, true);
} else if (snake.head_collision(m)) {
m.action(snake);
m.active = true;
}
}
}
batch.begin();
font.draw(batch, Integer.toString(points), 10, 25);
font.draw(batch, Integer.toString(snake.point), 10, 25);
batch.end();
}