From 5382e4896d8c37b3b4d4b563bbb61a8adc51448d Mon Sep 17 00:00:00 2001 From: Matt Low Date: Tue, 24 Dec 2019 23:39:27 +0400 Subject: [PATCH] Wakka Wakka --- core/src/com/me/pacman/Assets.java | 12 +++++- .../com/me/pacman/entity/MovableEntity.java | 16 ++++---- core/src/com/me/pacman/entity/Pacman.java | 4 +- core/src/com/me/pacman/level/Level.java | 39 ++++++++++++------- core/src/com/me/pacman/state/PlayState.java | 20 ++++++++++ 5 files changed, 67 insertions(+), 24 deletions(-) diff --git a/core/src/com/me/pacman/Assets.java b/core/src/com/me/pacman/Assets.java index cd1dc4c..c301ec6 100644 --- a/core/src/com/me/pacman/Assets.java +++ b/core/src/com/me/pacman/Assets.java @@ -3,6 +3,7 @@ package com.me.pacman; import com.badlogic.gdx.assets.AssetManager; import com.badlogic.gdx.assets.loaders.FileHandleResolver; 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.g2d.BitmapFont; import com.badlogic.gdx.graphics.g2d.TextureRegion; @@ -22,6 +23,9 @@ public class Assets { public TextureRegion[][] points; public TextureRegion[][] volume; + public Sound chomp_1; + public Sound chomp_2; + public Assets() { this.manager = new AssetManager(); } @@ -30,7 +34,6 @@ public class Assets { manager.load("level_background.png", Texture.class); manager.load("logo.png", Texture.class); - manager.load("sprites/level.png", Texture.class); manager.load("sprites/chase.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/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 FileHandleResolver resolver = new InternalFileHandleResolver(); 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); points = TextureRegion.split(manager.get("sprites/points.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() { diff --git a/core/src/com/me/pacman/entity/MovableEntity.java b/core/src/com/me/pacman/entity/MovableEntity.java index 1d05bc7..f923b19 100644 --- a/core/src/com/me/pacman/entity/MovableEntity.java +++ b/core/src/com/me/pacman/entity/MovableEntity.java @@ -34,28 +34,28 @@ public abstract class MovableEntity extends Entity { boolean turned = false; switch (nextDirection) { 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) { x = ((int) x) + 0.5f; turned = true; } break; 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) { y = ((int) y) + 0.5f; turned = true; } break; 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) { x = ((int) x) + 0.5f; turned = true; } break; 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) { y = ((int) y) + 0.5f; turned = true; @@ -75,19 +75,19 @@ public abstract class MovableEntity extends Entity { switch (currDirection) { case NORTH: 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; case EAST: 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; case SOUTH: 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; case WEST: 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; } diff --git a/core/src/com/me/pacman/entity/Pacman.java b/core/src/com/me/pacman/entity/Pacman.java index e335134..d8169d4 100644 --- a/core/src/com/me/pacman/entity/Pacman.java +++ b/core/src/com/me/pacman/entity/Pacman.java @@ -52,9 +52,9 @@ public class Pacman extends MovableEntity { counter += 1; } - switch (state.level.getComponent(x, y)) { + switch (state.level.getTile(x, y)) { case PELLET: - state.level.setComponent(x, y, LevelTile.EMPTY); + state.eatPellet(x, y); freezeFrames = 1; break; } diff --git a/core/src/com/me/pacman/level/Level.java b/core/src/com/me/pacman/level/Level.java index aaec5b8..77b2836 100644 --- a/core/src/com/me/pacman/level/Level.java +++ b/core/src/com/me/pacman/level/Level.java @@ -10,8 +10,8 @@ public class Level { private TextureRegion pellet; private TextureRegion powerPellet; - // Grid of components, [rows][columns] - public LevelTile[][] components; + // Grid of tiles, [rows][columns] + public LevelTile[][] tiles; public Level(PacDude game, String level) { @@ -21,28 +21,41 @@ public class Level { powerPellet = game.assets.level[0][7]; LevelLoader loader = new LevelLoader(level); - components = loader.loadLevel(); + tiles = loader.loadLevel(); } - public LevelTile getComponent(int x, int y) { - return components[y][x]; + public LevelTile getTile(int x, int y) { + return tiles[y][x]; } - public LevelTile getComponent(float x, float y) { - return getComponent((int) x, (int) y); + public LevelTile getTile(float x, float y) { + return getTile((int) x, (int) y); } - public void setComponent(int x, int y, LevelTile component) { - components[y][x] = component; + public void setTile(int x, int y, LevelTile tile) { + tiles[y][x] = tile; } - public void setComponent(float x, float y, LevelTile component) { - setComponent((int) x, (int) y, component); + public void setTile(float x, float y, LevelTile tile) { + 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) { - for (int i = 0; i < components.length; i++) { - LevelTile[] row = components[i]; + for (int i = 0; i < tiles.length; i++) { + LevelTile[] row = tiles[i]; for (int j = 0; j < row.length; j++) { LevelTile component = row[j]; TextureRegion sprite; diff --git a/core/src/com/me/pacman/state/PlayState.java b/core/src/com/me/pacman/state/PlayState.java index ea07e7d..b36a367 100644 --- a/core/src/com/me/pacman/state/PlayState.java +++ b/core/src/com/me/pacman/state/PlayState.java @@ -9,12 +9,17 @@ import com.me.pacman.PacDude; import com.me.pacman.entity.MovableEntity; import com.me.pacman.entity.Pacman; import com.me.pacman.level.Level; +import com.me.pacman.level.LevelTile; public class PlayState extends LevelState { private Texture levelBackground; private BitmapFont font; + private int pelletCount = 0; + private int pelletEatenCount = 0; + private int score = 0; + public boolean paused = false; private Pacman pacman; @@ -29,6 +34,7 @@ public class PlayState extends LevelState { font = game.assets.getFont(); level = new Level(game,"level"); + pelletCount = level.getTileCount(LevelTile.PELLET); pacman = new Pacman(this); @@ -41,6 +47,8 @@ public class PlayState extends LevelState { level.render(0, 16); pacman.render(game.batch, 0, 16); + + game.assets.getFont().draw(game.batch, "" + score, 25, 276); } @Override @@ -53,6 +61,18 @@ public class PlayState extends LevelState { 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 { @Override