A PacDude is born.
And he moves! But you can't control him.
This commit is contained in:
parent
765151a654
commit
76c169fe5a
@ -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
|
||||
|
31
core/src/com/me/pacman/entity/Entity.java
Normal file
31
core/src/com/me/pacman/entity/Entity.java
Normal file
@ -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;
|
||||
}
|
||||
|
||||
}
|
65
core/src/com/me/pacman/entity/MovableEntity.java
Normal file
65
core/src/com/me/pacman/entity/MovableEntity.java
Normal file
@ -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
|
||||
}
|
||||
|
||||
}
|
48
core/src/com/me/pacman/entity/Pacman.java
Normal file
48
core/src/com/me/pacman/entity/Pacman.java
Normal file
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -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];
|
||||
|
@ -9,5 +9,10 @@ public enum LevelComponent {
|
||||
GHOST_CHAMBER,
|
||||
GHOST_GATE,
|
||||
EMPTY,
|
||||
;
|
||||
|
||||
public boolean isPassable() {
|
||||
return this != WALL && this != GHOST_CHAMBER && this != GHOST_GATE;
|
||||
}
|
||||
|
||||
}
|
||||
|
14
core/src/com/me/pacman/state/LevelState.java
Normal file
14
core/src/com/me/pacman/state/LevelState.java
Normal file
@ -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);
|
||||
}
|
||||
|
||||
}
|
@ -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);
|
||||
}
|
||||
|
44
core/src/com/me/pacman/state/PlayState.java
Normal file
44
core/src/com/me/pacman/state/PlayState.java
Normal file
@ -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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user