-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from tonyzhang617/foods
Implement food (cakes) for snakes
- Loading branch information
Showing
17 changed files
with
544 additions
and
253 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
core/src/com/tianyi/zhang/multiplayer/snake/elements/Foods.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package com.tianyi.zhang.multiplayer.snake.elements; | ||
|
||
import com.tianyi.zhang.multiplayer.snake.helpers.Constants; | ||
|
||
import java.util.*; | ||
|
||
public class Foods { | ||
public static final int WIDTH = Constants.WIDTH; | ||
public static final int HEIGHT = Constants.HEIGHT; | ||
|
||
private final SortedSet<Integer> locations; | ||
private final Random random; | ||
|
||
public Foods() { | ||
locations = new TreeSet<Integer>(); | ||
random = new Random(Constants.SEED); | ||
} | ||
|
||
public Foods(Foods other) { | ||
locations = new TreeSet<Integer>(other.locations); | ||
random = new Random(Constants.SEED); | ||
} | ||
|
||
public int getQuantity() { | ||
return locations.size(); | ||
} | ||
|
||
/** | ||
* | ||
* @param exclude an ascending sorted array of Integers to be excluded from the random generation | ||
* @return | ||
*/ | ||
public Integer generateHelper(List<Integer> exclude) { | ||
Integer randomInt = Integer.valueOf(random.nextInt(HEIGHT * WIDTH - exclude.size())); | ||
for (Integer e : exclude) { | ||
if (randomInt.compareTo(e) < 0) { | ||
break; | ||
} | ||
randomInt += 1; | ||
} | ||
return randomInt; | ||
} | ||
|
||
public void generate(List<Snake> snakes) { | ||
List<Integer> exclude = new LinkedList<Integer>(); | ||
for (Snake s : snakes) { | ||
List<Integer> coords = s.getCoordinates(); | ||
for (int i = 0; i < coords.size(); i += 2) { | ||
exclude.add(indexFromXy(coords.get(i), coords.get(i+1))); | ||
} | ||
} | ||
exclude.addAll(locations); | ||
Collections.sort(exclude); | ||
for (int i = 1; i <= Constants.MAX_FOOD_QUANTITY - getQuantity(); ++i) { | ||
Integer newLoc = generateHelper(exclude); | ||
locations.add(newLoc); | ||
int size = exclude.size(); | ||
for (int j = 0; j <= size; ++j) { | ||
if (j == size) { | ||
exclude.add(newLoc); | ||
} else if (newLoc < exclude.get(j)) { | ||
exclude.add(j, newLoc); | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
|
||
public void consumedBy(Snake snake) { | ||
Integer headIndex = indexFromXy(snake.getHeadX(), snake.getHeadY()); | ||
if (locations.contains(headIndex)) { | ||
locations.remove(headIndex); | ||
snake.grow(); | ||
} | ||
} | ||
|
||
public boolean shouldGenerate() { | ||
return getQuantity() <= Constants.MIN_FOOD_QUANTITY; | ||
} | ||
|
||
public Integer indexFromXy(Integer x, Integer y) { | ||
return Integer.valueOf(y*WIDTH + x); | ||
} | ||
|
||
public Integer xFromIndex(int index) { | ||
return index - index / WIDTH * WIDTH; | ||
} | ||
|
||
public int yFromIndex(int index) { | ||
return index / WIDTH; | ||
} | ||
|
||
public List<Integer> getLocations() { | ||
List<Integer> result = new ArrayList<Integer>(locations.size() * 2); | ||
Iterator<Integer> iterator = locations.iterator(); | ||
while (iterator.hasNext()) { | ||
Integer index = iterator.next(); | ||
result.add(Integer.valueOf(xFromIndex(index))); | ||
result.add(Integer.valueOf(yFromIndex(index))); | ||
} | ||
return Collections.unmodifiableList(result); | ||
} | ||
|
||
public void setLocations(List<Integer> xyLocations) { | ||
this.locations.clear(); | ||
for (int i = 0; i < xyLocations.size(); i += 2) { | ||
this.locations.add(indexFromXy(xyLocations.get(i), xyLocations.get(i+1))); | ||
} | ||
} | ||
} |
80 changes: 80 additions & 0 deletions
80
core/src/com/tianyi/zhang/multiplayer/snake/elements/GameRenderer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package com.tianyi.zhang.multiplayer.snake.elements; | ||
|
||
import com.badlogic.gdx.Gdx; | ||
import com.badlogic.gdx.graphics.Camera; | ||
import com.badlogic.gdx.graphics.GL20; | ||
import com.badlogic.gdx.graphics.Texture; | ||
import com.badlogic.gdx.graphics.g2d.Sprite; | ||
import com.badlogic.gdx.graphics.g2d.SpriteBatch; | ||
import com.tianyi.zhang.multiplayer.snake.helpers.Constants; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import static com.tianyi.zhang.multiplayer.snake.elements.Grid.Block.FOOD; | ||
import static com.tianyi.zhang.multiplayer.snake.elements.Grid.Block.GROUND; | ||
import static com.tianyi.zhang.multiplayer.snake.elements.Grid.Block.PLAYER_SNAKE_BODY; | ||
import static com.tianyi.zhang.multiplayer.snake.helpers.Constants.BLOCK_LENGTH; | ||
import static com.tianyi.zhang.multiplayer.snake.helpers.Constants.BLOCK_OFFSET; | ||
|
||
public enum GameRenderer { | ||
INSTANCE; | ||
|
||
private Map<Grid.Block, Sprite> spriteMap; | ||
private SpriteBatch batch; | ||
|
||
private GameRenderer() { | ||
Sprite playerSnakeBody = new Sprite(newTextureWithLinearFilter("player_snake_body.png")); | ||
Sprite food = new Sprite(newTextureWithLinearFilter("cake.png")); | ||
Sprite ground = new Sprite(newTextureWithLinearFilter("ground.png")); | ||
|
||
spriteMap = new HashMap<Grid.Block, Sprite>(); | ||
spriteMap.put(PLAYER_SNAKE_BODY, playerSnakeBody); | ||
spriteMap.put(FOOD, food); | ||
spriteMap.put(GROUND, ground); | ||
|
||
batch = new SpriteBatch(); | ||
} | ||
|
||
private static Texture newTextureWithLinearFilter(String imagePath) { | ||
Texture texture = new Texture(Gdx.files.internal(imagePath)); | ||
texture.setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear); | ||
return texture; | ||
} | ||
|
||
public void clear() { | ||
Gdx.gl.glClearColor(0, 0, 0, 1); | ||
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); | ||
} | ||
|
||
public void render(Grid grid, Camera camera) { | ||
camera.update(); | ||
|
||
batch.setProjectionMatrix(camera.combined); | ||
batch.begin(); | ||
|
||
for (int y = Grid.HEIGHT - 1; y >= 0; --y) { | ||
for (int x = 0; x < Grid.WIDTH; ++x) { | ||
switch (grid.getBlockByCoordinate(x, y)) { | ||
case PLAYER_SNAKE_BODY: | ||
case SNAKE_BODY: | ||
batch.draw(spriteMap.get(PLAYER_SNAKE_BODY), x - Constants.BLOCK_OFFSET, y, BLOCK_LENGTH + BLOCK_OFFSET, BLOCK_LENGTH + BLOCK_OFFSET); | ||
break; | ||
case FOOD: | ||
batch.draw(spriteMap.get(GROUND), x, y, BLOCK_LENGTH, BLOCK_LENGTH); | ||
batch.draw(spriteMap.get(FOOD), x - Constants.BLOCK_OFFSET, y, BLOCK_LENGTH + BLOCK_OFFSET, BLOCK_LENGTH + BLOCK_OFFSET); | ||
break; | ||
case GROUND: | ||
batch.draw(spriteMap.get(GROUND), x, y, BLOCK_LENGTH, BLOCK_LENGTH); | ||
break; | ||
} | ||
} | ||
} | ||
|
||
batch.end(); | ||
} | ||
|
||
public void dispose() { | ||
batch.dispose(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.