Wakka Wakka

This commit is contained in:
Matt Low 2019-12-24 23:39:27 +04:00
parent a23e9169b7
commit 5382e4896d
5 changed files with 67 additions and 24 deletions

View File

@ -3,6 +3,7 @@ package com.me.pacman;
import com.badlogic.gdx.assets.AssetManager; import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.assets.loaders.FileHandleResolver; import com.badlogic.gdx.assets.loaders.FileHandleResolver;
import com.badlogic.gdx.assets.loaders.resolvers.InternalFileHandleResolver; import com.badlogic.gdx.assets.loaders.resolvers.InternalFileHandleResolver;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont; import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.TextureRegion; import com.badlogic.gdx.graphics.g2d.TextureRegion;
@ -22,6 +23,9 @@ public class Assets {
public TextureRegion[][] points; public TextureRegion[][] points;
public TextureRegion[][] volume; public TextureRegion[][] volume;
public Sound chomp_1;
public Sound chomp_2;
public Assets() { public Assets() {
this.manager = new AssetManager(); this.manager = new AssetManager();
} }
@ -30,7 +34,6 @@ public class Assets {
manager.load("level_background.png", Texture.class); manager.load("level_background.png", Texture.class);
manager.load("logo.png", Texture.class); manager.load("logo.png", Texture.class);
manager.load("sprites/level.png", Texture.class); manager.load("sprites/level.png", Texture.class);
manager.load("sprites/chase.png", Texture.class); manager.load("sprites/chase.png", Texture.class);
manager.load("sprites/death.png", Texture.class); manager.load("sprites/death.png", Texture.class);
@ -39,6 +42,9 @@ public class Assets {
manager.load("sprites/points.png", Texture.class); manager.load("sprites/points.png", Texture.class);
manager.load("sprites/volume.png", Texture.class); manager.load("sprites/volume.png", Texture.class);
manager.load("sounds/chomp_1.wav", Sound.class);
manager.load("sounds/chomp_2.wav", Sound.class);
// Yayyy! all of this to load a font // Yayyy! all of this to load a font
FileHandleResolver resolver = new InternalFileHandleResolver(); FileHandleResolver resolver = new InternalFileHandleResolver();
manager.setLoader(FreeTypeFontGenerator.class, new FreeTypeFontGeneratorLoader(resolver)); manager.setLoader(FreeTypeFontGenerator.class, new FreeTypeFontGeneratorLoader(resolver));
@ -60,6 +66,10 @@ public class Assets {
pacman = TextureRegion.split(manager.get("sprites/pacman.png", Texture.class), 16, 16); pacman = TextureRegion.split(manager.get("sprites/pacman.png", Texture.class), 16, 16);
points = TextureRegion.split(manager.get("sprites/points.png", Texture.class), 16, 16); points = TextureRegion.split(manager.get("sprites/points.png", Texture.class), 16, 16);
volume = TextureRegion.split(manager.get("sprites/volume.png", Texture.class), 16, 16); volume = TextureRegion.split(manager.get("sprites/volume.png", Texture.class), 16, 16);
// all our sounds
chomp_1 = manager.get("sounds/chomp_1.wav", Sound.class);
chomp_2 = manager.get("sounds/chomp_2.wav", Sound.class);
} }
public Texture getLevelBackground() { public Texture getLevelBackground() {

View File

@ -34,28 +34,28 @@ public abstract class MovableEntity extends Entity {
boolean turned = false; boolean turned = false;
switch (nextDirection) { switch (nextDirection) {
case NORTH: case NORTH:
nextComponent = state.level.getComponent(x, y + 1f); nextComponent = state.level.getTile(x, y + 1f);
if (nextComponent.isPassable() && Math.abs(x - ((int) x + 0.5f)) <= tolerance) { if (nextComponent.isPassable() && Math.abs(x - ((int) x + 0.5f)) <= tolerance) {
x = ((int) x) + 0.5f; x = ((int) x) + 0.5f;
turned = true; turned = true;
} }
break; break;
case EAST: case EAST:
nextComponent = state.level.getComponent(x + 1f, y); nextComponent = state.level.getTile(x + 1f, y);
if (nextComponent.isPassable() && Math.abs(y - ((int) y + 0.5f)) <= tolerance) { if (nextComponent.isPassable() && Math.abs(y - ((int) y + 0.5f)) <= tolerance) {
y = ((int) y) + 0.5f; y = ((int) y) + 0.5f;
turned = true; turned = true;
} }
break; break;
case SOUTH: case SOUTH:
nextComponent = state.level.getComponent(x, y - 1f); nextComponent = state.level.getTile(x, y - 1f);
if (nextComponent.isPassable() && Math.abs(x - ((int) x + 0.5f)) <= tolerance) { if (nextComponent.isPassable() && Math.abs(x - ((int) x + 0.5f)) <= tolerance) {
x = ((int) x) + 0.5f; x = ((int) x) + 0.5f;
turned = true; turned = true;
} }
break; break;
case WEST: case WEST:
nextComponent = state.level.getComponent(x - 1f, y); nextComponent = state.level.getTile(x - 1f, y);
if (nextComponent.isPassable() && Math.abs(y - ((int) y + 0.5f)) <= tolerance) { if (nextComponent.isPassable() && Math.abs(y - ((int) y + 0.5f)) <= tolerance) {
y = ((int) y) + 0.5f; y = ((int) y) + 0.5f;
turned = true; turned = true;
@ -75,19 +75,19 @@ public abstract class MovableEntity extends Entity {
switch (currDirection) { switch (currDirection) {
case NORTH: case NORTH:
new_y += dist; new_y += dist;
canMove = state.level.getComponent(new_x, new_y + 0.5f).isPassable(); canMove = state.level.getTile(new_x, new_y + 0.5f).isPassable();
break; break;
case EAST: case EAST:
new_x += dist; new_x += dist;
canMove = state.level.getComponent(new_x + 0.5f, new_y).isPassable(); canMove = state.level.getTile(new_x + 0.5f, new_y).isPassable();
break; break;
case SOUTH: case SOUTH:
new_y -= dist; new_y -= dist;
canMove = state.level.getComponent(new_x, new_y - 0.5f).isPassable(); canMove = state.level.getTile(new_x, new_y - 0.5f).isPassable();
break; break;
case WEST: case WEST:
new_x -= dist; new_x -= dist;
canMove = state.level.getComponent(new_x - 0.5f, new_y).isPassable(); canMove = state.level.getTile(new_x - 0.5f, new_y).isPassable();
break; break;
} }

View File

@ -52,9 +52,9 @@ public class Pacman extends MovableEntity {
counter += 1; counter += 1;
} }
switch (state.level.getComponent(x, y)) { switch (state.level.getTile(x, y)) {
case PELLET: case PELLET:
state.level.setComponent(x, y, LevelTile.EMPTY); state.eatPellet(x, y);
freezeFrames = 1; freezeFrames = 1;
break; break;
} }

View File

@ -10,8 +10,8 @@ public class Level {
private TextureRegion pellet; private TextureRegion pellet;
private TextureRegion powerPellet; private TextureRegion powerPellet;
// Grid of components, [rows][columns] // Grid of tiles, [rows][columns]
public LevelTile[][] components; public LevelTile[][] tiles;
public Level(PacDude game, String level) { public Level(PacDude game, String level) {
@ -21,28 +21,41 @@ public class Level {
powerPellet = game.assets.level[0][7]; powerPellet = game.assets.level[0][7];
LevelLoader loader = new LevelLoader(level); LevelLoader loader = new LevelLoader(level);
components = loader.loadLevel(); tiles = loader.loadLevel();
} }
public LevelTile getComponent(int x, int y) { public LevelTile getTile(int x, int y) {
return components[y][x]; return tiles[y][x];
} }
public LevelTile getComponent(float x, float y) { public LevelTile getTile(float x, float y) {
return getComponent((int) x, (int) y); return getTile((int) x, (int) y);
} }
public void setComponent(int x, int y, LevelTile component) { public void setTile(int x, int y, LevelTile tile) {
components[y][x] = component; tiles[y][x] = tile;
} }
public void setComponent(float x, float y, LevelTile component) { public void setTile(float x, float y, LevelTile tile) {
setComponent((int) x, (int) y, component); setTile((int) x, (int) y, tile);
}
public int getTileCount(LevelTile tile) {
int count = 0;
for (int i = 0; i < tiles.length; i++) {
LevelTile[] row = tiles[i];
for (int j = 0; j < row.length; j++) {
if (row[j] == tile) {
count++;
}
}
}
return count;
} }
public void render(int offsetX, int offsetY) { public void render(int offsetX, int offsetY) {
for (int i = 0; i < components.length; i++) { for (int i = 0; i < tiles.length; i++) {
LevelTile[] row = components[i]; LevelTile[] row = tiles[i];
for (int j = 0; j < row.length; j++) { for (int j = 0; j < row.length; j++) {
LevelTile component = row[j]; LevelTile component = row[j];
TextureRegion sprite; TextureRegion sprite;

View File

@ -9,12 +9,17 @@ import com.me.pacman.PacDude;
import com.me.pacman.entity.MovableEntity; import com.me.pacman.entity.MovableEntity;
import com.me.pacman.entity.Pacman; import com.me.pacman.entity.Pacman;
import com.me.pacman.level.Level; import com.me.pacman.level.Level;
import com.me.pacman.level.LevelTile;
public class PlayState extends LevelState { public class PlayState extends LevelState {
private Texture levelBackground; private Texture levelBackground;
private BitmapFont font; private BitmapFont font;
private int pelletCount = 0;
private int pelletEatenCount = 0;
private int score = 0;
public boolean paused = false; public boolean paused = false;
private Pacman pacman; private Pacman pacman;
@ -29,6 +34,7 @@ public class PlayState extends LevelState {
font = game.assets.getFont(); font = game.assets.getFont();
level = new Level(game,"level"); level = new Level(game,"level");
pelletCount = level.getTileCount(LevelTile.PELLET);
pacman = new Pacman(this); pacman = new Pacman(this);
@ -41,6 +47,8 @@ public class PlayState extends LevelState {
level.render(0, 16); level.render(0, 16);
pacman.render(game.batch, 0, 16); pacman.render(game.batch, 0, 16);
game.assets.getFont().draw(game.batch, "" + score, 25, 276);
} }
@Override @Override
@ -53,6 +61,18 @@ public class PlayState extends LevelState {
Gdx.input.setInputProcessor(null); Gdx.input.setInputProcessor(null);
} }
public void eatPellet(float x, float y) {
level.setTile(x, y, LevelTile.EMPTY);
if (pelletEatenCount % 2 == 0) {
game.assets.chomp_1.play(1.0f);
} else {
game.assets.chomp_2.play(1.0f);
}
pelletEatenCount++;
pelletCount--;
score += 10;
}
private final class Controller extends InputAdapter { private final class Controller extends InputAdapter {
@Override @Override