diff --git a/core/src/com/me/pacman/PacDude.java b/core/src/com/me/pacman/PacDude.java index 628cb6c..033b774 100644 --- a/core/src/com/me/pacman/PacDude.java +++ b/core/src/com/me/pacman/PacDude.java @@ -8,7 +8,7 @@ import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.graphics.glutils.ShapeRenderer; import com.badlogic.gdx.utils.viewport.FitViewport; import com.badlogic.gdx.utils.viewport.Viewport; -import com.me.pacman.state.MenuState; +import com.me.pacman.state.PlayState; public class PacDude extends Game { @@ -37,7 +37,7 @@ public class PacDude extends Game { batch = new SpriteBatch(); sr = new ShapeRenderer(); - setScreen(new MenuState(this)); + setScreen(new PlayState(this)); } @Override diff --git a/core/src/com/me/pacman/entity/Entity.java b/core/src/com/me/pacman/entity/Entity.java new file mode 100644 index 0000000..45426b5 --- /dev/null +++ b/core/src/com/me/pacman/entity/Entity.java @@ -0,0 +1,31 @@ +package com.me.pacman.entity; + +import com.badlogic.gdx.graphics.g2d.SpriteBatch; +import com.badlogic.gdx.graphics.g2d.TextureRegion; +import com.me.pacman.state.LevelState; + +public abstract class Entity { + + public LevelState state; + public float x; + public float y; + public int age; + + public Entity(LevelState state, float x, float y) { + this.state = state; + this.x = x; + this.y = y; + this.age = 0; + } + + public void render(SpriteBatch batch, int offsetX, int offsetY) { + batch.draw(getSprite(), (int) (x * 8) + (offsetX - 4), (y * 8) + (offsetY - 4)); + } + + public abstract TextureRegion getSprite(); + + public void update(float dt) { + this.age += 1; + } + +} diff --git a/core/src/com/me/pacman/entity/MovableEntity.java b/core/src/com/me/pacman/entity/MovableEntity.java new file mode 100644 index 0000000..7379b98 --- /dev/null +++ b/core/src/com/me/pacman/entity/MovableEntity.java @@ -0,0 +1,65 @@ +package com.me.pacman.entity; + +import com.me.pacman.state.LevelState; + +public abstract class MovableEntity extends Entity { + + // speed in tiles / second. + public float speed; + public Direction direction; + public boolean moving; + public boolean canMove = true; + + public MovableEntity(LevelState state, float x, float y, Direction direction, float speed, boolean moving) { + super(state, x, y); + this.speed = speed; + this.direction = direction; + this.moving = moving; + } + + public void update(float dt) { + if (!moving) { + return; + } + + float new_y = y; + float new_x = x; + + switch (direction) { + case NORTH: + new_y += speed * dt; + canMove = state.level.getComponent(new_x, new_y + 1f).isPassable(); + break; + case EAST: + new_x += speed * dt; + canMove = state.level.getComponent(new_x + 1f,new_y).isPassable(); + break; + case SOUTH: + new_y -= speed * dt; + canMove = state.level.getComponent(new_x, new_y - 1f).isPassable(); + break; + case WEST: + new_x -= speed * dt; + canMove = state.level.getComponent(new_x - 1f,new_y).isPassable(); + break; + } + + if (canMove) { + x = new_x; + y = new_y; + } else { + x = (int) new_x; + y = (int) new_y; + } + + super.update(dt); + } + + public enum Direction { + NORTH, + EAST, + SOUTH, + WEST + } + +} diff --git a/core/src/com/me/pacman/entity/Pacman.java b/core/src/com/me/pacman/entity/Pacman.java new file mode 100644 index 0000000..8bfcdc4 --- /dev/null +++ b/core/src/com/me/pacman/entity/Pacman.java @@ -0,0 +1,48 @@ +package com.me.pacman.entity; + +import com.badlogic.gdx.graphics.g2d.TextureRegion; +import com.me.pacman.state.PlayState; + +public class Pacman extends MovableEntity { + + private TextureRegion[][] sprite; + + private PlayState state; + private int counter = 1; + + public Pacman(PlayState state) { + super(state,13.5f, 7, Direction.EAST, 11f, true); + this.state = state; + sprite = state.getGame().assets.pacman; + } + + @Override + public TextureRegion getSprite() { + int spriteDir; + switch(direction) { + case NORTH: + spriteDir = 0; + break; + case SOUTH: + spriteDir = 1; + break; + case WEST: + spriteDir = 2; + break; + default: + spriteDir = 3; + break; + } + return sprite[spriteDir][canMove && moving? counter % 3 : 1]; + } + + @Override + public void update(float dt) { + super.update(dt); + + if (!state.paused && canMove && age % 4 == 0) { + counter += 1; + } + } + +} diff --git a/core/src/com/me/pacman/level/Level.java b/core/src/com/me/pacman/level/Level.java index 13fabe1..124794e 100644 --- a/core/src/com/me/pacman/level/Level.java +++ b/core/src/com/me/pacman/level/Level.java @@ -25,6 +25,18 @@ public class Level { components = loader.loadLevel(); } + public LevelComponent getComponent(int x, int y) { + return components[y][x]; + } + + public LevelComponent getComponent(float x, float y) { + return getComponent((int) x, (int) y); + } + + public void setComponent(int x, int y, LevelComponent component) { + components[y][x] = component; + } + public void render(int offsetX, int offsetY) { for (int i = 0; i < components.length; i++) { LevelComponent[] row = components[i]; diff --git a/core/src/com/me/pacman/level/LevelComponent.java b/core/src/com/me/pacman/level/LevelComponent.java index 1688b09..1c8aeb4 100644 --- a/core/src/com/me/pacman/level/LevelComponent.java +++ b/core/src/com/me/pacman/level/LevelComponent.java @@ -9,5 +9,10 @@ public enum LevelComponent { GHOST_CHAMBER, GHOST_GATE, EMPTY, + ; + + public boolean isPassable() { + return this != WALL && this != GHOST_CHAMBER && this != GHOST_GATE; + } } diff --git a/core/src/com/me/pacman/state/LevelState.java b/core/src/com/me/pacman/state/LevelState.java new file mode 100644 index 0000000..1f9b48a --- /dev/null +++ b/core/src/com/me/pacman/state/LevelState.java @@ -0,0 +1,14 @@ +package com.me.pacman.state; + +import com.me.pacman.PacDude; +import com.me.pacman.level.Level; + +public abstract class LevelState extends State { + + public Level level; + + public LevelState(PacDude game) { + super(game); + } + +} diff --git a/core/src/com/me/pacman/state/MenuState.java b/core/src/com/me/pacman/state/MenuState.java index 4b9c556..4888dd1 100644 --- a/core/src/com/me/pacman/state/MenuState.java +++ b/core/src/com/me/pacman/state/MenuState.java @@ -1,24 +1,16 @@ package com.me.pacman.state; -import com.badlogic.gdx.assets.loaders.FileHandleResolver; -import com.badlogic.gdx.assets.loaders.resolvers.InternalFileHandleResolver; -import com.badlogic.gdx.graphics.Color; import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.g2d.BitmapFont; -import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator; -import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGeneratorLoader; -import com.badlogic.gdx.graphics.g2d.freetype.FreetypeFontLoader; import com.me.pacman.PacDude; import com.me.pacman.level.Level; -public class MenuState extends State { +public class MenuState extends LevelState { private Texture levelBackground; private Texture logo; private BitmapFont font; - private Level level; - public MenuState(PacDude game) { super(game); } diff --git a/core/src/com/me/pacman/state/PlayState.java b/core/src/com/me/pacman/state/PlayState.java new file mode 100644 index 0000000..8f8207a --- /dev/null +++ b/core/src/com/me/pacman/state/PlayState.java @@ -0,0 +1,44 @@ +package com.me.pacman.state; + +import com.badlogic.gdx.graphics.Texture; +import com.badlogic.gdx.graphics.g2d.BitmapFont; +import com.me.pacman.PacDude; +import com.me.pacman.entity.Pacman; +import com.me.pacman.level.Level; + +public class PlayState extends LevelState { + + private Texture levelBackground; + private BitmapFont font; + + public boolean paused = false; + + private Pacman pacman; + + public PlayState(PacDude game) { + super(game); + } + + @Override + public void setup() { + levelBackground = game.assets.getLevelBackground(); + font = game.assets.getFont(); + + level = new Level(game,"level"); + + pacman = new Pacman(this); + } + + @Override + public void render() { + game.batch.draw(levelBackground, 0, 16); + level.render(0, 16); + + pacman.render(game.batch, 0, 16); + } + + @Override + public void update(float dt) { + pacman.update(dt); + } +}