Shift from Game-Design
This commit is contained in:
commit
bb41a130ba
153 changed files with 4046 additions and 0 deletions
27
src/Defaults.java
Normal file
27
src/Defaults.java
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
/**
|
||||
* @author Jose Stovall
|
||||
* A launcher class in the default package which runs the main game
|
||||
* Also handles copying and setting LWJGL Natives
|
||||
*/
|
||||
|
||||
import edu.utc.gamedev.Game;
|
||||
import edu.utc.gamedev.Util.FileUtils;
|
||||
import org.newdawn.slick.util.ResourceLoader;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
public class Defaults
|
||||
{
|
||||
public static void main(String[] args)
|
||||
{
|
||||
// Copies the natives folder FROM the compiled jar if it doesn't exist
|
||||
if (!(new File("natives").exists()))
|
||||
{
|
||||
FileUtils.copyResourcesRecursively(ResourceLoader.getResource("natives"), new File("natives"));
|
||||
}
|
||||
|
||||
// Sets the LWJGL Libraries path to the natives folder
|
||||
System.setProperty("org.lwjgl.librarypath", new File("natives").getAbsolutePath());
|
||||
new Game();
|
||||
}
|
||||
}
|
||||
3
src/META-INF/MANIFEST.MF
Normal file
3
src/META-INF/MANIFEST.MF
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
Manifest-Version: 1.0
|
||||
Main-Class: Defaults
|
||||
|
||||
21
src/edu/utc/gamedev/Entity/Entity.java
Normal file
21
src/edu/utc/gamedev/Entity/Entity.java
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
package edu.utc.gamedev.Entity;
|
||||
|
||||
import org.lwjgl.util.Rectangle;
|
||||
|
||||
public abstract class Entity
|
||||
{
|
||||
public void update(float delta)
|
||||
{
|
||||
}
|
||||
|
||||
public void draw()
|
||||
{
|
||||
}
|
||||
|
||||
public Rectangle getHitBox()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
66
src/edu/utc/gamedev/Entity/Item/EntityHeart.java
Normal file
66
src/edu/utc/gamedev/Entity/Item/EntityHeart.java
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
package edu.utc.gamedev.Entity.Item;
|
||||
|
||||
import edu.utc.gamedev.Item.ItemHeart;
|
||||
import edu.utc.gamedev.Manager.SceneManager;
|
||||
import edu.utc.gamedev.Scene.SceneLevel;
|
||||
import edu.utc.gamedev.Util.Helper;
|
||||
import org.lwjgl.opengl.Display;
|
||||
import org.lwjgl.util.Rectangle;
|
||||
|
||||
public class EntityHeart extends EntityItem
|
||||
{
|
||||
private SceneLevel scene = SceneManager.getInstance().getScene();
|
||||
|
||||
public EntityHeart(int x, int y)
|
||||
{
|
||||
super(new ItemHeart(), x, y);
|
||||
this.box = new Rectangle(x, y, sprite.getTextureWidth(), sprite.getTextureHeight());
|
||||
state = State.IDLE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(float delta)
|
||||
{
|
||||
// Handle hitbox intersection
|
||||
if (scene.getPlayer() != null && this.box.intersects(scene.getPlayer().getHitBox()))
|
||||
{
|
||||
scene.getPlayer().damage(-10F);
|
||||
scene.getEntityManager().removeEntity(this);
|
||||
}
|
||||
if (Helper.getTime() - spawnTime >= 9000)
|
||||
{
|
||||
scene.getEntityManager().removeEntity(this);
|
||||
}
|
||||
|
||||
int motion = 1;
|
||||
if (state != State.IDLE && Helper.getTime() - lastMoved < 256)
|
||||
return;
|
||||
lastMoved = Helper.getTime();
|
||||
switch (state)
|
||||
{
|
||||
case FALLING:
|
||||
box.translate(0, motion);
|
||||
if (box.getY() >= (Display.getHeight() - (box.getHeight())))
|
||||
{
|
||||
box.setY(Display.getHeight() - (box.getHeight()));
|
||||
state = State.RISING;
|
||||
}
|
||||
break;
|
||||
case RISING:
|
||||
box.translate(0, -motion);
|
||||
if (box.getY() <= (Display.getHeight() - (box.getHeight() * 1.05)))
|
||||
{
|
||||
state = State.FALLING;
|
||||
}
|
||||
break;
|
||||
case IDLE:
|
||||
box.translate(0, motion * 4);
|
||||
if (box.getY() >= (Display.getHeight() - (box.getHeight())))
|
||||
{
|
||||
box.setY(Display.getHeight() - (box.getHeight()));
|
||||
state = State.RISING;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
119
src/edu/utc/gamedev/Entity/Item/EntityItem.java
Normal file
119
src/edu/utc/gamedev/Entity/Item/EntityItem.java
Normal file
|
|
@ -0,0 +1,119 @@
|
|||
package edu.utc.gamedev.Entity.Item;
|
||||
|
||||
import edu.utc.gamedev.Entity.Entity;
|
||||
import edu.utc.gamedev.Item.Item;
|
||||
import edu.utc.gamedev.Manager.SceneManager;
|
||||
import edu.utc.gamedev.Util.Helper;
|
||||
import org.lwjgl.opengl.Display;
|
||||
import org.lwjgl.opengl.GL11;
|
||||
import org.lwjgl.util.Rectangle;
|
||||
import org.newdawn.slick.opengl.Texture;
|
||||
|
||||
public class EntityItem extends Entity
|
||||
{
|
||||
protected Item actualItem;
|
||||
protected Rectangle box;
|
||||
protected Texture sprite;
|
||||
protected float wr, hr;
|
||||
protected State state;
|
||||
protected long spawnTime;
|
||||
protected long lastMoved;
|
||||
protected int lifespan;
|
||||
|
||||
public EntityItem(Item item, int x, int y, int lifespan)
|
||||
{
|
||||
this.actualItem = item;
|
||||
sprite = Helper.loadTexture("items/" + item.getIconString());
|
||||
this.box = new Rectangle(x, y, 64, (64 * sprite.getImageHeight() / sprite.getImageWidth()));
|
||||
this.box.setY(y - box.getHeight());
|
||||
this.wr = 1.0f * sprite.getImageWidth() / sprite.getTextureWidth();
|
||||
this.hr = 1.0f * sprite.getImageHeight() / sprite.getTextureHeight();
|
||||
this.state = State.IDLE;
|
||||
this.spawnTime = Helper.getTime();
|
||||
this.lifespan = lifespan;
|
||||
}
|
||||
|
||||
public EntityItem(Item item, int x, int y)
|
||||
{
|
||||
this(item, x, y, 9000);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void draw()
|
||||
{
|
||||
float x = (float) box.getX();
|
||||
float y = (float) box.getY();
|
||||
float w = (float) box.getWidth();
|
||||
float h = (float) box.getHeight();
|
||||
|
||||
GL11.glBindTexture(GL11.GL_TEXTURE_2D, sprite.getTextureID());
|
||||
GL11.glColor3f(1F, 1F, 1F);
|
||||
GL11.glBegin(GL11.GL_QUADS);
|
||||
|
||||
GL11.glTexCoord2f(0F, 0F);
|
||||
GL11.glVertex2f(x, y);
|
||||
|
||||
GL11.glTexCoord2f(wr, 0F);
|
||||
GL11.glVertex2f(x + w, y);
|
||||
|
||||
GL11.glTexCoord2f(wr, hr);
|
||||
GL11.glVertex2f(x + w, y + h);
|
||||
|
||||
GL11.glTexCoord2f(0F, hr);
|
||||
GL11.glVertex2f(x, y + h);
|
||||
|
||||
GL11.glEnd();
|
||||
GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(float delta)
|
||||
{
|
||||
// Handle hitbox intersection
|
||||
if (SceneManager.getInstance().getScene().getPlayer() != null && this.box.intersects(SceneManager.getInstance().getScene().getPlayer().getHitBox()))
|
||||
{
|
||||
SceneManager.getInstance().getScene().getPlayer().setHeldItem(this.actualItem.setCursor());
|
||||
SceneManager.getInstance().getScene().getEntityManager().removeEntity(this);
|
||||
}
|
||||
if (this.lifespan > 0 && Helper.getTime() - spawnTime >= this.lifespan)
|
||||
{
|
||||
SceneManager.getInstance().getScene().getEntityManager().removeEntity(this);
|
||||
}
|
||||
|
||||
int motion = 1;
|
||||
if (state != State.IDLE && Helper.getTime() - lastMoved < 256)
|
||||
return;
|
||||
lastMoved = Helper.getTime();
|
||||
switch (state)
|
||||
{
|
||||
case FALLING:
|
||||
box.translate(0, motion);
|
||||
if (box.getY() >= (Display.getHeight() - (box.getHeight())))
|
||||
{
|
||||
box.setY(Display.getHeight() - (box.getHeight()));
|
||||
state = State.RISING;
|
||||
}
|
||||
break;
|
||||
case RISING:
|
||||
box.translate(0, -motion);
|
||||
if (box.getY() <= (Display.getHeight() - (box.getHeight() * 1.05)))
|
||||
{
|
||||
state = State.FALLING;
|
||||
}
|
||||
break;
|
||||
case IDLE:
|
||||
box.translate(0, motion * 4);
|
||||
if (box.getY() >= (Display.getHeight() - (box.getHeight())))
|
||||
{
|
||||
box.setY(Display.getHeight() - (box.getHeight()));
|
||||
state = State.RISING;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public enum State
|
||||
{
|
||||
FALLING, RISING, IDLE
|
||||
}
|
||||
}
|
||||
66
src/edu/utc/gamedev/Entity/Living/EntityLiving.java
Normal file
66
src/edu/utc/gamedev/Entity/Living/EntityLiving.java
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
package edu.utc.gamedev.Entity.Living;
|
||||
|
||||
import edu.utc.gamedev.Entity.Entity;
|
||||
import edu.utc.gamedev.Manager.SceneManager;
|
||||
import org.lwjgl.opengl.GL11;
|
||||
|
||||
public abstract class EntityLiving extends Entity
|
||||
{
|
||||
public float getHealth()
|
||||
{
|
||||
return 0F;
|
||||
}
|
||||
|
||||
public float getMaxHealth()
|
||||
{
|
||||
return 0F;
|
||||
}
|
||||
|
||||
|
||||
public void damage(float amount)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public void drawHealth()
|
||||
{
|
||||
float amtToDraw = getHealth() / getMaxHealth();
|
||||
|
||||
float x = (float) this.getHitBox().getX();
|
||||
float y = (float) this.getHitBox().getY() - 12;
|
||||
float w = (float) this.getHitBox().getWidth();
|
||||
float h = 6F;
|
||||
|
||||
GL11.glColor3f(0F, 0F, 0F);
|
||||
GL11.glBegin(GL11.GL_QUADS);
|
||||
|
||||
GL11.glVertex2f(x, y);
|
||||
GL11.glVertex2f(x + w, y);
|
||||
GL11.glVertex2f(x + w, y + h);
|
||||
GL11.glVertex2f(x, y + h);
|
||||
|
||||
GL11.glEnd();
|
||||
|
||||
w = this.getHitBox().getWidth() * amtToDraw;
|
||||
|
||||
float rAmt = Math.abs((amtToDraw * 255F - 255F) / 255F);
|
||||
float gAmt = amtToDraw;
|
||||
|
||||
GL11.glColor3f(rAmt, gAmt, 0);
|
||||
GL11.glBegin(GL11.GL_QUADS);
|
||||
|
||||
GL11.glVertex2f(x, y);
|
||||
GL11.glVertex2f(x + w, y);
|
||||
GL11.glVertex2f(x + w, y + h);
|
||||
GL11.glVertex2f(x, y + h);
|
||||
|
||||
GL11.glEnd();
|
||||
}
|
||||
|
||||
public void kill()
|
||||
{
|
||||
SceneManager.getInstance().getScene().getEntityManager().removeEntity(this);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
366
src/edu/utc/gamedev/Entity/Living/EntityMonster.java
Normal file
366
src/edu/utc/gamedev/Entity/Living/EntityMonster.java
Normal file
|
|
@ -0,0 +1,366 @@
|
|||
package edu.utc.gamedev.Entity.Living;
|
||||
|
||||
import edu.utc.gamedev.Entity.Item.EntityHeart;
|
||||
import edu.utc.gamedev.Manager.AudioManager;
|
||||
import edu.utc.gamedev.Manager.SceneManager;
|
||||
import edu.utc.gamedev.Scene.SceneLevel;
|
||||
import edu.utc.gamedev.Tile.Dungeon.TilePlatform;
|
||||
import edu.utc.gamedev.Util.Helper;
|
||||
import org.lwjgl.opengl.Display;
|
||||
import org.lwjgl.opengl.GL11;
|
||||
import org.lwjgl.util.Rectangle;
|
||||
import org.newdawn.slick.opengl.Texture;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
import static edu.utc.gamedev.Entity.Living.EntityMonster.State.HIT;
|
||||
|
||||
public class EntityMonster extends EntityLiving
|
||||
{
|
||||
private final float GRAVITY = 3F;
|
||||
private State state;
|
||||
private Rectangle box;
|
||||
private Texture sprite;
|
||||
private boolean flip;
|
||||
private long lastDamagedTime;
|
||||
private Type variant;
|
||||
private float health;
|
||||
private float wr, hr;
|
||||
private float motionMultiplier;
|
||||
private StartingSide start;
|
||||
private State lastState;
|
||||
private SceneLevel scene = SceneManager.getInstance().getScene();
|
||||
|
||||
public EntityMonster(Type variant)
|
||||
{
|
||||
this.sprite = Helper.loadTexture("entity/" + variant.getResourceName());
|
||||
Random rand = scene.rand();
|
||||
int xSpawn;
|
||||
if (rand.nextInt(2) == 0)
|
||||
{
|
||||
start = StartingSide.RIGHT;
|
||||
xSpawn = (Display.getWidth() + rand.nextInt(256));
|
||||
}
|
||||
else
|
||||
{
|
||||
start = StartingSide.LEFT;
|
||||
xSpawn = (0 - rand.nextInt(256));
|
||||
}
|
||||
this.box = new Rectangle(xSpawn, (Display.getHeight() - (sprite.getImageHeight())), (int) variant.getWidth(), (int) (variant.getWidth() * sprite.getImageHeight() / sprite.getImageWidth()));
|
||||
this.flip = (xSpawn <= 0);
|
||||
this.wr = 1.0f * sprite.getImageWidth() / sprite.getTextureWidth();
|
||||
this.hr = 1.0f * sprite.getImageHeight() / sprite.getTextureHeight();
|
||||
this.state = State.RIGHT;
|
||||
this.variant = variant;
|
||||
this.health = variant.getStartingHealth();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(float delta)
|
||||
{
|
||||
int motion = (int) (variant.getSpeed() * delta);
|
||||
int verticalMotion = (int) (motion * motionMultiplier);
|
||||
int horizontalMotion = (int) (verticalMotion * 1.25);
|
||||
// Handle directional punching properly
|
||||
if (SceneManager.getInstance().getScene().getPlayer() != null && SceneManager.getInstance().getScene().getPlayer().getHitBox().getX() > box.getX())
|
||||
{
|
||||
horizontalMotion *= -1;
|
||||
}
|
||||
box.setY(variant == Type.BAT ? Display.getHeight() - (box.getHeight() * 5) : Display.getHeight() - box.getHeight());
|
||||
|
||||
switch (state)
|
||||
{
|
||||
case IDLE:
|
||||
|
||||
if (start == StartingSide.RIGHT)
|
||||
{
|
||||
state = State.RIGHT;
|
||||
}
|
||||
else
|
||||
{
|
||||
state = State.LEFT;
|
||||
}
|
||||
break;
|
||||
case RIGHT:
|
||||
box.translate(motion, 0);
|
||||
if (box.getX() > (Display.getWidth() - box.getWidth()))
|
||||
{
|
||||
flip = false;
|
||||
state = State.LEFT;
|
||||
}
|
||||
break;
|
||||
case LEFT:
|
||||
box.translate(-motion, 0);
|
||||
if (box.getX() <= 0)
|
||||
{
|
||||
state = State.RIGHT;
|
||||
flip = true;
|
||||
}
|
||||
break;
|
||||
case HIT:
|
||||
// Make large creatures sink into the ground when hit, instead of fly into the air
|
||||
if (this.variant.width > 64F)
|
||||
{
|
||||
box.translate(horizontalMotion, verticalMotion);
|
||||
}
|
||||
else
|
||||
{
|
||||
box.translate(horizontalMotion, -verticalMotion);
|
||||
}
|
||||
// Normalize locations to prevent glitchy monsters
|
||||
if (box.getX() < 0)
|
||||
{
|
||||
box.setX(0);
|
||||
}
|
||||
if (box.getX() + box.getWidth() > Display.getWidth())
|
||||
{
|
||||
box.setX(Display.getWidth() - box.getWidth());
|
||||
}
|
||||
// Apply Motion!
|
||||
if (verticalMotion <= 0)
|
||||
{
|
||||
state = State.FALLING;
|
||||
}
|
||||
else
|
||||
{
|
||||
motionMultiplier -= 0.25F;
|
||||
}
|
||||
break;
|
||||
case FALLING:
|
||||
box.translate(horizontalMotion, verticalMotion);
|
||||
// Normalize locations to prevent glitchy monsters
|
||||
if (box.getX() < 0)
|
||||
{
|
||||
box.setX(0);
|
||||
}
|
||||
if (box.getX() + box.getWidth() > Display.getWidth())
|
||||
{
|
||||
box.setX(Display.getWidth() - box.getWidth());
|
||||
}
|
||||
// Apply Motion!
|
||||
if (getEntityPlatform() != null)
|
||||
{
|
||||
// Fixes the player's feet being past the platform
|
||||
box.setY(getEntityPlatform().getHitBox().getY() - (box.getHeight()));
|
||||
state = lastState;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (motionMultiplier < GRAVITY)
|
||||
{
|
||||
motionMultiplier += 0.25F;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
if (scene.getPlayer() != null && box.intersects(scene.getPlayer().getHitBox()))
|
||||
{
|
||||
if (scene.getPlayer().getState() != EntityPlayer.State.DEAD && scene.getPlayer().getState() != EntityPlayer.State.STUNNED)
|
||||
{
|
||||
scene.getPlayer().damage(variant.getDamageToPlayer());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public TilePlatform getEntityPlatform()
|
||||
{
|
||||
if (SceneManager.getInstance().getScene() instanceof SceneLevel)
|
||||
{
|
||||
for (TilePlatform t : SceneManager.getInstance().getScene().getTileManager().getPlatforms())
|
||||
{
|
||||
if (t.entityStanding(this))
|
||||
{
|
||||
return t;
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public long getLastDamagedTime()
|
||||
{
|
||||
return lastDamagedTime;
|
||||
}
|
||||
|
||||
public void setInvulnerableTime()
|
||||
{
|
||||
lastDamagedTime = Helper.getTime();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void draw()
|
||||
{
|
||||
float x = (float) box.getX();
|
||||
float y = (float) box.getY();
|
||||
float w = (float) box.getWidth();
|
||||
float h = (float) box.getHeight();
|
||||
|
||||
GL11.glBindTexture(GL11.GL_TEXTURE_2D, sprite.getTextureID());
|
||||
GL11.glColor3f(1F, 1F, 1F);
|
||||
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST);
|
||||
GL11.glBegin(GL11.GL_QUADS);
|
||||
|
||||
if (state == HIT)
|
||||
{
|
||||
GL11.glColor3f(180 / 255F, 0F, 3 / 255F);
|
||||
}
|
||||
if (flip)
|
||||
{
|
||||
GL11.glTexCoord2f(0F, 0F);
|
||||
GL11.glVertex2f(x + w, y);
|
||||
|
||||
GL11.glTexCoord2f(wr, 0F);
|
||||
GL11.glVertex2f(x, y);
|
||||
|
||||
GL11.glTexCoord2f(wr, hr);
|
||||
GL11.glVertex2f(x, y + h);
|
||||
|
||||
GL11.glTexCoord2f(0F, hr);
|
||||
GL11.glVertex2f(x + w, y + h);
|
||||
}
|
||||
else
|
||||
{
|
||||
GL11.glTexCoord2f(0F, 0F);
|
||||
GL11.glVertex2f(x, y);
|
||||
|
||||
GL11.glTexCoord2f(wr, 0F);
|
||||
GL11.glVertex2f(x + w, y);
|
||||
|
||||
GL11.glTexCoord2f(wr, hr);
|
||||
GL11.glVertex2f(x + w, y + h);
|
||||
|
||||
GL11.glTexCoord2f(0F, hr);
|
||||
GL11.glVertex2f(x, y + h);
|
||||
}
|
||||
|
||||
GL11.glEnd();
|
||||
GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getMaxHealth()
|
||||
{
|
||||
return variant.startingHealth;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Rectangle getHitBox()
|
||||
{
|
||||
return box;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void kill()
|
||||
{
|
||||
// Monsters have a 25% chance of dropping a heart only if the player needs it
|
||||
if (scene.rand().nextInt(4) == 0 && scene.getPlayer().getHealth() <= 10F)
|
||||
{
|
||||
scene.getEntityManager().addEntity(new EntityHeart(box.getX() + (box.getWidth() / 2), box.getY()));
|
||||
}
|
||||
|
||||
scene.getEntityManager().removeEntity(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void damage(float amount)
|
||||
{
|
||||
// Don't allow entities to be damaged multiple times
|
||||
if (state == State.HIT || state == State.FALLING)
|
||||
{
|
||||
return;
|
||||
}
|
||||
health -= amount;
|
||||
if (health > 0F)
|
||||
{
|
||||
AudioManager.getInstance().play("damage_hit" + (scene.rand().nextInt(10) + 1));
|
||||
lastState = state;
|
||||
motionMultiplier = GRAVITY;
|
||||
state = HIT;
|
||||
}
|
||||
else
|
||||
{
|
||||
kill();
|
||||
AudioManager.getInstance().play(variant.getResourceName() + "_death");
|
||||
}
|
||||
}
|
||||
|
||||
public float getHealth()
|
||||
{
|
||||
return health;
|
||||
}
|
||||
|
||||
public State getState()
|
||||
{
|
||||
return state;
|
||||
}
|
||||
|
||||
public void setState(State s)
|
||||
{
|
||||
state = s;
|
||||
}
|
||||
|
||||
public enum State
|
||||
{
|
||||
IDLE, LEFT, RIGHT, HIT, FALLING
|
||||
}
|
||||
|
||||
public enum StartingSide
|
||||
{
|
||||
LEFT, RIGHT
|
||||
}
|
||||
|
||||
public enum Type
|
||||
{
|
||||
ORC(0.25F, 20F, 1F, 92F, "orc"),
|
||||
GUT_EATER(0.15F, 40F, 1.5F, 192F, "gut_eater"),
|
||||
BAT(0.75F, 1F, 0.25F, 48F, "bat"),
|
||||
ZOMBIE(0.15F, 18F, 0.75F, 64F, "zombie"),
|
||||
FLAME(1F, 2F, 0.5F, 30F, "flame"),
|
||||
ROCK_MONSTER(0.2F, 20F, 0.4F, 60F, "rock_monster"),
|
||||
ICE_MONSTER(0.65F, 10F, 0.5F, 48F, "ice_monster"),
|
||||
DARK_MAGE(0.5F, 14F, 0.75F, 64F, "dark_mage"),
|
||||
OOZER(0.15F, 32F, 0.8F, 40F, "oozer");
|
||||
|
||||
private final float speed;
|
||||
private final float startingHealth;
|
||||
private final float damageToPlayer;
|
||||
private final float width;
|
||||
private final String resourceName;
|
||||
|
||||
Type(float speed, float health, float damage, float width, String resourceName)
|
||||
{
|
||||
this.speed = speed;
|
||||
this.startingHealth = health;
|
||||
this.damageToPlayer = damage;
|
||||
this.resourceName = resourceName;
|
||||
this.width = width;
|
||||
}
|
||||
|
||||
public float getSpeed()
|
||||
{
|
||||
return speed;
|
||||
}
|
||||
|
||||
public float getStartingHealth()
|
||||
{
|
||||
return startingHealth;
|
||||
}
|
||||
|
||||
public float getDamageToPlayer()
|
||||
{
|
||||
return damageToPlayer;
|
||||
}
|
||||
|
||||
public String getResourceName()
|
||||
{
|
||||
return resourceName;
|
||||
}
|
||||
|
||||
public float getWidth()
|
||||
{
|
||||
return width;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
311
src/edu/utc/gamedev/Entity/Living/EntityPlayer.java
Normal file
311
src/edu/utc/gamedev/Entity/Living/EntityPlayer.java
Normal file
|
|
@ -0,0 +1,311 @@
|
|||
package edu.utc.gamedev.Entity.Living;
|
||||
|
||||
import edu.utc.gamedev.Item.Item;
|
||||
import edu.utc.gamedev.Item.ItemFist;
|
||||
import edu.utc.gamedev.Manager.AudioManager;
|
||||
import edu.utc.gamedev.Manager.SceneManager;
|
||||
import edu.utc.gamedev.Scene.SceneLevel;
|
||||
import edu.utc.gamedev.Tile.Dungeon.TilePlatform;
|
||||
import edu.utc.gamedev.Util.Helper;
|
||||
import org.lwjgl.LWJGLException;
|
||||
import org.lwjgl.input.Keyboard;
|
||||
import org.lwjgl.input.Mouse;
|
||||
import org.lwjgl.opengl.Display;
|
||||
import org.lwjgl.opengl.GL11;
|
||||
import org.lwjgl.util.Rectangle;
|
||||
import org.newdawn.slick.opengl.Texture;
|
||||
|
||||
public class EntityPlayer extends EntityLiving
|
||||
{
|
||||
private final float GRAVITY = 3.5F;
|
||||
private Rectangle box;
|
||||
private Texture sprite;
|
||||
private float speed;
|
||||
private State state;
|
||||
private boolean flip;
|
||||
private long stunTime;
|
||||
private float health;
|
||||
private float maxHealth;
|
||||
private float motionMultiplier;
|
||||
private float wr, hr;
|
||||
private Item heldItem;
|
||||
|
||||
public EntityPlayer(int x, int y)
|
||||
{
|
||||
sprite = Helper.loadTexture("entity/player");
|
||||
box = new Rectangle(x, y, 32, (32 * sprite.getImageHeight() / sprite.getImageWidth()));
|
||||
this.wr = 1.0f * sprite.getImageWidth() / sprite.getTextureWidth();
|
||||
this.hr = 1.0f * sprite.getImageHeight() / sprite.getTextureHeight();
|
||||
this.state = State.FALLING;
|
||||
this.flip = false;
|
||||
this.health = 20F;
|
||||
this.maxHealth = 20F;
|
||||
this.speed = .6F;
|
||||
this.heldItem = new ItemFist().setCursor();
|
||||
}
|
||||
|
||||
public EntityPlayer()
|
||||
{
|
||||
this(0, 0);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public float getMaxHealth()
|
||||
{
|
||||
return this.maxHealth;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(float delta)
|
||||
{
|
||||
int motion = (int) (this.speed * delta);
|
||||
int verticalMotion = (int) (motion * motionMultiplier);
|
||||
if (Keyboard.isKeyDown(Keyboard.KEY_A) && this.box.getX() > 0 && !(this.state == State.STUNNED))
|
||||
{
|
||||
this.flip = true;
|
||||
box.translate(-motion, 0);
|
||||
}
|
||||
if (Keyboard.isKeyDown(Keyboard.KEY_D) && this.box.getX() <= (Display.getWidth() - box.getWidth()) && !(this.state == State.STUNNED))
|
||||
{
|
||||
this.flip = false;
|
||||
box.translate(motion, 0);
|
||||
}
|
||||
|
||||
// Fix the player from being off the screen at any time
|
||||
if (this.box.getX() < 0 || this.box.getX() > Display.getWidth())
|
||||
{
|
||||
this.box.setX(0);
|
||||
}
|
||||
if (this.box.getY() < 0 || this.box.getY() > Display.getHeight())
|
||||
{
|
||||
this.box.setY(Display.getHeight() - this.box.getHeight());
|
||||
}
|
||||
|
||||
|
||||
switch (state)
|
||||
{
|
||||
case IDLE:
|
||||
motionMultiplier = 0.0F;
|
||||
if (Keyboard.isKeyDown(Keyboard.KEY_SPACE))
|
||||
{
|
||||
AudioManager.getInstance().play("player_jump");
|
||||
motionMultiplier = GRAVITY;
|
||||
state = State.JUMPING;
|
||||
}
|
||||
// Makes our player fall off of platforms
|
||||
else if (getPlayerPlatform() == null)
|
||||
{
|
||||
state = State.FALLING;
|
||||
}
|
||||
break;
|
||||
case JUMPING:
|
||||
box.translate(0, -verticalMotion);
|
||||
if (verticalMotion <= 0)
|
||||
{
|
||||
state = State.FALLING;
|
||||
}
|
||||
else
|
||||
{
|
||||
motionMultiplier -= 0.25F;
|
||||
}
|
||||
break;
|
||||
case FALLING:
|
||||
box.translate(0, verticalMotion);
|
||||
if (getPlayerPlatform() != null)
|
||||
{
|
||||
// Fixes the player's feet being past the platform
|
||||
box.setY(getPlayerPlatform().getHitBox().getY() - (box.getHeight()));
|
||||
state = State.IDLE;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (motionMultiplier < GRAVITY)
|
||||
{
|
||||
motionMultiplier += 0.25F;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case STUNNED:
|
||||
if (Helper.getTime() - this.stunTime > (40 * delta))
|
||||
{
|
||||
if (getPlayerPlatform() == null)
|
||||
{
|
||||
this.state = State.FALLING;
|
||||
}
|
||||
else
|
||||
{
|
||||
this.state = State.IDLE;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case DEAD:
|
||||
kill();
|
||||
break;
|
||||
}
|
||||
if (!Mouse.isCreated() || this.heldItem == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
while (Mouse.next())
|
||||
{
|
||||
|
||||
if ((!this.heldItem.limitUseDistance() && Mouse.getEventButtonState()))
|
||||
{
|
||||
this.heldItem.onUse();
|
||||
}
|
||||
if (Mouse.getEventButtonState() && Math.abs(this.box.getX() - Mouse.getX()) < 384)
|
||||
{
|
||||
if (Math.abs(this.box.getY() - (Display.getHeight() - Mouse.getY())) < 192)
|
||||
{
|
||||
this.heldItem.onUse();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void draw()
|
||||
{
|
||||
float x = (float) box.getX();
|
||||
float y = (float) box.getY();
|
||||
float w = (float) box.getWidth();
|
||||
float h = (float) box.getHeight();
|
||||
|
||||
|
||||
GL11.glBindTexture(GL11.GL_TEXTURE_2D, sprite.getTextureID());
|
||||
GL11.glColor3f(255F, 255F, 255F);
|
||||
GL11.glBegin(GL11.GL_QUADS);
|
||||
|
||||
// So that the player renders red when hurt:
|
||||
if (this.state == State.STUNNED)
|
||||
{
|
||||
GL11.glColor3f(.8F, 0F, 0F);
|
||||
}
|
||||
if (this.flip)
|
||||
{
|
||||
GL11.glTexCoord2f(0F, 0F);
|
||||
GL11.glVertex2f(x + w, y);
|
||||
|
||||
GL11.glTexCoord2f(wr, 0F);
|
||||
GL11.glVertex2f(x, y);
|
||||
|
||||
GL11.glTexCoord2f(wr, hr);
|
||||
GL11.glVertex2f(x, y + h);
|
||||
|
||||
GL11.glTexCoord2f(0F, hr);
|
||||
GL11.glVertex2f(x + w, y + h);
|
||||
}
|
||||
else
|
||||
{
|
||||
GL11.glTexCoord2f(0F, 0F);
|
||||
GL11.glVertex2f(x, y);
|
||||
|
||||
GL11.glTexCoord2f(wr, 0F);
|
||||
GL11.glVertex2f(x + w, y);
|
||||
|
||||
GL11.glTexCoord2f(wr, hr);
|
||||
GL11.glVertex2f(x + w, y + h);
|
||||
|
||||
GL11.glTexCoord2f(0F, hr);
|
||||
GL11.glVertex2f(x, y + h);
|
||||
}
|
||||
|
||||
GL11.glEnd();
|
||||
GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void kill()
|
||||
{
|
||||
if (SceneManager.getInstance().getScene() instanceof SceneLevel)
|
||||
{
|
||||
((SceneLevel) SceneManager.getInstance().getScene()).setPlayer(null);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Rectangle getHitBox()
|
||||
{
|
||||
return this.box;
|
||||
}
|
||||
|
||||
public State getState()
|
||||
{
|
||||
return this.state;
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getHealth()
|
||||
{
|
||||
return this.health;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void damage(float amount)
|
||||
{
|
||||
this.health -= amount;
|
||||
|
||||
// Normalize health; player should have no more than 20 health
|
||||
if (this.health > maxHealth)
|
||||
this.health = maxHealth;
|
||||
|
||||
// Do nothing else if we're just "healing"
|
||||
if (amount <= 0)
|
||||
return;
|
||||
|
||||
if (this.health > 0F)
|
||||
{
|
||||
this.stunTime = Helper.getTime();
|
||||
this.state = State.STUNNED;
|
||||
AudioManager.getInstance().play("player_hit" + (SceneManager.getInstance().getScene().rand().nextInt(2) + 1));
|
||||
}
|
||||
else
|
||||
{
|
||||
this.state = State.DEAD;
|
||||
AudioManager.getInstance().play("player_death");
|
||||
}
|
||||
}
|
||||
|
||||
public Item getHeldItem()
|
||||
{
|
||||
return this.heldItem;
|
||||
}
|
||||
|
||||
public void setHeldItem(Item item)
|
||||
{
|
||||
if (item == null)
|
||||
{
|
||||
this.heldItem = null;
|
||||
try
|
||||
{
|
||||
Mouse.setNativeCursor(null);
|
||||
}
|
||||
catch (LWJGLException lwjglEx)
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
this.heldItem = item;
|
||||
}
|
||||
|
||||
public TilePlatform getPlayerPlatform()
|
||||
{
|
||||
if (SceneManager.getInstance().getScene() instanceof SceneLevel)
|
||||
{
|
||||
for (TilePlatform t : SceneManager.getInstance().getScene().getTileManager().getPlatforms())
|
||||
{
|
||||
if (t.entityStanding(this))
|
||||
{
|
||||
return t;
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public enum State
|
||||
{
|
||||
IDLE, JUMPING, FALLING, STUNNED, DEAD
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,117 @@
|
|||
package edu.utc.gamedev.Entity.Projectile;
|
||||
|
||||
import edu.utc.gamedev.Entity.Living.EntityMonster;
|
||||
import edu.utc.gamedev.Entity.Entity;
|
||||
import edu.utc.gamedev.Manager.SceneManager;
|
||||
import edu.utc.gamedev.Scene.SceneLevel;
|
||||
import edu.utc.gamedev.Util.Helper;
|
||||
import org.lwjgl.opengl.Display;
|
||||
import org.lwjgl.opengl.GL11;
|
||||
import org.lwjgl.util.Rectangle;
|
||||
import org.lwjgl.util.vector.Vector2f;
|
||||
import org.newdawn.slick.opengl.Texture;
|
||||
|
||||
public class EntityPotatoProjectile extends Entity
|
||||
{
|
||||
private Vector2f vel;
|
||||
private Rectangle box;
|
||||
private float speed = 4f;
|
||||
private boolean flip;
|
||||
private float wr, hr;
|
||||
private Texture sprite;
|
||||
private SceneLevel scene = SceneManager.getInstance().getScene();
|
||||
|
||||
public EntityPotatoProjectile(int x, int y, Vector2f vel, boolean flip)
|
||||
{
|
||||
sprite = Helper.loadTexture("entity/projectile/potato");
|
||||
this.box = new Rectangle(x, y, 32, (32 * sprite.getImageHeight() / sprite.getImageWidth()));
|
||||
this.wr = 1.0f * sprite.getImageWidth() / sprite.getTextureWidth();
|
||||
this.hr = 1.0f * sprite.getImageHeight() / sprite.getTextureHeight();
|
||||
this.vel = new Vector2f();
|
||||
vel.normalise(this.vel);
|
||||
this.flip = flip;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(float delta)
|
||||
{
|
||||
// Handle off-screen projectiles
|
||||
if (speed <= 0 || box.getX() < 0 || box.getX() > Display.getWidth() || box.getY() < 0 || box.getY() > Display.getHeight())
|
||||
{
|
||||
scene.getProjectileManager().removeProjectile(this);
|
||||
return;
|
||||
}
|
||||
if (speed > 0)
|
||||
{
|
||||
box.translate((int) (delta * speed * vel.getX()), (int) ((delta * speed * vel.getY())));
|
||||
Vector2f.add(vel, new Vector2f(0, .1f), vel);
|
||||
}
|
||||
// Collision for potatoes
|
||||
for (Entity e : scene.getEntityManager().getEntities())
|
||||
{
|
||||
if (!(e instanceof EntityMonster))
|
||||
continue;
|
||||
|
||||
if (this.box.intersects(e.getHitBox()))
|
||||
{
|
||||
EntityMonster m = (EntityMonster) e;
|
||||
m.damage(10F);
|
||||
m.setState(EntityMonster.State.HIT);
|
||||
scene.getProjectileManager().removeProjectile(this);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void draw()
|
||||
{
|
||||
float x = (float) box.getX();
|
||||
float y = (float) box.getY();
|
||||
float w = (float) box.getWidth();
|
||||
float h = (float) box.getHeight();
|
||||
|
||||
GL11.glBindTexture(GL11.GL_TEXTURE_2D, sprite.getTextureID());
|
||||
GL11.glColor3f(1F, 1F, 1F);
|
||||
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST);
|
||||
GL11.glBegin(GL11.GL_QUADS);
|
||||
|
||||
if (this.flip)
|
||||
{
|
||||
GL11.glTexCoord2f(0F, 0F);
|
||||
GL11.glVertex2f(x + w, y);
|
||||
|
||||
GL11.glTexCoord2f(wr, 0F);
|
||||
GL11.glVertex2f(x, y);
|
||||
|
||||
GL11.glTexCoord2f(wr, hr);
|
||||
GL11.glVertex2f(x, y + h);
|
||||
|
||||
GL11.glTexCoord2f(0F, hr);
|
||||
GL11.glVertex2f(x + w, y + h);
|
||||
}
|
||||
else
|
||||
{
|
||||
GL11.glTexCoord2f(0F, 0F);
|
||||
GL11.glVertex2f(x, y);
|
||||
|
||||
GL11.glTexCoord2f(wr, 0F);
|
||||
GL11.glVertex2f(x + w, y);
|
||||
|
||||
GL11.glTexCoord2f(wr, hr);
|
||||
GL11.glVertex2f(x + w, y + h);
|
||||
|
||||
GL11.glTexCoord2f(0F, hr);
|
||||
GL11.glVertex2f(x, y + h);
|
||||
}
|
||||
|
||||
GL11.glEnd();
|
||||
GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Rectangle getHitBox()
|
||||
{
|
||||
return this.box;
|
||||
}
|
||||
}
|
||||
240
src/edu/utc/gamedev/Game.java
Normal file
240
src/edu/utc/gamedev/Game.java
Normal file
|
|
@ -0,0 +1,240 @@
|
|||
package edu.utc.gamedev;
|
||||
|
||||
import edu.utc.gamedev.Entity.Living.EntityMonster;
|
||||
import edu.utc.gamedev.Manager.AudioManager;
|
||||
import edu.utc.gamedev.Manager.SceneManager;
|
||||
import edu.utc.gamedev.Manager.ScreenshotManager;
|
||||
import edu.utc.gamedev.Scene.Dungeon;
|
||||
import edu.utc.gamedev.Scene.Menu;
|
||||
import edu.utc.gamedev.Scene.Scene;
|
||||
import edu.utc.gamedev.Util.Config;
|
||||
import org.lwjgl.LWJGLException;
|
||||
import org.lwjgl.opengl.Display;
|
||||
import org.lwjgl.opengl.DisplayMode;
|
||||
import org.lwjgl.opengl.GL11;
|
||||
import org.newdawn.slick.opengl.PNGDecoder;
|
||||
import org.newdawn.slick.util.ResourceLoader;
|
||||
|
||||
import java.awt.*;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.URL;
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
public class Game
|
||||
{
|
||||
private static Game instance;
|
||||
public Config config;
|
||||
public ScreenshotManager screenshotManager;
|
||||
public SceneManager sceneManager;
|
||||
private boolean fullscreen;
|
||||
|
||||
public Game()
|
||||
{
|
||||
instance = this;
|
||||
this.config = new Config();
|
||||
this.screenshotManager = new ScreenshotManager();
|
||||
this.fullscreen = false;
|
||||
this.sceneManager = new SceneManager();
|
||||
initGL();
|
||||
initAudio();
|
||||
initGame();
|
||||
}
|
||||
|
||||
public static Game getInstance()
|
||||
{
|
||||
return instance;
|
||||
}
|
||||
|
||||
public void initGame()
|
||||
{
|
||||
Menu gameMenu = new Menu();
|
||||
gameMenu.addItem("Play", new Dungeon());
|
||||
gameMenu.addSpecial("Exit", Menu.DO_EXIT);
|
||||
|
||||
Scene currScene = gameMenu;
|
||||
|
||||
while (currScene.go())
|
||||
{
|
||||
currScene = currScene.nextScene();
|
||||
|
||||
if (currScene == null)
|
||||
{
|
||||
currScene = gameMenu;
|
||||
}
|
||||
}
|
||||
|
||||
AudioManager.getInstance().destroy();
|
||||
Display.destroy();
|
||||
}
|
||||
|
||||
public void initGL()
|
||||
{
|
||||
setDisplayMode(this.config.resolutionWidth, this.config.resolutionHeight, fullscreen);
|
||||
Display.setTitle("Crawl 'n Brawl");
|
||||
// Centers the window
|
||||
Display.setLocation((Toolkit.getDefaultToolkit().getScreenSize().width / 2) - (this.config.resolutionWidth / 2),
|
||||
(Toolkit.getDefaultToolkit().getScreenSize().height / 2) - (this.config.resolutionHeight / 2));
|
||||
|
||||
initIcon();
|
||||
|
||||
try
|
||||
{
|
||||
Display.create();
|
||||
}
|
||||
catch (LWJGLException e)
|
||||
{
|
||||
System.out.println("Caught an LWJGL exception when creating the display. Quitting..");
|
||||
System.out.println(e.getMessage());
|
||||
System.exit(0);
|
||||
}
|
||||
|
||||
GL11.glEnable(GL11.GL_TEXTURE_2D);
|
||||
GL11.glClearColor(0F, 0F, 0F, 0.0f);
|
||||
GL11.glEnable(GL11.GL_BLEND);
|
||||
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
|
||||
GL11.glViewport(0, 0, this.config.resolutionWidth, this.config.resolutionHeight);
|
||||
GL11.glMatrixMode(GL11.GL_PROJECTION);
|
||||
GL11.glLoadIdentity();
|
||||
GL11.glOrtho(0, this.config.resolutionWidth, this.config.resolutionHeight, 0, 1, -1);
|
||||
GL11.glMatrixMode(GL11.GL_MODELVIEW);
|
||||
}
|
||||
|
||||
/**
|
||||
* SOURCE: http://wiki.lwjgl.org/wiki/LWJGL_Basics_5_(Fullscreen).html
|
||||
*
|
||||
* @param width The width of the display required
|
||||
* @param height The height of the display required
|
||||
* @param fullscreen True if we want fullscreen mode
|
||||
*/
|
||||
public void setDisplayMode(int width, int height, boolean fullscreen)
|
||||
{
|
||||
if ((Display.getDisplayMode().getWidth() == width) && (Display.getDisplayMode().getHeight() == height) && (Display.isFullscreen() == fullscreen))
|
||||
return;
|
||||
|
||||
try
|
||||
{
|
||||
org.lwjgl.opengl.DisplayMode targetDisplayMode = null;
|
||||
|
||||
if (fullscreen)
|
||||
{
|
||||
org.lwjgl.opengl.DisplayMode[] modes = Display.getAvailableDisplayModes();
|
||||
int freq = 0;
|
||||
|
||||
for (int i = 0; i < modes.length; i++)
|
||||
{
|
||||
org.lwjgl.opengl.DisplayMode current = modes[i];
|
||||
|
||||
if ((current.getWidth() == width) && (current.getHeight() == height))
|
||||
{
|
||||
if ((targetDisplayMode == null) || (current.getFrequency() >= freq))
|
||||
{
|
||||
if ((targetDisplayMode == null) || (current.getBitsPerPixel() > targetDisplayMode.getBitsPerPixel()))
|
||||
{
|
||||
targetDisplayMode = current;
|
||||
freq = targetDisplayMode.getFrequency();
|
||||
}
|
||||
}
|
||||
|
||||
if ((current.getBitsPerPixel() == Display.getDesktopDisplayMode().getBitsPerPixel()) && (current.getFrequency() == Display.getDesktopDisplayMode().getFrequency()))
|
||||
{
|
||||
targetDisplayMode = current;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
targetDisplayMode = new DisplayMode(width, height);
|
||||
}
|
||||
|
||||
if (targetDisplayMode == null)
|
||||
{
|
||||
System.out.println("Failed to find value mode: " + width + "x" + height + " fs=" + fullscreen);
|
||||
return;
|
||||
}
|
||||
|
||||
Display.setDisplayMode(targetDisplayMode);
|
||||
Display.setFullscreen(fullscreen);
|
||||
// Stupid LWJGL stuff. Thanks Ellpeck!
|
||||
Display.setResizable(false);
|
||||
Display.setResizable(true);
|
||||
Display.setVSyncEnabled(true);
|
||||
}
|
||||
catch (LWJGLException e)
|
||||
{
|
||||
System.err.println("Unable to setup mode " + width + "x" + height + " fullscreen=" + fullscreen + e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* SOURCE: http://forum.lwjgl.org/index.php?topic=3422.0
|
||||
*
|
||||
* @return The ByteBuffer of the PNG located at the passed URL
|
||||
*/
|
||||
private ByteBuffer loadIcon(URL url) throws IOException
|
||||
{
|
||||
System.out.println("Loaded icon " + url.getPath().replace("/C:/", "C:/"));
|
||||
InputStream is = url.openStream();
|
||||
try
|
||||
{
|
||||
PNGDecoder decoder = new PNGDecoder(is);
|
||||
ByteBuffer buffer = ByteBuffer.allocateDirect(decoder.getWidth() * decoder.getHeight() * 4);
|
||||
decoder.decode(buffer, decoder.getWidth() * 4, PNGDecoder.RGBA);
|
||||
buffer.flip();
|
||||
return buffer;
|
||||
}
|
||||
finally
|
||||
{
|
||||
is.close();
|
||||
}
|
||||
}
|
||||
|
||||
public void initIcon()
|
||||
{
|
||||
try
|
||||
{
|
||||
Display.setIcon(new ByteBuffer[]{
|
||||
loadIcon(ResourceLoader.getResource("assets/icon/icon16.png".replace("/", File.separator))),
|
||||
loadIcon(ResourceLoader.getResource("assets/icon/icon32.png".replace("/", File.separator))),
|
||||
});
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
System.out.println("Icon file not found; using default.");
|
||||
Display.setIcon(new ByteBuffer[]{});
|
||||
}
|
||||
}
|
||||
|
||||
public void initAudio()
|
||||
{
|
||||
try
|
||||
{
|
||||
AudioManager.getInstance().loadSample("sword_slash", "assets/sounds/sword_slash.wav".replace("/", File.separator));
|
||||
AudioManager.getInstance().loadSample("player_jump", "assets/sounds/player_jump.wav".replace("/", File.separator));
|
||||
AudioManager.getInstance().loadSample("player_death", "assets/sounds/death/player_death.wav".replace("/", File.separator));
|
||||
AudioManager.getInstance().loadSample("level_up", "assets/sounds/environment/level_up.wav".replace("/", File.separator));
|
||||
for (int i = 1; i <= 2; i++)
|
||||
{
|
||||
AudioManager.getInstance().loadSample("player_hit" + i, "assets/sounds/damage/player_hit" + i + ".wav".replace("/", File.separator));
|
||||
}
|
||||
for (int i = 1; i <= 10; i++)
|
||||
{
|
||||
AudioManager.getInstance().loadSample("damage_hit" + i, "assets/sounds/damage/damage_hit" + i + ".wav".replace("/", File.separator));
|
||||
}
|
||||
for (int i = 0; i < EntityMonster.Type.values().length; i++)
|
||||
{
|
||||
AudioManager.getInstance().loadSample(EntityMonster.Type.values()[i].getResourceName() + "_death", "assets/sounds/death/".replace("/", File.separator) + EntityMonster.Type.values()[i].getResourceName() + "_death.wav");
|
||||
}
|
||||
AudioManager.getInstance().loadSample("menu_move", "assets/sounds/menu/move.wav".replace("/", File.separator));
|
||||
AudioManager.getInstance().loadSample("menu_select", "assets/sounds/menu/select.wav".replace("/", File.separator));
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
System.out.println("Audio file not found; some sounds may not work correctly");
|
||||
System.out.println(e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
113
src/edu/utc/gamedev/Item/Item.java
Normal file
113
src/edu/utc/gamedev/Item/Item.java
Normal file
|
|
@ -0,0 +1,113 @@
|
|||
package edu.utc.gamedev.Item;
|
||||
|
||||
import edu.utc.gamedev.Util.Helper;
|
||||
import org.lwjgl.BufferUtils;
|
||||
import org.lwjgl.LWJGLException;
|
||||
import org.lwjgl.input.Cursor;
|
||||
import org.lwjgl.input.Mouse;
|
||||
import org.newdawn.slick.util.ResourceLoader;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.IntBuffer;
|
||||
|
||||
public class Item
|
||||
{
|
||||
private final String iconString;
|
||||
public int textureWidth;
|
||||
public int textureHeight;
|
||||
|
||||
public Item(String resourceFile)
|
||||
{
|
||||
this.iconString = resourceFile;
|
||||
}
|
||||
|
||||
public int getCooldownTime()
|
||||
{
|
||||
return 40;
|
||||
}
|
||||
|
||||
public String getIconString()
|
||||
{
|
||||
return this.iconString;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @throws LWJGLException if the cursor cannot be created or changed
|
||||
* @throws IOException if the cursor file cannot be found
|
||||
* <p>
|
||||
* Changes the cursor to a little sword!
|
||||
* SOURCE: https://goo.gl/Boi1AP
|
||||
*/
|
||||
public Item setCursor()
|
||||
{
|
||||
|
||||
InputStream stream = ResourceLoader.getResourceAsStream("assets/textures/items/".replace("/", File.separator) + iconString + ".png");
|
||||
BufferedImage cursorImg;
|
||||
try
|
||||
{
|
||||
cursorImg = ImageIO.read(stream);
|
||||
Mouse.create();
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
try
|
||||
{
|
||||
cursorImg = ImageIO.read(ResourceLoader.getResourceAsStream(Helper.errTexture()));
|
||||
}
|
||||
catch (IOException whatTheFuck)
|
||||
{
|
||||
throw new RuntimeException(whatTheFuck);
|
||||
}
|
||||
}
|
||||
catch (LWJGLException e)
|
||||
{
|
||||
System.err.println("There was an issue creating the mouse");
|
||||
System.err.println(e.getMessage());
|
||||
return this;
|
||||
}
|
||||
|
||||
textureWidth = cursorImg.getWidth();
|
||||
textureHeight = cursorImg.getHeight();
|
||||
|
||||
int rgbData[] = new int[textureWidth * textureHeight];
|
||||
|
||||
for (int i = 0; i < rgbData.length; i++)
|
||||
{
|
||||
int x = i % textureWidth;
|
||||
int y = textureHeight - 1 - i / textureWidth; // this will also flip the image vertically
|
||||
|
||||
rgbData[i] = cursorImg.getRGB(x, y);
|
||||
}
|
||||
|
||||
IntBuffer buffer = BufferUtils.createIntBuffer(textureWidth * textureHeight);
|
||||
buffer.put(rgbData);
|
||||
buffer.rewind();
|
||||
|
||||
try
|
||||
{
|
||||
Cursor cursor = new Cursor(textureWidth, textureHeight, 2, textureHeight - 2, 1, buffer, null);
|
||||
Mouse.setNativeCursor(cursor);
|
||||
}
|
||||
catch (LWJGLException e)
|
||||
{
|
||||
System.err.println("There was an issue setting the custom mouse cursor");
|
||||
System.err.println(e.getMessage());
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
public void onUse()
|
||||
{
|
||||
}
|
||||
|
||||
public boolean limitUseDistance()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
49
src/edu/utc/gamedev/Item/ItemFist.java
Normal file
49
src/edu/utc/gamedev/Item/ItemFist.java
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
package edu.utc.gamedev.Item;
|
||||
|
||||
import edu.utc.gamedev.Entity.Living.EntityMonster;
|
||||
import edu.utc.gamedev.Entity.Entity;
|
||||
import edu.utc.gamedev.Entity.Living.EntityPlayer;
|
||||
import edu.utc.gamedev.Manager.SceneManager;
|
||||
import edu.utc.gamedev.Util.Helper;
|
||||
import org.lwjgl.input.Mouse;
|
||||
import org.lwjgl.opengl.Display;
|
||||
import org.lwjgl.util.Rectangle;
|
||||
|
||||
public class ItemFist extends Item
|
||||
{
|
||||
|
||||
public ItemFist()
|
||||
{
|
||||
super("fist");
|
||||
}
|
||||
|
||||
public int getCooldownTime()
|
||||
{
|
||||
return 1000;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onUse()
|
||||
{
|
||||
if (SceneManager.getInstance().getScene().getPlayer() == null || SceneManager.getInstance().getScene().getPlayer().getState() == EntityPlayer.State.STUNNED)
|
||||
return;
|
||||
|
||||
int mouseX = Mouse.getX();
|
||||
int mouseY = Display.getHeight() - Mouse.getY();
|
||||
|
||||
Rectangle attackRect = new Rectangle(mouseX, mouseY, textureWidth, textureHeight);
|
||||
|
||||
for (Entity e : SceneManager.getInstance().getScene().getEntityManager().getEntities())
|
||||
{
|
||||
if (e instanceof EntityMonster)
|
||||
{
|
||||
EntityMonster m = (EntityMonster) e;
|
||||
if (m.getHitBox().intersects(attackRect) && m.getState() != EntityMonster.State.HIT && Helper.getTime() - m.getLastDamagedTime() > getCooldownTime())
|
||||
{
|
||||
m.damage(2F);
|
||||
((EntityMonster) e).setState(EntityMonster.State.HIT);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
16
src/edu/utc/gamedev/Item/ItemHeart.java
Normal file
16
src/edu/utc/gamedev/Item/ItemHeart.java
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
package edu.utc.gamedev.Item;
|
||||
|
||||
public class ItemHeart extends Item
|
||||
{
|
||||
public ItemHeart()
|
||||
{
|
||||
super("heart");
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Item setCursor()
|
||||
{
|
||||
return this;
|
||||
}
|
||||
}
|
||||
53
src/edu/utc/gamedev/Item/ItemPotatoGun.java
Normal file
53
src/edu/utc/gamedev/Item/ItemPotatoGun.java
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
package edu.utc.gamedev.Item;
|
||||
|
||||
import edu.utc.gamedev.Entity.Living.EntityPlayer;
|
||||
import edu.utc.gamedev.Entity.Projectile.EntityPotatoProjectile;
|
||||
import edu.utc.gamedev.Manager.SceneManager;
|
||||
import edu.utc.gamedev.Util.Helper;
|
||||
import org.lwjgl.input.Mouse;
|
||||
import org.lwjgl.opengl.Display;
|
||||
import org.lwjgl.util.Rectangle;
|
||||
import org.lwjgl.util.vector.Vector2f;
|
||||
|
||||
public class ItemPotatoGun extends Item
|
||||
{
|
||||
private long lastShot;
|
||||
|
||||
public ItemPotatoGun()
|
||||
{
|
||||
super("potato_gun");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onUse()
|
||||
{
|
||||
if (SceneManager.getInstance().getScene().getPlayer().getState() == EntityPlayer.State.STUNNED)
|
||||
return;
|
||||
|
||||
if (Helper.getTime() - lastShot > getCooldownTime())
|
||||
{
|
||||
Rectangle r = SceneManager.getInstance().getScene().getPlayer().getHitBox();
|
||||
int mx = Mouse.getX();
|
||||
int my = Display.getHeight() - Mouse.getY();
|
||||
|
||||
int bx = r.getX() + (r.getWidth() / 2);
|
||||
int by = r.getY() + 8;
|
||||
|
||||
Vector2f vel = new Vector2f(mx - bx, my - by);
|
||||
SceneManager.getInstance().getScene().getProjectileManager().addProjectile(new EntityPotatoProjectile(bx, by, vel, (my < r.getY())));
|
||||
lastShot = Helper.getTime();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getCooldownTime()
|
||||
{
|
||||
return 240;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean limitUseDistance()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
9
src/edu/utc/gamedev/Item/Sword/ItemBroadSword.java
Normal file
9
src/edu/utc/gamedev/Item/Sword/ItemBroadSword.java
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
package edu.utc.gamedev.Item.Sword;
|
||||
|
||||
public class ItemBroadSword extends ItemSword
|
||||
{
|
||||
public ItemBroadSword()
|
||||
{
|
||||
super(6F, "broad_sword");
|
||||
}
|
||||
}
|
||||
9
src/edu/utc/gamedev/Item/Sword/ItemDagger.java
Normal file
9
src/edu/utc/gamedev/Item/Sword/ItemDagger.java
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
package edu.utc.gamedev.Item.Sword;
|
||||
|
||||
public class ItemDagger extends ItemSword
|
||||
{
|
||||
public ItemDagger()
|
||||
{
|
||||
super(2.5F, "dagger");
|
||||
}
|
||||
}
|
||||
9
src/edu/utc/gamedev/Item/Sword/ItemGreatSword.java
Normal file
9
src/edu/utc/gamedev/Item/Sword/ItemGreatSword.java
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
package edu.utc.gamedev.Item.Sword;
|
||||
|
||||
public class ItemGreatSword extends ItemSword
|
||||
{
|
||||
public ItemGreatSword()
|
||||
{
|
||||
super(8F, "great_sword");
|
||||
}
|
||||
}
|
||||
9
src/edu/utc/gamedev/Item/Sword/ItemRefinedSword.java
Normal file
9
src/edu/utc/gamedev/Item/Sword/ItemRefinedSword.java
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
package edu.utc.gamedev.Item.Sword;
|
||||
|
||||
public class ItemRefinedSword extends ItemSword
|
||||
{
|
||||
public ItemRefinedSword()
|
||||
{
|
||||
super(5F, "refined_sword");
|
||||
}
|
||||
}
|
||||
9
src/edu/utc/gamedev/Item/Sword/ItemRustySword.java
Normal file
9
src/edu/utc/gamedev/Item/Sword/ItemRustySword.java
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
package edu.utc.gamedev.Item.Sword;
|
||||
|
||||
public class ItemRustySword extends ItemSword
|
||||
{
|
||||
public ItemRustySword()
|
||||
{
|
||||
super(4F, "rusty_sword");
|
||||
}
|
||||
}
|
||||
54
src/edu/utc/gamedev/Item/Sword/ItemSword.java
Normal file
54
src/edu/utc/gamedev/Item/Sword/ItemSword.java
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
package edu.utc.gamedev.Item.Sword;
|
||||
|
||||
import edu.utc.gamedev.Entity.Living.EntityMonster;
|
||||
import edu.utc.gamedev.Entity.Entity;
|
||||
import edu.utc.gamedev.Entity.Living.EntityPlayer;
|
||||
import edu.utc.gamedev.Item.Item;
|
||||
import edu.utc.gamedev.Manager.AudioManager;
|
||||
import edu.utc.gamedev.Manager.SceneManager;
|
||||
import edu.utc.gamedev.Util.Helper;
|
||||
import org.lwjgl.input.Mouse;
|
||||
import org.lwjgl.opengl.Display;
|
||||
import org.lwjgl.util.Rectangle;
|
||||
|
||||
public abstract class ItemSword extends Item
|
||||
{
|
||||
private float damage;
|
||||
|
||||
public ItemSword(float damage, String resourceFile)
|
||||
{
|
||||
super(resourceFile);
|
||||
this.damage = damage;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onUse()
|
||||
{
|
||||
if (SceneManager.getInstance().getScene().getPlayer().getState() == EntityPlayer.State.STUNNED)
|
||||
return;
|
||||
int mouseX = Mouse.getX();
|
||||
int mouseY = Display.getHeight() - Mouse.getY();
|
||||
|
||||
Rectangle attackRect = new Rectangle(mouseX, mouseY, textureWidth, textureHeight);
|
||||
|
||||
for (Entity e : SceneManager.getInstance().getScene().getEntityManager().getEntities())
|
||||
{
|
||||
if (e instanceof EntityMonster)
|
||||
{
|
||||
EntityMonster m = (EntityMonster) e;
|
||||
if (m.getHitBox().intersects(attackRect) && m.getState() != EntityMonster.State.HIT && Helper.getTime() - m.getLastDamagedTime() > getCooldownTime())
|
||||
{
|
||||
AudioManager.getInstance().play("sword_slash");
|
||||
m.damage(damage);
|
||||
m.setState(EntityMonster.State.HIT);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getCooldownTime()
|
||||
{
|
||||
return 800;
|
||||
}
|
||||
}
|
||||
9
src/edu/utc/gamedev/Item/Sword/ItemWoodenSword.java
Normal file
9
src/edu/utc/gamedev/Item/Sword/ItemWoodenSword.java
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
package edu.utc.gamedev.Item.Sword;
|
||||
|
||||
public class ItemWoodenSword extends ItemSword
|
||||
{
|
||||
public ItemWoodenSword()
|
||||
{
|
||||
super(3F, "wooden_sword");
|
||||
}
|
||||
}
|
||||
174
src/edu/utc/gamedev/Manager/AudioManager.java
Normal file
174
src/edu/utc/gamedev/Manager/AudioManager.java
Normal file
|
|
@ -0,0 +1,174 @@
|
|||
package edu.utc.gamedev.Manager;
|
||||
|
||||
/**
|
||||
* @author Craig Tanis
|
||||
* <p>
|
||||
* Code obtained from https://github.com/ctanis/cpsc3520_fa17/blob/master/pongy/src/AudioManager.java
|
||||
*/
|
||||
|
||||
import edu.utc.gamedev.Game;
|
||||
import org.lwjgl.openal.AL;
|
||||
import org.newdawn.slick.openal.Audio;
|
||||
import org.newdawn.slick.openal.AudioLoader;
|
||||
import org.newdawn.slick.openal.SoundStore;
|
||||
import org.newdawn.slick.util.ResourceLoader;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
|
||||
|
||||
/**
|
||||
* A utility class for caching sounds from files and enabling the
|
||||
* straightforward triggering of those sounds (samples and loops)
|
||||
*/
|
||||
public class AudioManager
|
||||
{
|
||||
private static AudioManager instance;
|
||||
private HashMap<String, AudioWrapper> sounds;
|
||||
|
||||
private AudioManager()
|
||||
{
|
||||
sounds = new HashMap<String, AudioWrapper>();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the singleton AudioManager instance
|
||||
*/
|
||||
public static AudioManager getInstance()
|
||||
{
|
||||
if (instance == null) instance = new AudioManager();
|
||||
return instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* load a one-shot sample
|
||||
*
|
||||
* @param name identifier for loaded sound
|
||||
* @param path path to file
|
||||
*/
|
||||
public void loadSample(String name, String path) throws IOException
|
||||
{
|
||||
// extract file extension
|
||||
String mode = path.substring(path.lastIndexOf('.') + 1).toUpperCase();
|
||||
|
||||
Audio tmp = AudioLoader.getAudio(mode, ResourceLoader.getResourceAsStream(path));
|
||||
sounds.put(name, new AudioWrapper(AudioType.SAMPLE, tmp));
|
||||
}
|
||||
|
||||
/**
|
||||
* load a looping music track
|
||||
*
|
||||
* @param name identifier for loaded sound
|
||||
* @param path path to file
|
||||
*/
|
||||
public void loadLoop(String name, String path) throws IOException
|
||||
{
|
||||
// extract file extension
|
||||
String mode = path.substring(path.lastIndexOf('.') + 1).toUpperCase();
|
||||
|
||||
Audio tmp = AudioLoader.getStreamingAudio(mode, ResourceLoader.getResource(path));
|
||||
sounds.put(name, new AudioWrapper(AudioType.LOOP, tmp));
|
||||
}
|
||||
|
||||
/**
|
||||
* get sound associated with name identifier
|
||||
*
|
||||
* @param name the loaded sound identifier
|
||||
*/
|
||||
public AudioWrapper get(String name)
|
||||
{
|
||||
return sounds.get(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* play sound associated with name identifier
|
||||
*
|
||||
* @param name the loaded sound identifier
|
||||
*/
|
||||
public void play(String name)
|
||||
{
|
||||
sounds.get(name).play();
|
||||
}
|
||||
|
||||
/**
|
||||
* play sound associated with name identifier
|
||||
*
|
||||
* @param name the loaded sound identifier
|
||||
* @param vol volume of sound (0.0 to 1.0)
|
||||
*/
|
||||
public void play(String name, float vol)
|
||||
{
|
||||
sounds.get(name).play(vol);
|
||||
}
|
||||
|
||||
/**
|
||||
* call this once per frame
|
||||
*/
|
||||
public void update()
|
||||
{
|
||||
SoundStore.get().poll(0);
|
||||
}
|
||||
|
||||
/**
|
||||
* call this to clean up
|
||||
*/
|
||||
public void destroy()
|
||||
{
|
||||
AL.destroy();
|
||||
}
|
||||
|
||||
|
||||
private enum AudioType
|
||||
{
|
||||
LOOP, SAMPLE
|
||||
}
|
||||
|
||||
/**
|
||||
* A handle on a loaded audio object, to play the same sound repeatedly
|
||||
* without having to look it up
|
||||
*/
|
||||
public class AudioWrapper
|
||||
{
|
||||
private Audio sound;
|
||||
private AudioType type;
|
||||
|
||||
private AudioWrapper(AudioType t, Audio a)
|
||||
{
|
||||
type = t;
|
||||
sound = a;
|
||||
}
|
||||
|
||||
/**
|
||||
* play this sound at config's volume
|
||||
*/
|
||||
public void play()
|
||||
{
|
||||
play(Game.getInstance().config.volume);
|
||||
}
|
||||
|
||||
/**
|
||||
* Play this sound
|
||||
*
|
||||
* @param vol value between 0.0 and 1.0 to specify relative volume of sound
|
||||
* if volume is 0, sound is not played
|
||||
*/
|
||||
public void play(float vol)
|
||||
{
|
||||
// prevents the sound from being played if sound is "off"
|
||||
if (vol <= 0F)
|
||||
return;
|
||||
|
||||
switch (type)
|
||||
{
|
||||
case LOOP:
|
||||
sound.playAsMusic(1.0f, vol, true);
|
||||
break;
|
||||
|
||||
case SAMPLE:
|
||||
sound.playAsSoundEffect(1.0f, vol, false);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
72
src/edu/utc/gamedev/Manager/EntityManager.java
Normal file
72
src/edu/utc/gamedev/Manager/EntityManager.java
Normal file
|
|
@ -0,0 +1,72 @@
|
|||
package edu.utc.gamedev.Manager;
|
||||
|
||||
import edu.utc.gamedev.Entity.Entity;
|
||||
import edu.utc.gamedev.Entity.Living.EntityLiving;
|
||||
import edu.utc.gamedev.Entity.Living.EntityMonster;
|
||||
|
||||
import java.util.ConcurrentModificationException;
|
||||
import java.util.LinkedList;
|
||||
|
||||
public class EntityManager
|
||||
{
|
||||
private LinkedList<Entity> entities;
|
||||
|
||||
public EntityManager()
|
||||
{
|
||||
entities = new LinkedList<>();
|
||||
}
|
||||
|
||||
public void drawAll()
|
||||
{
|
||||
for (Entity e : entities)
|
||||
{
|
||||
e.draw();
|
||||
if (e instanceof EntityLiving)
|
||||
{
|
||||
((EntityLiving) e).drawHealth();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void updateAll(float delta)
|
||||
{
|
||||
try
|
||||
{
|
||||
for (Entity e : entities)
|
||||
{
|
||||
e.update(delta);
|
||||
}
|
||||
}
|
||||
catch (ConcurrentModificationException e)
|
||||
{
|
||||
// We do nothing because nothing can be done ¯\_(ツ)_/¯
|
||||
}
|
||||
}
|
||||
|
||||
public void addEntity(Entity e)
|
||||
{
|
||||
entities.add(e);
|
||||
}
|
||||
|
||||
public void removeEntity(Entity e)
|
||||
{
|
||||
entities.remove(e);
|
||||
}
|
||||
|
||||
public LinkedList<Entity> getEntities()
|
||||
{
|
||||
return this.entities;
|
||||
}
|
||||
|
||||
public boolean allMonstersDead()
|
||||
{
|
||||
for (Entity e : entities)
|
||||
{
|
||||
if (e instanceof EntityMonster)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
54
src/edu/utc/gamedev/Manager/ProjectileManager.java
Normal file
54
src/edu/utc/gamedev/Manager/ProjectileManager.java
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
package edu.utc.gamedev.Manager;
|
||||
|
||||
import edu.utc.gamedev.Entity.Projectile.EntityPotatoProjectile;
|
||||
|
||||
import java.util.ConcurrentModificationException;
|
||||
import java.util.LinkedList;
|
||||
|
||||
public class ProjectileManager
|
||||
{
|
||||
private LinkedList<EntityPotatoProjectile> projectiles;
|
||||
|
||||
public ProjectileManager()
|
||||
{
|
||||
projectiles = new LinkedList<>();
|
||||
}
|
||||
|
||||
public void drawAll()
|
||||
{
|
||||
for (EntityPotatoProjectile e : projectiles)
|
||||
{
|
||||
e.draw();
|
||||
}
|
||||
}
|
||||
|
||||
public void updateAll(float delta)
|
||||
{
|
||||
try
|
||||
{
|
||||
for (EntityPotatoProjectile e : projectiles)
|
||||
{
|
||||
e.update(delta);
|
||||
}
|
||||
}
|
||||
catch (ConcurrentModificationException e)
|
||||
{
|
||||
// We do nothing because nothing can be done ¯\_(ツ)_/¯
|
||||
}
|
||||
}
|
||||
|
||||
public void addProjectile(EntityPotatoProjectile e)
|
||||
{
|
||||
projectiles.add(e);
|
||||
}
|
||||
|
||||
public void removeProjectile(EntityPotatoProjectile e)
|
||||
{
|
||||
projectiles.remove(e);
|
||||
}
|
||||
|
||||
public LinkedList<EntityPotatoProjectile> getProjectiles()
|
||||
{
|
||||
return this.projectiles;
|
||||
}
|
||||
}
|
||||
32
src/edu/utc/gamedev/Manager/SceneManager.java
Normal file
32
src/edu/utc/gamedev/Manager/SceneManager.java
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
package edu.utc.gamedev.Manager;
|
||||
|
||||
|
||||
import edu.utc.gamedev.Scene.SceneLevel;
|
||||
|
||||
public class SceneManager
|
||||
{
|
||||
private static SceneManager instance;
|
||||
private SceneLevel currentSceneLevel;
|
||||
|
||||
public SceneManager()
|
||||
{
|
||||
this.currentSceneLevel = null;
|
||||
instance = this;
|
||||
}
|
||||
|
||||
public static SceneManager getInstance()
|
||||
{
|
||||
return instance;
|
||||
}
|
||||
|
||||
public SceneLevel getScene()
|
||||
{
|
||||
return this.currentSceneLevel;
|
||||
}
|
||||
|
||||
public void setScene(SceneLevel s)
|
||||
{
|
||||
this.currentSceneLevel = s;
|
||||
}
|
||||
|
||||
}
|
||||
81
src/edu/utc/gamedev/Manager/ScreenshotManager.java
Normal file
81
src/edu/utc/gamedev/Manager/ScreenshotManager.java
Normal file
|
|
@ -0,0 +1,81 @@
|
|||
package edu.utc.gamedev.Manager;
|
||||
|
||||
import org.lwjgl.BufferUtils;
|
||||
import org.lwjgl.opengl.Display;
|
||||
import org.lwjgl.opengl.GL11;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
|
||||
public class ScreenshotManager
|
||||
{
|
||||
private File screenshotsFolder;
|
||||
|
||||
public ScreenshotManager()
|
||||
{
|
||||
screenshotsFolder = new File(System.getProperty("user.dir") + File.separator + "screenshots");
|
||||
}
|
||||
|
||||
|
||||
public File getScreenshotsFolder()
|
||||
{
|
||||
return this.screenshotsFolder;
|
||||
}
|
||||
|
||||
public void setScreenshotsFolder(File newFolder)
|
||||
{
|
||||
this.screenshotsFolder = newFolder;
|
||||
}
|
||||
|
||||
/**
|
||||
* SOURCE: http://wiki.lwjgl.org/wiki/Taking_Screen_Shots.html
|
||||
*/
|
||||
public void takeScreenshot()
|
||||
{
|
||||
GL11.glReadBuffer(GL11.GL_FRONT);
|
||||
int width = Display.getDisplayMode().getWidth();
|
||||
int height = Display.getDisplayMode().getHeight();
|
||||
int bpp = 4;
|
||||
ByteBuffer buffer = BufferUtils.createByteBuffer(width * height * bpp);
|
||||
GL11.glReadPixels(0, 0, width, height, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, buffer);
|
||||
|
||||
if (!this.screenshotsFolder.exists())
|
||||
{
|
||||
this.screenshotsFolder.mkdir();
|
||||
}
|
||||
|
||||
String format = "png";
|
||||
long timestamp = System.currentTimeMillis();
|
||||
SimpleDateFormat dateFormat = new SimpleDateFormat("MMM-dd-yyyy HH mm ss");
|
||||
Date resultdate = new Date(timestamp);
|
||||
File file = new File(this.screenshotsFolder.getAbsolutePath() + File.separator + dateFormat.format(resultdate) + "." + format);
|
||||
System.out.println(file.getAbsolutePath());
|
||||
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
|
||||
|
||||
for (int x = 0; x < width; x++)
|
||||
{
|
||||
for (int y = 0; y < height; y++)
|
||||
{
|
||||
int i = (x + (width * y)) * bpp;
|
||||
int r = buffer.get(i) & 0xFF;
|
||||
int g = buffer.get(i + 1) & 0xFF;
|
||||
int b = buffer.get(i + 2) & 0xFF;
|
||||
image.setRGB(x, height - (y + 1), (0xFF << 24) | (r << 16) | (g << 8) | b);
|
||||
}
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
ImageIO.write(image, format, file);
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
System.out.println("Error capturing screenshot");
|
||||
}
|
||||
}
|
||||
}
|
||||
87
src/edu/utc/gamedev/Manager/TileManager.java
Normal file
87
src/edu/utc/gamedev/Manager/TileManager.java
Normal file
|
|
@ -0,0 +1,87 @@
|
|||
package edu.utc.gamedev.Manager;
|
||||
|
||||
import edu.utc.gamedev.Tile.ITile;
|
||||
import edu.utc.gamedev.Tile.Dungeon.TileDecoration;
|
||||
import edu.utc.gamedev.Tile.Dungeon.TilePlatform;
|
||||
|
||||
import java.util.LinkedList;
|
||||
|
||||
public class TileManager
|
||||
{
|
||||
private LinkedList<ITile> tiles;
|
||||
private LinkedList<TileDecoration> decorations;
|
||||
private LinkedList<TilePlatform> platforms;
|
||||
|
||||
public TileManager()
|
||||
{
|
||||
tiles = new LinkedList<>();
|
||||
decorations = new LinkedList<>();
|
||||
platforms = new LinkedList<>();
|
||||
}
|
||||
|
||||
public void drawAll()
|
||||
{
|
||||
for (ITile t : tiles)
|
||||
{
|
||||
t.draw();
|
||||
}
|
||||
for (TileDecoration t : decorations)
|
||||
{
|
||||
t.draw();
|
||||
}
|
||||
for (TilePlatform t : platforms)
|
||||
{
|
||||
t.draw();
|
||||
t.update();
|
||||
}
|
||||
}
|
||||
|
||||
public void addTile(ITile t)
|
||||
{
|
||||
if (t instanceof TileDecoration)
|
||||
{
|
||||
this.decorations.add((TileDecoration) t);
|
||||
}
|
||||
else if (t instanceof TilePlatform)
|
||||
{
|
||||
this.platforms.add((TilePlatform) t);
|
||||
}
|
||||
else
|
||||
{
|
||||
this.tiles.add(t);
|
||||
}
|
||||
}
|
||||
|
||||
public void addAll(Iterable<ITile> tiles)
|
||||
{
|
||||
for (ITile t : tiles)
|
||||
{
|
||||
this.addTile(t);
|
||||
}
|
||||
}
|
||||
|
||||
public void removeTile(ITile t)
|
||||
{
|
||||
this.tiles.remove(t);
|
||||
}
|
||||
|
||||
public void removeDecoration(TileDecoration t)
|
||||
{
|
||||
this.decorations.remove(t);
|
||||
}
|
||||
|
||||
public LinkedList<TileDecoration> getDecorations()
|
||||
{
|
||||
return (LinkedList<TileDecoration>) this.decorations.clone();
|
||||
}
|
||||
|
||||
public void clearDecorations()
|
||||
{
|
||||
this.decorations.clear();
|
||||
}
|
||||
|
||||
public LinkedList<TilePlatform> getPlatforms()
|
||||
{
|
||||
return (LinkedList<TilePlatform>) this.platforms.clone();
|
||||
}
|
||||
}
|
||||
297
src/edu/utc/gamedev/Scene/Dungeon.java
Normal file
297
src/edu/utc/gamedev/Scene/Dungeon.java
Normal file
|
|
@ -0,0 +1,297 @@
|
|||
package edu.utc.gamedev.Scene;
|
||||
|
||||
import edu.utc.gamedev.Entity.Item.EntityItem;
|
||||
import edu.utc.gamedev.Entity.Living.EntityMonster;
|
||||
import edu.utc.gamedev.Entity.Living.EntityPlayer;
|
||||
import edu.utc.gamedev.Game;
|
||||
import edu.utc.gamedev.Item.*;
|
||||
import edu.utc.gamedev.Item.Sword.*;
|
||||
import edu.utc.gamedev.Manager.*;
|
||||
import edu.utc.gamedev.Tile.*;
|
||||
import edu.utc.gamedev.Tile.TileBackground;
|
||||
import edu.utc.gamedev.Tile.Dungeon.TileDecoration;
|
||||
import edu.utc.gamedev.Tile.Dungeon.TileExit;
|
||||
import edu.utc.gamedev.Util.FileUtils;
|
||||
import edu.utc.gamedev.Util.Helper;
|
||||
import org.lwjgl.input.Keyboard;
|
||||
import org.lwjgl.opengl.Display;
|
||||
import org.lwjgl.opengl.GL11;
|
||||
import org.lwjgl.util.Color;
|
||||
import org.newdawn.slick.TrueTypeFont;
|
||||
import org.newdawn.slick.opengl.TextureImpl;
|
||||
import org.newdawn.slick.util.ResourceLoader;
|
||||
|
||||
import java.awt.*;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.Random;
|
||||
|
||||
public class Dungeon extends SceneLevel
|
||||
{
|
||||
private static Dungeon instance;
|
||||
public EntityPlayer player;
|
||||
public EntityManager entityManager;
|
||||
public ProjectileManager projectileManager;
|
||||
public TileManager tileManager;
|
||||
public Random rand = new Random();
|
||||
private TileBackground background;
|
||||
private TilePlatformGround ground;
|
||||
private int level, maxLevels;
|
||||
private TrueTypeFont levelFont;
|
||||
private TileExit exit;
|
||||
|
||||
public Dungeon()
|
||||
{
|
||||
this(new EntityPlayer(), 1);
|
||||
}
|
||||
|
||||
public Dungeon(EntityPlayer player, int level)
|
||||
{
|
||||
SceneManager.getInstance().setScene(this);
|
||||
|
||||
// Update variables and such
|
||||
instance = this;
|
||||
|
||||
// Managers
|
||||
this.entityManager = new EntityManager();
|
||||
this.projectileManager = new ProjectileManager();
|
||||
this.tileManager = new TileManager();
|
||||
|
||||
// Player
|
||||
this.player = player;
|
||||
this.entityManager.addEntity(this.player);
|
||||
this.level = level;
|
||||
this.maxLevels = 1;
|
||||
|
||||
initFont();
|
||||
initScene();
|
||||
}
|
||||
|
||||
// public static Living getInstance()
|
||||
// {
|
||||
// return instance;
|
||||
// }
|
||||
|
||||
public void initFont()
|
||||
{
|
||||
try
|
||||
{
|
||||
File font = new File("natives/justabit.ttf".replace("/", File.separator)).getAbsoluteFile();
|
||||
if (!font.exists())
|
||||
{
|
||||
System.out.println("Font did not exist; copying...");
|
||||
FileUtils.copyStream(ResourceLoader.getResourceAsStream("assets/justabit.ttf"), new File("natives/justabit.ttf".replace("/", File.separator)));
|
||||
}
|
||||
|
||||
Font awtFont = Font.createFont(Font.TRUETYPE_FONT, font).deriveFont(32F);
|
||||
levelFont = new TrueTypeFont(awtFont, false);
|
||||
}
|
||||
catch (IOException | FontFormatException e)
|
||||
{
|
||||
levelFont = new TrueTypeFont(new Font("Arial", Font.PLAIN, 32), false);
|
||||
}
|
||||
}
|
||||
|
||||
public void initScene()
|
||||
{
|
||||
// Tiles
|
||||
Color c = Helper.getRandomColor();
|
||||
background = new TileBackground("wall").changeColor(c);
|
||||
ground = new TilePlatformGround();
|
||||
exit = new TileExit().setColor(c);
|
||||
tileManager.addTile(this.background);
|
||||
for (int i = 0; i < rand.nextInt(3) + 1; i++)
|
||||
{
|
||||
tileManager.addTile(new TileDecoration("wall_decor_", 4).changeColor(c));
|
||||
}
|
||||
if (this.level % 5 == 0)
|
||||
{
|
||||
if (getItemAtStage(level / 5) != null)
|
||||
{
|
||||
AudioManager.getInstance().play("level_up");
|
||||
entityManager.addEntity(new EntityItem(getItemAtStage(level / 5), Display.getWidth() / 2, Display.getHeight() - 32, -1));
|
||||
}
|
||||
}
|
||||
|
||||
tileManager.addTile(ground);
|
||||
tileManager.addTile(new TilePlatformBase());
|
||||
|
||||
spawnMonsters();
|
||||
}
|
||||
|
||||
public Item getItemAtStage(int stage)
|
||||
{
|
||||
switch (stage)
|
||||
{
|
||||
case 1:
|
||||
return new ItemDagger();
|
||||
case 2:
|
||||
return new ItemWoodenSword();
|
||||
case 3:
|
||||
return new ItemRustySword();
|
||||
case 4:
|
||||
return new ItemRefinedSword();
|
||||
case 5:
|
||||
return new ItemBroadSword();
|
||||
case 6:
|
||||
return new ItemGreatSword();
|
||||
case 7:
|
||||
return new ItemPotatoGun();
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Scene nextScene()
|
||||
{
|
||||
// Progress to the next level!
|
||||
if (entityManager.allMonstersDead() && player.getHitBox().intersects(exit.getHitBox()))
|
||||
{
|
||||
// If the next level is less than level
|
||||
if (level + 1 <= maxLevels)
|
||||
{
|
||||
return new Dungeon(player, level + 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
return new Surface(player);
|
||||
}
|
||||
}
|
||||
|
||||
Menu menu = new Menu();
|
||||
if (player == null || player.getState() == EntityPlayer.State.DEAD)
|
||||
{
|
||||
menu.addItem("Play Again", new Dungeon());
|
||||
}
|
||||
else
|
||||
{
|
||||
menu.addItem("Resume", this);
|
||||
}
|
||||
menu.addSpecial("Exit", Menu.DO_EXIT);
|
||||
|
||||
return menu;
|
||||
|
||||
}
|
||||
|
||||
|
||||
public TileBackground getBackground()
|
||||
{
|
||||
return background;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean drawFrame(float delta)
|
||||
{
|
||||
entityManager.updateAll(delta);
|
||||
projectileManager.updateAll(delta);
|
||||
|
||||
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
|
||||
|
||||
tileManager.drawAll();
|
||||
if (entityManager.allMonstersDead())
|
||||
{
|
||||
exit.draw();
|
||||
}
|
||||
entityManager.drawAll();
|
||||
projectileManager.drawAll();
|
||||
|
||||
|
||||
// I use F2 for screenshots based on other games I've enjoyed
|
||||
if (Keyboard.isKeyDown(Keyboard.KEY_F2))
|
||||
{
|
||||
Game.getInstance().screenshotManager.takeScreenshot();
|
||||
}
|
||||
|
||||
// I use F11 for fullscreen for the same reason!
|
||||
if (Keyboard.isKeyDown(Keyboard.KEY_F11))
|
||||
{
|
||||
if (Display.isFullscreen())
|
||||
{
|
||||
Game.getInstance().setDisplayMode(Game.getInstance().config.resolutionWidth, Game.getInstance().config.resolutionHeight, false);
|
||||
}
|
||||
else
|
||||
{
|
||||
Game.getInstance().setDisplayMode(Toolkit.getDefaultToolkit().getScreenSize().width, Toolkit.getDefaultToolkit().getScreenSize().height, true);
|
||||
}
|
||||
Display.setResizable(true);
|
||||
GL11.glViewport(0, 0, Display.getWidth(), Display.getHeight());
|
||||
GL11.glMatrixMode(GL11.GL_PROJECTION);
|
||||
GL11.glLoadIdentity();
|
||||
GL11.glOrtho(0, Display.getWidth(), Display.getHeight(), 0, 1, -1);
|
||||
}
|
||||
|
||||
// Screen resize handler
|
||||
if (Display.wasResized())
|
||||
{
|
||||
GL11.glViewport(0, 0, Display.getWidth(), Display.getHeight());
|
||||
GL11.glMatrixMode(GL11.GL_PROJECTION);
|
||||
GL11.glLoadIdentity();
|
||||
GL11.glOrtho(0, Display.getWidth(), Display.getHeight(), 0, 1, -1);
|
||||
}
|
||||
|
||||
TextureImpl.bindNone();
|
||||
int rem = (maxLevels - level) + 1;
|
||||
|
||||
levelFont.drawString(1, 0, (rem + (rem == 1 ? " level" : " levels") + " from the surface"));
|
||||
|
||||
if (Keyboard.isKeyDown(Keyboard.KEY_ESCAPE))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (entityManager.allMonstersDead())
|
||||
{
|
||||
if (player.getHitBox().intersects(exit.getHitBox()))
|
||||
{
|
||||
if (Keyboard.isKeyDown(Keyboard.KEY_SPACE))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return player != null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Spawns a quantity of monsters directly corresponding to the level the player is at
|
||||
*/
|
||||
public void spawnMonsters()
|
||||
{
|
||||
int variants = EntityMonster.Type.values().length;
|
||||
|
||||
for (int i = 0; i < this.level; i++)
|
||||
{
|
||||
entityManager.addEntity(new EntityMonster(EntityMonster.Type.values()[rand.nextInt(variants)]));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public EntityPlayer getPlayer()
|
||||
{
|
||||
return this.player;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setPlayer(EntityPlayer p)
|
||||
{
|
||||
this.player = p;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TileManager getTileManager()
|
||||
{
|
||||
return this.tileManager;
|
||||
}
|
||||
|
||||
@Override
|
||||
public EntityManager getEntityManager()
|
||||
{
|
||||
return this.entityManager;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ProjectileManager getProjectileManager()
|
||||
{
|
||||
return this.projectileManager;
|
||||
}
|
||||
}
|
||||
226
src/edu/utc/gamedev/Scene/Menu.java
Normal file
226
src/edu/utc/gamedev/Scene/Menu.java
Normal file
|
|
@ -0,0 +1,226 @@
|
|||
package edu.utc.gamedev.Scene;
|
||||
|
||||
import edu.utc.gamedev.Game;
|
||||
import edu.utc.gamedev.Manager.AudioManager;
|
||||
import edu.utc.gamedev.Tile.TileBackground;
|
||||
import edu.utc.gamedev.Util.FileUtils;
|
||||
import edu.utc.gamedev.Util.Helper;
|
||||
import org.lwjgl.input.Keyboard;
|
||||
import org.lwjgl.opengl.Display;
|
||||
import org.lwjgl.opengl.GL11;
|
||||
import org.newdawn.slick.Color;
|
||||
import org.newdawn.slick.TrueTypeFont;
|
||||
import org.newdawn.slick.opengl.TextureImpl;
|
||||
import org.newdawn.slick.util.ResourceLoader;
|
||||
|
||||
import java.awt.*;
|
||||
import java.awt.font.TextAttribute;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
public class Menu extends Scene
|
||||
{
|
||||
public static final int DO_EXIT = 0;
|
||||
private List<Item> items;
|
||||
private int currItem;
|
||||
private long lastChanged;
|
||||
private TileBackground bg;
|
||||
private TrueTypeFont menuFont;
|
||||
private TrueTypeFont selectedFont;
|
||||
private TrueTypeFont titleFont;
|
||||
|
||||
public Menu()
|
||||
{
|
||||
items = new LinkedList<>();
|
||||
lastChanged = Helper.getTime();
|
||||
bg = new TileBackground("menu");
|
||||
HashMap<TextAttribute, Integer> titleAttributes = new HashMap<>();
|
||||
titleAttributes.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
|
||||
|
||||
HashMap<TextAttribute, Integer> selectedAttributes = new HashMap<>();
|
||||
selectedAttributes.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
|
||||
|
||||
try
|
||||
{
|
||||
File font = new File("natives/justabit.ttf".replace("/", File.separator)).getAbsoluteFile();
|
||||
if (!font.exists())
|
||||
{
|
||||
System.out.println("Font did not exist; copying...");
|
||||
FileUtils.copyStream(ResourceLoader.getResourceAsStream("assets/justabit.ttf"), new File("natives/justabit.ttf".replace("/", File.separator)));
|
||||
}
|
||||
|
||||
Font awtFont = Font.createFont(Font.TRUETYPE_FONT, font).deriveFont(titleAttributes).deriveFont(64F);
|
||||
titleFont = new TrueTypeFont(awtFont, false);
|
||||
|
||||
awtFont = Font.createFont(Font.TRUETYPE_FONT, font).deriveFont(selectedAttributes).deriveFont(Font.BOLD).deriveFont(52F);
|
||||
selectedFont = new TrueTypeFont(awtFont, false);
|
||||
|
||||
awtFont = Font.createFont(Font.TRUETYPE_FONT, font).deriveFont(40F);
|
||||
menuFont = new TrueTypeFont(awtFont, false);
|
||||
}
|
||||
catch (IOException | FontFormatException e)
|
||||
{
|
||||
titleFont = new TrueTypeFont(new Font("Arial", Font.PLAIN, 64).deriveFont(titleAttributes).deriveFont(64F), false);
|
||||
selectedFont = new TrueTypeFont(new Font("Arial", Font.BOLD, 52).deriveFont(selectedAttributes).deriveFont(Font.BOLD).deriveFont(52F), false);
|
||||
menuFont = new TrueTypeFont(new Font("Arial", Font.PLAIN, 40).deriveFont(40F), false);
|
||||
}
|
||||
}
|
||||
|
||||
// reset menu
|
||||
public void clear()
|
||||
{
|
||||
items.clear();
|
||||
}
|
||||
|
||||
public void addItem(String label, Scene s)
|
||||
{
|
||||
items.add(new Item(label, s));
|
||||
}
|
||||
|
||||
public void addSpecial(String label, int tag)
|
||||
{
|
||||
items.add(new Special(label, tag));
|
||||
}
|
||||
|
||||
public Scene nextScene()
|
||||
{
|
||||
return items.get(currItem).scene;
|
||||
}
|
||||
|
||||
public boolean drawFrame(float delta)
|
||||
{
|
||||
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
|
||||
|
||||
bg.draw();
|
||||
|
||||
while (Keyboard.next())
|
||||
{
|
||||
if (Keyboard.getEventKeyState()) // key was pressed
|
||||
{
|
||||
switch (Keyboard.getEventKey())
|
||||
{
|
||||
case Keyboard.KEY_W:
|
||||
AudioManager.getInstance().play("menu_move");
|
||||
currItem--;
|
||||
if (currItem < 0)
|
||||
{
|
||||
currItem += items.size(); // go to end
|
||||
}
|
||||
break;
|
||||
case Keyboard.KEY_UP:
|
||||
AudioManager.getInstance().play("menu_move");
|
||||
currItem--;
|
||||
if (currItem < 0)
|
||||
{
|
||||
currItem += items.size(); // go to end
|
||||
}
|
||||
break;
|
||||
case Keyboard.KEY_S:
|
||||
AudioManager.getInstance().play("menu_move");
|
||||
currItem = (currItem + 1) % items.size();
|
||||
break;
|
||||
case Keyboard.KEY_DOWN:
|
||||
AudioManager.getInstance().play("menu_move");
|
||||
currItem = (currItem + 1) % items.size();
|
||||
break;
|
||||
case Keyboard.KEY_RETURN:
|
||||
AudioManager.getInstance().play("menu_select");
|
||||
Item item = items.get(currItem);
|
||||
if (item instanceof Special)
|
||||
{
|
||||
switch (((Special) item).tag)
|
||||
{
|
||||
case DO_EXIT:
|
||||
exit();
|
||||
break;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
case Keyboard.KEY_F2:
|
||||
Game.getInstance().screenshotManager.takeScreenshot();
|
||||
break;
|
||||
case Keyboard.KEY_F11:
|
||||
if (Display.isFullscreen())
|
||||
{
|
||||
Game.getInstance().setDisplayMode(Game.getInstance().config.resolutionWidth, Game.getInstance().config.resolutionHeight, false);
|
||||
}
|
||||
else
|
||||
{
|
||||
Game.getInstance().setDisplayMode(Toolkit.getDefaultToolkit().getScreenSize().width, Toolkit.getDefaultToolkit().getScreenSize().height, true);
|
||||
}
|
||||
Display.setResizable(true);
|
||||
GL11.glViewport(0, 0, Display.getWidth(), Display.getHeight());
|
||||
GL11.glMatrixMode(GL11.GL_PROJECTION);
|
||||
GL11.glLoadIdentity();
|
||||
GL11.glOrtho(0, Display.getWidth(), Display.getHeight(), 0, 1, -1);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Screen resize handler
|
||||
if (Display.wasResized())
|
||||
{
|
||||
GL11.glViewport(0, 0, Display.getWidth(), Display.getHeight());
|
||||
GL11.glMatrixMode(GL11.GL_PROJECTION);
|
||||
GL11.glLoadIdentity();
|
||||
GL11.glOrtho(0, Display.getWidth(), Display.getHeight(), 0, 1, -1);
|
||||
}
|
||||
|
||||
float spacing = Display.getHeight() / (items.size() + 4);
|
||||
float offset = 2 * spacing;
|
||||
|
||||
TextureImpl.bindNone();
|
||||
titleFont.drawString((Display.getWidth() / 2) - (titleFont.getWidth("Crawl 'n Brawl") / 2), 64F, "Crawl 'n Brawl", new Color(0, 123, 124));
|
||||
|
||||
for (int i = 0; i < items.size(); i++)
|
||||
{
|
||||
if (i == currItem)
|
||||
{
|
||||
TextureImpl.bindNone();
|
||||
|
||||
selectedFont.drawString(Display.getWidth() / 2 - (selectedFont.getWidth(items.get(i).label) / 2), offset, items.get(i).label, new Color(85, 124, 0));
|
||||
}
|
||||
else
|
||||
{
|
||||
TextureImpl.bindNone();
|
||||
menuFont.drawString(Display.getWidth() / 2 - (menuFont.getWidth(items.get(i).label) / 2), offset, items.get(i).label, new Color(74, 91, 37));
|
||||
}
|
||||
|
||||
offset += spacing;
|
||||
}
|
||||
|
||||
// font binds a texture, so let's turn it off..
|
||||
GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
// a menu item: label and associated Scene to jump to
|
||||
private static class Item
|
||||
{
|
||||
public String label;
|
||||
public Scene scene;
|
||||
|
||||
public Item(String label, Scene s)
|
||||
{
|
||||
this.label = label;
|
||||
this.scene = s;
|
||||
}
|
||||
}
|
||||
|
||||
private static class Special extends Item
|
||||
{
|
||||
public int tag;
|
||||
|
||||
public Special(String label, int tag)
|
||||
{
|
||||
super(label, null);
|
||||
this.tag = tag;
|
||||
}
|
||||
}
|
||||
}
|
||||
53
src/edu/utc/gamedev/Scene/Scene.java
Normal file
53
src/edu/utc/gamedev/Scene/Scene.java
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
package edu.utc.gamedev.Scene;
|
||||
|
||||
import edu.utc.gamedev.Game;
|
||||
import edu.utc.gamedev.Manager.AudioManager;
|
||||
import edu.utc.gamedev.Util.Helper;
|
||||
import org.lwjgl.opengl.Display;
|
||||
|
||||
public abstract class Scene
|
||||
{
|
||||
private boolean doExit = false;
|
||||
|
||||
// return false if the game should be quit
|
||||
public abstract boolean drawFrame(float delta);
|
||||
|
||||
// null typically means Game should load menu
|
||||
public Scene nextScene()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
protected void exit()
|
||||
{
|
||||
doExit = true;
|
||||
}
|
||||
|
||||
// returns false when game should be exited
|
||||
public boolean go()
|
||||
{
|
||||
long lastloop = Helper.getTime();
|
||||
|
||||
boolean keepGoing = true;
|
||||
do
|
||||
{
|
||||
Display.sync(Game.getInstance().config.targetFPS);
|
||||
long now = Helper.getTime();
|
||||
long delta = now - lastloop;
|
||||
lastloop = now;
|
||||
|
||||
keepGoing = drawFrame(delta);
|
||||
|
||||
// UPDATE DISPLAY
|
||||
Display.update();
|
||||
AudioManager.getInstance().update();
|
||||
|
||||
if (Display.isCloseRequested() || doExit)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
} while (keepGoing);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
32
src/edu/utc/gamedev/Scene/SceneLevel.java
Normal file
32
src/edu/utc/gamedev/Scene/SceneLevel.java
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
package edu.utc.gamedev.Scene;
|
||||
|
||||
import edu.utc.gamedev.Entity.Living.EntityPlayer;
|
||||
import edu.utc.gamedev.Manager.EntityManager;
|
||||
import edu.utc.gamedev.Manager.ProjectileManager;
|
||||
import edu.utc.gamedev.Manager.SceneManager;
|
||||
import edu.utc.gamedev.Manager.TileManager;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
public abstract class SceneLevel extends Scene
|
||||
{
|
||||
public SceneLevel()
|
||||
{
|
||||
SceneManager.getInstance().setScene(this);
|
||||
}
|
||||
|
||||
public abstract EntityPlayer getPlayer();
|
||||
|
||||
public abstract void setPlayer(EntityPlayer player);
|
||||
|
||||
public abstract TileManager getTileManager();
|
||||
|
||||
public abstract EntityManager getEntityManager();
|
||||
|
||||
public abstract ProjectileManager getProjectileManager();
|
||||
|
||||
public Random rand()
|
||||
{
|
||||
return new Random();
|
||||
}
|
||||
}
|
||||
228
src/edu/utc/gamedev/Scene/Surface.java
Normal file
228
src/edu/utc/gamedev/Scene/Surface.java
Normal file
|
|
@ -0,0 +1,228 @@
|
|||
package edu.utc.gamedev.Scene;
|
||||
|
||||
import edu.utc.gamedev.Entity.Living.EntityPlayer;
|
||||
import edu.utc.gamedev.Game;
|
||||
import edu.utc.gamedev.Manager.EntityManager;
|
||||
import edu.utc.gamedev.Manager.ProjectileManager;
|
||||
import edu.utc.gamedev.Manager.SceneManager;
|
||||
import edu.utc.gamedev.Manager.TileManager;
|
||||
import edu.utc.gamedev.Tile.ITile;
|
||||
import edu.utc.gamedev.Tile.Surface.TileGrass;
|
||||
import edu.utc.gamedev.Tile.Surface.TileHouse;
|
||||
import edu.utc.gamedev.Tile.Surface.TileWell;
|
||||
import edu.utc.gamedev.Tile.TileBackground;
|
||||
import edu.utc.gamedev.Tile.TilePlatformGround;
|
||||
import edu.utc.gamedev.Util.FileUtils;
|
||||
import org.lwjgl.input.Keyboard;
|
||||
import org.lwjgl.opengl.Display;
|
||||
import org.lwjgl.opengl.GL11;
|
||||
import org.newdawn.slick.TrueTypeFont;
|
||||
import org.newdawn.slick.opengl.TextureImpl;
|
||||
import org.newdawn.slick.util.ResourceLoader;
|
||||
|
||||
import java.awt.*;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class Surface extends SceneLevel
|
||||
{
|
||||
|
||||
private EntityPlayer player;
|
||||
private TileManager tileManager;
|
||||
private TileBackground background;
|
||||
private TilePlatformGround ground;
|
||||
private TrueTypeFont levelFont;
|
||||
private EntityManager entityManager;
|
||||
private ITile house, well;
|
||||
private ProjectileManager projectileManager;
|
||||
|
||||
public Surface(EntityPlayer player)
|
||||
{
|
||||
SceneManager.getInstance().setScene(this);
|
||||
|
||||
// Managers
|
||||
this.tileManager = new TileManager();
|
||||
this.entityManager = new EntityManager();
|
||||
this.projectileManager = new ProjectileManager();
|
||||
|
||||
// Player
|
||||
this.player = player;
|
||||
|
||||
// House
|
||||
this.house = new TileHouse();
|
||||
this.well = new TileWell();
|
||||
this.player.getHitBox().setX(well.getHitBox().getX() + well.getHitBox().getWidth() + 32);
|
||||
this.player.getHitBox().setY(Display.getHeight() - this.player.getHitBox().getHeight());
|
||||
|
||||
initFont();
|
||||
initScene();
|
||||
}
|
||||
|
||||
|
||||
public void initFont()
|
||||
{
|
||||
try
|
||||
{
|
||||
File font = new File("natives/justabit.ttf".replace("/", File.separator)).getAbsoluteFile();
|
||||
if (!font.exists())
|
||||
{
|
||||
System.out.println("Font did not exist; copying...");
|
||||
FileUtils.copyStream(ResourceLoader.getResourceAsStream("assets/justabit.ttf"), new File("natives/justabit.ttf".replace("/", File.separator)));
|
||||
}
|
||||
|
||||
Font awtFont = Font.createFont(Font.TRUETYPE_FONT, font).deriveFont(32F);
|
||||
levelFont = new TrueTypeFont(awtFont, false);
|
||||
}
|
||||
catch (IOException | FontFormatException e)
|
||||
{
|
||||
levelFont = new TrueTypeFont(new Font("Arial", Font.PLAIN, 32), false);
|
||||
}
|
||||
}
|
||||
|
||||
public void initScene()
|
||||
{
|
||||
// Tiles
|
||||
background = new TileBackground("sky");
|
||||
ground = new TilePlatformGround();
|
||||
tileManager.addTile(ground);
|
||||
tileManager.addTile(background);
|
||||
tileManager.addAll(populateGrass());
|
||||
tileManager.addTile(house);
|
||||
tileManager.addTile(well);
|
||||
entityManager.addEntity(player);
|
||||
player.setHeldItem(null);
|
||||
}
|
||||
|
||||
private ArrayList<ITile> populateGrass()
|
||||
{
|
||||
int x = 0;
|
||||
ArrayList<ITile> ret = new ArrayList<>();
|
||||
TileGrass grassTemp;
|
||||
while (x < Toolkit.getDefaultToolkit().getScreenSize().width)
|
||||
{
|
||||
grassTemp = new TileGrass(x);
|
||||
ret.add(grassTemp);
|
||||
x += grassTemp.getHitBox().getWidth();
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Scene nextScene()
|
||||
{
|
||||
if (player != null && player.getHitBox().intersects(well.getHitBox()))
|
||||
{
|
||||
return new Dungeon();
|
||||
}
|
||||
Menu menu = new Menu();
|
||||
if (player == null || player.getState() == EntityPlayer.State.DEAD)
|
||||
{
|
||||
menu.addItem("Play Again", new Dungeon());
|
||||
}
|
||||
else
|
||||
{
|
||||
menu.addItem("Resume", this);
|
||||
}
|
||||
menu.addSpecial("Exit", Menu.DO_EXIT);
|
||||
|
||||
return menu;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean drawFrame(float delta)
|
||||
{
|
||||
entityManager.updateAll(delta);
|
||||
projectileManager.updateAll(delta);
|
||||
|
||||
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
|
||||
|
||||
tileManager.drawAll();
|
||||
entityManager.drawAll();
|
||||
projectileManager.drawAll();
|
||||
|
||||
// I use F2 for screenshots based on other games I've enjoyed
|
||||
if (Keyboard.isKeyDown(Keyboard.KEY_F2))
|
||||
{
|
||||
Game.getInstance().screenshotManager.takeScreenshot();
|
||||
}
|
||||
|
||||
// I use F11 for fullscreen for the same reason!
|
||||
if (Keyboard.isKeyDown(Keyboard.KEY_F11))
|
||||
{
|
||||
if (Display.isFullscreen())
|
||||
{
|
||||
Game.getInstance().setDisplayMode(Game.getInstance().config.resolutionWidth, Game.getInstance().config.resolutionHeight, false);
|
||||
}
|
||||
else
|
||||
{
|
||||
Game.getInstance().setDisplayMode(Toolkit.getDefaultToolkit().getScreenSize().width, Toolkit.getDefaultToolkit().getScreenSize().height, true);
|
||||
}
|
||||
Display.setResizable(true);
|
||||
GL11.glViewport(0, 0, Display.getWidth(), Display.getHeight());
|
||||
GL11.glMatrixMode(GL11.GL_PROJECTION);
|
||||
GL11.glLoadIdentity();
|
||||
GL11.glOrtho(0, Display.getWidth(), Display.getHeight(), 0, 1, -1);
|
||||
}
|
||||
|
||||
// Screen resize handler
|
||||
if (Display.wasResized())
|
||||
{
|
||||
GL11.glViewport(0, 0, Display.getWidth(), Display.getHeight());
|
||||
GL11.glMatrixMode(GL11.GL_PROJECTION);
|
||||
GL11.glLoadIdentity();
|
||||
GL11.glOrtho(0, Display.getWidth(), Display.getHeight(), 0, 1, -1);
|
||||
}
|
||||
|
||||
TextureImpl.bindNone();
|
||||
levelFont.drawString(1, 0, ("Surface"));
|
||||
|
||||
if (Keyboard.isKeyDown(Keyboard.KEY_ESCAPE))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (player.getHitBox().intersects(house.getHitBox()))
|
||||
{
|
||||
entityManager.removeEntity(player);
|
||||
player = null;
|
||||
return false;
|
||||
}
|
||||
if (player.getHitBox().intersects(well.getHitBox()))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public EntityPlayer getPlayer()
|
||||
{
|
||||
return this.player;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setPlayer(EntityPlayer p)
|
||||
{
|
||||
this.player = p;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TileManager getTileManager()
|
||||
{
|
||||
return this.tileManager;
|
||||
}
|
||||
|
||||
@Override
|
||||
public EntityManager getEntityManager()
|
||||
{
|
||||
return this.entityManager;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ProjectileManager getProjectileManager()
|
||||
{
|
||||
return this.projectileManager;
|
||||
}
|
||||
}
|
||||
103
src/edu/utc/gamedev/Tile/Dungeon/TileDecoration.java
Normal file
103
src/edu/utc/gamedev/Tile/Dungeon/TileDecoration.java
Normal file
|
|
@ -0,0 +1,103 @@
|
|||
package edu.utc.gamedev.Tile.Dungeon;
|
||||
|
||||
import edu.utc.gamedev.Manager.SceneManager;
|
||||
import edu.utc.gamedev.Scene.Dungeon;
|
||||
import edu.utc.gamedev.Scene.Scene;
|
||||
import edu.utc.gamedev.Tile.ITile;
|
||||
import edu.utc.gamedev.Util.Helper;
|
||||
import org.lwjgl.opengl.Display;
|
||||
import org.lwjgl.opengl.GL11;
|
||||
import org.lwjgl.util.Color;
|
||||
import org.lwjgl.util.Rectangle;
|
||||
import org.newdawn.slick.opengl.Texture;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Random;
|
||||
|
||||
|
||||
public class TileDecoration implements ITile
|
||||
{
|
||||
private Texture sprite;
|
||||
private Rectangle box;
|
||||
private Color color;
|
||||
|
||||
public TileDecoration(String spriteName, int numberOfVariants)
|
||||
{
|
||||
Random rand = new Random();
|
||||
if (numberOfVariants > 0)
|
||||
{
|
||||
|
||||
int spriteVariant = numberOfVariants > 1 ? rand.nextInt(numberOfVariants) + 1 : 1;
|
||||
sprite = Helper.loadTexture("environment/".replace("/", File.separator) + spriteName + spriteVariant);
|
||||
}
|
||||
else
|
||||
{
|
||||
sprite = Helper.loadTexture("environment/".replace("/", File.separator) + spriteName);
|
||||
}
|
||||
int xLoc = rand.nextInt(Display.getWidth() / sprite.getTextureWidth()) * sprite.getTextureWidth();
|
||||
int yLoc = rand.nextInt(Display.getHeight() / sprite.getTextureHeight()) * sprite.getTextureHeight();
|
||||
this.box = new Rectangle(xLoc, yLoc, sprite.getTextureWidth(), sprite.getTextureHeight());
|
||||
this.color = new Color(255, 255, 255);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void draw()
|
||||
{
|
||||
Scene s = SceneManager.getInstance().getScene();
|
||||
if (!(s instanceof Dungeon))
|
||||
{
|
||||
return;
|
||||
}
|
||||
Dungeon dungeon = (Dungeon) s;
|
||||
int scaleFactor = (dungeon.getBackground().getHitBox().getWidth() / 1024);
|
||||
this.box.setWidth(sprite.getTextureWidth() * scaleFactor);
|
||||
this.box.setHeight(sprite.getTextureHeight() * scaleFactor);
|
||||
|
||||
float x = (float) box.getX();
|
||||
float y = (float) box.getY();
|
||||
float w = (float) box.getWidth();
|
||||
float h = (float) box.getHeight();
|
||||
|
||||
GL11.glBindTexture(GL11.GL_TEXTURE_2D, sprite.getTextureID());
|
||||
|
||||
GL11.glColor3f(color.getRed() / 255F, (float) color.getGreen() / 255F, (float) color.getBlue() / 255F);
|
||||
|
||||
// Allows the background to scale w/o blurring
|
||||
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST);
|
||||
|
||||
GL11.glBegin(GL11.GL_QUADS);
|
||||
|
||||
GL11.glTexCoord2f(0F, 0F);
|
||||
GL11.glVertex2f(x, y);
|
||||
|
||||
GL11.glTexCoord2f(1F, 0F);
|
||||
GL11.glVertex2f(x + w, y);
|
||||
|
||||
GL11.glTexCoord2f(1F, 1F);
|
||||
GL11.glVertex2f(x + w, y + h);
|
||||
|
||||
GL11.glTexCoord2f(0F, 1F);
|
||||
GL11.glVertex2f(x, y + h);
|
||||
|
||||
GL11.glEnd();
|
||||
|
||||
GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param c the color overlay to apply to the background
|
||||
* <p>
|
||||
* Changes the background GL color to the specified color
|
||||
*/
|
||||
public TileDecoration changeColor(Color c)
|
||||
{
|
||||
this.color = c;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Rectangle getHitBox()
|
||||
{
|
||||
return this.box;
|
||||
}
|
||||
}
|
||||
79
src/edu/utc/gamedev/Tile/Dungeon/TileExit.java
Normal file
79
src/edu/utc/gamedev/Tile/Dungeon/TileExit.java
Normal file
|
|
@ -0,0 +1,79 @@
|
|||
package edu.utc.gamedev.Tile.Dungeon;
|
||||
|
||||
import edu.utc.gamedev.Tile.ITile;
|
||||
import edu.utc.gamedev.Util.Helper;
|
||||
import org.lwjgl.opengl.Display;
|
||||
import org.lwjgl.opengl.GL11;
|
||||
import org.lwjgl.util.Color;
|
||||
import org.lwjgl.util.Rectangle;
|
||||
import org.newdawn.slick.opengl.Texture;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
|
||||
public class TileExit implements ITile
|
||||
{
|
||||
|
||||
private Texture sprite;
|
||||
private Rectangle box;
|
||||
private Color color;
|
||||
|
||||
public TileExit()
|
||||
{
|
||||
sprite = Helper.loadTexture("environment/exit".replace("/", File.separator));
|
||||
this.box = new Rectangle(0, Display.getHeight() - sprite.getTextureHeight(), sprite.getTextureWidth(), sprite.getTextureHeight());
|
||||
color = new Color(255, 255, 255);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void draw()
|
||||
{
|
||||
if(this.box.getY() != Display.getHeight() - this.box.getHeight())
|
||||
{
|
||||
this.box.setY(Display.getHeight() - this.box.getHeight());
|
||||
}
|
||||
|
||||
float x = (float) box.getX();
|
||||
float y = (float) box.getY();
|
||||
float w = (float) box.getWidth();
|
||||
float h = (float) box.getHeight();
|
||||
|
||||
GL11.glBindTexture(GL11.GL_TEXTURE_2D, sprite.getTextureID());
|
||||
|
||||
GL11.glColor3f(color.getRed() / 255F, (float) color.getGreen() / 255F, (float) color.getBlue() / 255F);
|
||||
|
||||
// Allows the background to scale w/o blurring
|
||||
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST);
|
||||
|
||||
GL11.glBegin(GL11.GL_QUADS);
|
||||
|
||||
GL11.glTexCoord2f(0F, 0F);
|
||||
GL11.glVertex2f(x, y);
|
||||
|
||||
GL11.glTexCoord2f(1F, 0F);
|
||||
GL11.glVertex2f(x + w, y);
|
||||
|
||||
GL11.glTexCoord2f(1F, 1F);
|
||||
GL11.glVertex2f(x + w, y + h);
|
||||
|
||||
GL11.glTexCoord2f(0F, 1F);
|
||||
GL11.glVertex2f(x, y + h);
|
||||
|
||||
GL11.glEnd();
|
||||
|
||||
GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Rectangle getHitBox()
|
||||
{
|
||||
return this.box;
|
||||
}
|
||||
|
||||
public TileExit setColor(Color c)
|
||||
{
|
||||
this.color = c;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
23
src/edu/utc/gamedev/Tile/Dungeon/TilePlatform.java
Normal file
23
src/edu/utc/gamedev/Tile/Dungeon/TilePlatform.java
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
package edu.utc.gamedev.Tile.Dungeon;
|
||||
|
||||
import edu.utc.gamedev.Entity.Entity;
|
||||
import edu.utc.gamedev.Tile.ITile;
|
||||
import org.lwjgl.util.Rectangle;
|
||||
|
||||
public abstract class TilePlatform implements ITile
|
||||
{
|
||||
@Override
|
||||
public abstract void draw();
|
||||
|
||||
@Override
|
||||
public abstract Rectangle getHitBox();
|
||||
|
||||
public abstract void update();
|
||||
|
||||
public boolean entityStanding(Entity e)
|
||||
{
|
||||
Rectangle entityHitBox = new Rectangle(e.getHitBox().getX(), e.getHitBox().getY(), e.getHitBox().getWidth(), e.getHitBox().getHeight());
|
||||
entityHitBox.translate(0, 1);
|
||||
return entityHitBox.intersects(getHitBox());
|
||||
}
|
||||
}
|
||||
10
src/edu/utc/gamedev/Tile/ITile.java
Normal file
10
src/edu/utc/gamedev/Tile/ITile.java
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
package edu.utc.gamedev.Tile;
|
||||
|
||||
import org.lwjgl.util.Rectangle;
|
||||
|
||||
public interface ITile
|
||||
{
|
||||
void draw();
|
||||
|
||||
Rectangle getHitBox();
|
||||
}
|
||||
68
src/edu/utc/gamedev/Tile/Surface/TileGrass.java
Normal file
68
src/edu/utc/gamedev/Tile/Surface/TileGrass.java
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
package edu.utc.gamedev.Tile.Surface;
|
||||
|
||||
import edu.utc.gamedev.Tile.ITile;
|
||||
import edu.utc.gamedev.Util.Helper;
|
||||
import org.lwjgl.opengl.Display;
|
||||
import org.lwjgl.opengl.GL11;
|
||||
import org.lwjgl.util.Rectangle;
|
||||
import org.newdawn.slick.opengl.Texture;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
|
||||
public class TileGrass implements ITile
|
||||
{
|
||||
private Texture sprite;
|
||||
private Rectangle box;
|
||||
public TileGrass(int x)
|
||||
{
|
||||
sprite = Helper.loadTexture("environment/grass".replace("/", File.separator));
|
||||
this.box = new Rectangle(x, 0, sprite.getTextureWidth(), sprite.getTextureHeight());
|
||||
this.box.setY(Display.getHeight() - this.box.getHeight());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void draw()
|
||||
{
|
||||
if (this.box.getY() != Display.getHeight() - this.box.getHeight())
|
||||
{
|
||||
this.box.setY(Display.getHeight() - this.box.getHeight());
|
||||
}
|
||||
|
||||
float x = (float) box.getX();
|
||||
float y = (float) box.getY();
|
||||
float w = (float) box.getWidth();
|
||||
float h = (float) box.getHeight();
|
||||
|
||||
GL11.glBindTexture(GL11.GL_TEXTURE_2D, sprite.getTextureID());
|
||||
|
||||
GL11.glColor3f(255F, 255F, 255F);
|
||||
|
||||
// Allows the background to scale w/o blurring
|
||||
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST);
|
||||
|
||||
GL11.glBegin(GL11.GL_QUADS);
|
||||
|
||||
GL11.glTexCoord2f(0F, 0F);
|
||||
GL11.glVertex2f(x, y);
|
||||
|
||||
GL11.glTexCoord2f(1F, 0F);
|
||||
GL11.glVertex2f(x + w, y);
|
||||
|
||||
GL11.glTexCoord2f(1F, 1F);
|
||||
GL11.glVertex2f(x + w, y + h);
|
||||
|
||||
GL11.glTexCoord2f(0F, 1F);
|
||||
GL11.glVertex2f(x, y + h);
|
||||
|
||||
GL11.glEnd();
|
||||
|
||||
GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Rectangle getHitBox()
|
||||
{
|
||||
return this.box;
|
||||
}
|
||||
}
|
||||
67
src/edu/utc/gamedev/Tile/Surface/TileHouse.java
Normal file
67
src/edu/utc/gamedev/Tile/Surface/TileHouse.java
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
package edu.utc.gamedev.Tile.Surface;
|
||||
|
||||
import edu.utc.gamedev.Tile.ITile;
|
||||
import edu.utc.gamedev.Util.Helper;
|
||||
import org.lwjgl.opengl.Display;
|
||||
import org.lwjgl.opengl.GL11;
|
||||
import org.lwjgl.util.Rectangle;
|
||||
import org.newdawn.slick.opengl.Texture;
|
||||
|
||||
public class TileHouse implements ITile
|
||||
{
|
||||
private Rectangle box;
|
||||
private Texture sprite;
|
||||
|
||||
public TileHouse()
|
||||
{
|
||||
sprite = Helper.loadTexture("environment/house");
|
||||
box = new Rectangle(0, 0, sprite.getTextureWidth(), sprite.getTextureHeight());
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void draw()
|
||||
{
|
||||
if (box.getX() != (Display.getWidth() - box.getWidth()) - 32)
|
||||
{
|
||||
box.setX((Display.getWidth() - box.getWidth()) - 32);
|
||||
}
|
||||
if (box.getY() != (Display.getHeight() - box.getHeight()))
|
||||
{
|
||||
box.setY(Display.getHeight() - box.getHeight());
|
||||
}
|
||||
|
||||
float x = (float) box.getX();
|
||||
float y = (float) box.getY();
|
||||
float w = (float) box.getWidth();
|
||||
float h = (float) box.getHeight();
|
||||
|
||||
GL11.glBindTexture(GL11.GL_TEXTURE_2D, sprite.getTextureID());
|
||||
|
||||
GL11.glColor3f(255F, 255F, 255F);
|
||||
|
||||
GL11.glBegin(GL11.GL_QUADS);
|
||||
|
||||
GL11.glTexCoord2f(0F, 0F);
|
||||
GL11.glVertex2f(x, y);
|
||||
|
||||
GL11.glTexCoord2f(1F, 0F);
|
||||
GL11.glVertex2f(x + w, y);
|
||||
|
||||
GL11.glTexCoord2f(1F, 1F);
|
||||
GL11.glVertex2f(x + w, y + h);
|
||||
|
||||
GL11.glTexCoord2f(0F, 1F);
|
||||
GL11.glVertex2f(x, y + h);
|
||||
|
||||
GL11.glEnd();
|
||||
|
||||
GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Rectangle getHitBox()
|
||||
{
|
||||
return this.box;
|
||||
}
|
||||
}
|
||||
67
src/edu/utc/gamedev/Tile/Surface/TileWell.java
Normal file
67
src/edu/utc/gamedev/Tile/Surface/TileWell.java
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
package edu.utc.gamedev.Tile.Surface;
|
||||
|
||||
import edu.utc.gamedev.Tile.ITile;
|
||||
import edu.utc.gamedev.Util.Helper;
|
||||
import org.lwjgl.opengl.Display;
|
||||
import org.lwjgl.opengl.GL11;
|
||||
import org.lwjgl.util.Rectangle;
|
||||
import org.newdawn.slick.opengl.Texture;
|
||||
|
||||
public class TileWell implements ITile
|
||||
{
|
||||
private Rectangle box;
|
||||
private Texture sprite;
|
||||
|
||||
public TileWell()
|
||||
{
|
||||
sprite = Helper.loadTexture("environment/well");
|
||||
box = new Rectangle(0, 0, sprite.getTextureWidth(), sprite.getTextureHeight());
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void draw()
|
||||
{
|
||||
if (box.getX() != 32)
|
||||
{
|
||||
box.setX(32);
|
||||
}
|
||||
if (box.getY() != (Display.getHeight() - box.getHeight()))
|
||||
{
|
||||
box.setY(Display.getHeight() - box.getHeight());
|
||||
}
|
||||
|
||||
float x = (float) box.getX();
|
||||
float y = (float) box.getY();
|
||||
float w = (float) box.getWidth();
|
||||
float h = (float) box.getHeight();
|
||||
|
||||
GL11.glBindTexture(GL11.GL_TEXTURE_2D, sprite.getTextureID());
|
||||
|
||||
GL11.glColor3f(255F, 255F, 255F);
|
||||
|
||||
GL11.glBegin(GL11.GL_QUADS);
|
||||
|
||||
GL11.glTexCoord2f(0F, 0F);
|
||||
GL11.glVertex2f(x, y);
|
||||
|
||||
GL11.glTexCoord2f(1F, 0F);
|
||||
GL11.glVertex2f(x + w, y);
|
||||
|
||||
GL11.glTexCoord2f(1F, 1F);
|
||||
GL11.glVertex2f(x + w, y + h);
|
||||
|
||||
GL11.glTexCoord2f(0F, 1F);
|
||||
GL11.glVertex2f(x, y + h);
|
||||
|
||||
GL11.glEnd();
|
||||
|
||||
GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Rectangle getHitBox()
|
||||
{
|
||||
return this.box;
|
||||
}
|
||||
}
|
||||
105
src/edu/utc/gamedev/Tile/TileBackground.java
Normal file
105
src/edu/utc/gamedev/Tile/TileBackground.java
Normal file
|
|
@ -0,0 +1,105 @@
|
|||
package edu.utc.gamedev.Tile;
|
||||
|
||||
import edu.utc.gamedev.Util.Helper;
|
||||
import org.lwjgl.opengl.Display;
|
||||
import org.lwjgl.opengl.GL11;
|
||||
import org.lwjgl.util.Color;
|
||||
import org.lwjgl.util.Rectangle;
|
||||
import org.newdawn.slick.opengl.Texture;
|
||||
import org.newdawn.slick.opengl.TextureLoader;
|
||||
import org.newdawn.slick.util.ResourceLoader;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
public class TileBackground implements ITile
|
||||
{
|
||||
private Texture sprite;
|
||||
private Rectangle box;
|
||||
private Color color;
|
||||
|
||||
public TileBackground(String spriteName)
|
||||
{
|
||||
sprite = Helper.loadTexture("environment/" + spriteName);
|
||||
this.box = new Rectangle(0, 0, sprite.getImageWidth(), sprite.getImageHeight());
|
||||
this.color = new Color(255, 255, 255);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void draw()
|
||||
{
|
||||
this.box.setWidth(normalizeDimension(Display.getWidth()));
|
||||
this.box.setHeight(box.getWidth());
|
||||
|
||||
float x = (float) box.getX();
|
||||
float y = (float) box.getY();
|
||||
float w = (float) box.getWidth();
|
||||
float h = (float) box.getHeight();
|
||||
|
||||
GL11.glBindTexture(GL11.GL_TEXTURE_2D, sprite.getTextureID());
|
||||
GL11.glColor3f(color.getRed() / 255F, (float) color.getGreen() / 255F, (float) color.getBlue() / 255F);
|
||||
|
||||
// Allows the background to scale w/o blurring
|
||||
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST);
|
||||
|
||||
GL11.glBegin(GL11.GL_QUADS);
|
||||
|
||||
GL11.glTexCoord2f(0F, 0F);
|
||||
GL11.glVertex2f(x, y);
|
||||
|
||||
GL11.glTexCoord2f(1F, 0F);
|
||||
GL11.glVertex2f(x + w, y);
|
||||
|
||||
GL11.glTexCoord2f(1F, 1F);
|
||||
GL11.glVertex2f(x + w, y + h);
|
||||
|
||||
GL11.glTexCoord2f(0F, 1F);
|
||||
GL11.glVertex2f(x, y + h);
|
||||
|
||||
GL11.glEnd();
|
||||
|
||||
GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);
|
||||
}
|
||||
|
||||
public void setSprite(String spriteName)
|
||||
{
|
||||
Texture temp = sprite;
|
||||
try
|
||||
{
|
||||
temp = TextureLoader.getTexture("png", ResourceLoader.getResourceAsStream("assets/textures/entity/".replace("/", File.separator) + spriteName + ".png"));
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
System.err.println("Image not found, not changing");
|
||||
}
|
||||
sprite = temp;
|
||||
}
|
||||
|
||||
public int normalizeDimension(float dirty)
|
||||
{
|
||||
int i = 0;
|
||||
while (Math.pow(2, i) < dirty)
|
||||
{
|
||||
i++;
|
||||
}
|
||||
return (int) Math.pow(2, i);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Rectangle getHitBox()
|
||||
{
|
||||
return this.box;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param c the color overlay to apply to the background
|
||||
* <p>
|
||||
* Changes the background GL color to the specified color
|
||||
*/
|
||||
public TileBackground changeColor(Color c)
|
||||
{
|
||||
this.color = c;
|
||||
return this;
|
||||
}
|
||||
|
||||
}
|
||||
72
src/edu/utc/gamedev/Tile/TilePlatformBase.java
Normal file
72
src/edu/utc/gamedev/Tile/TilePlatformBase.java
Normal file
|
|
@ -0,0 +1,72 @@
|
|||
package edu.utc.gamedev.Tile;
|
||||
|
||||
import edu.utc.gamedev.Tile.Dungeon.TilePlatform;
|
||||
import org.lwjgl.opengl.Display;
|
||||
import org.lwjgl.opengl.GL11;
|
||||
import org.lwjgl.util.Rectangle;
|
||||
|
||||
public class TilePlatformBase extends TilePlatform
|
||||
{
|
||||
public Rectangle box;
|
||||
int w, h, x, y;
|
||||
|
||||
public TilePlatformBase()
|
||||
{
|
||||
super();
|
||||
|
||||
this.w = 96;
|
||||
this.h = 8;
|
||||
this.x = Display.getWidth() / 2 - (w / 2);
|
||||
this.y = Display.getHeight() - 96;
|
||||
this.box = new Rectangle(x, y, w, h);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Rectangle getHitBox()
|
||||
{
|
||||
return this.box;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update()
|
||||
{
|
||||
if (this.box.getX() != (Display.getWidth() / 2 - (w / 2)) || this.box.getY() != (Display.getHeight() - 96))
|
||||
{
|
||||
this.x = Display.getWidth() / 2 - (w / 2);
|
||||
this.y = Display.getHeight() - 96;
|
||||
this.box.setLocation(x, y);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void draw()
|
||||
{
|
||||
float x = (float) box.getX();
|
||||
float y = (float) box.getY();
|
||||
float w = (float) box.getWidth();
|
||||
float h = (float) box.getHeight();
|
||||
|
||||
GL11.glColor3f(1F, 1F, 1F);
|
||||
|
||||
// Allows the background to scale w/o blurring
|
||||
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST);
|
||||
|
||||
GL11.glBegin(GL11.GL_QUADS);
|
||||
|
||||
GL11.glTexCoord2f(0F, 0F);
|
||||
GL11.glVertex2f(x, y);
|
||||
|
||||
GL11.glTexCoord2f(1F, 0F);
|
||||
GL11.glVertex2f(x + w, y);
|
||||
|
||||
GL11.glTexCoord2f(1F, 1F);
|
||||
GL11.glVertex2f(x + w, y + h);
|
||||
|
||||
GL11.glTexCoord2f(0F, 1F);
|
||||
GL11.glVertex2f(x, y + h);
|
||||
|
||||
GL11.glEnd();
|
||||
|
||||
GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);
|
||||
}
|
||||
}
|
||||
35
src/edu/utc/gamedev/Tile/TilePlatformGround.java
Normal file
35
src/edu/utc/gamedev/Tile/TilePlatformGround.java
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
package edu.utc.gamedev.Tile;
|
||||
|
||||
import edu.utc.gamedev.Tile.Dungeon.TilePlatform;
|
||||
import org.lwjgl.opengl.Display;
|
||||
import org.lwjgl.util.Rectangle;
|
||||
|
||||
public class TilePlatformGround extends TilePlatform
|
||||
{
|
||||
private Rectangle box;
|
||||
|
||||
public TilePlatformGround()
|
||||
{
|
||||
this.box = new Rectangle(0, Display.getHeight(), Display.getWidth(), 4);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update()
|
||||
{
|
||||
// Update the bounding box so that it stays aligned with the display as it moves / resizes
|
||||
this.box.setY(Display.getHeight());
|
||||
this.box.setWidth(Display.getWidth());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void draw()
|
||||
{
|
||||
// Nothing to do here; no sense in drawing the ground, honestly... we don't see it
|
||||
}
|
||||
|
||||
@Override
|
||||
public Rectangle getHitBox()
|
||||
{
|
||||
return this.box;
|
||||
}
|
||||
}
|
||||
81
src/edu/utc/gamedev/Util/Config.java
Normal file
81
src/edu/utc/gamedev/Util/Config.java
Normal file
|
|
@ -0,0 +1,81 @@
|
|||
package edu.utc.gamedev.Util;
|
||||
|
||||
/**
|
||||
* @author: Jose Stovall
|
||||
* <p>
|
||||
* Config files were learned on my own back when I was Googling stuff previously for my own personal projects
|
||||
* That being said: this file was written on my own, but how I learned it previous was via Google.
|
||||
*/
|
||||
|
||||
import java.io.*;
|
||||
import java.util.Properties;
|
||||
|
||||
public class Config
|
||||
{
|
||||
public int resolutionWidth;
|
||||
public int resolutionHeight;
|
||||
public int targetFPS;
|
||||
public float volume;
|
||||
|
||||
public Config()
|
||||
{
|
||||
loadConfig();
|
||||
}
|
||||
|
||||
public void loadConfig()
|
||||
{
|
||||
try
|
||||
{
|
||||
File configFile = new File("settings.properties");
|
||||
FileReader reader = new FileReader(configFile);
|
||||
Properties props = new Properties();
|
||||
props.load(reader);
|
||||
|
||||
String[] resolutions = props.getProperty("windowed_resolution", "800x600").split("x");
|
||||
this.resolutionWidth = Integer.parseInt(resolutions[0]);
|
||||
this.resolutionHeight = Integer.parseInt(resolutions[1]);
|
||||
this.targetFPS = Integer.parseInt(props.getProperty("target_FPS", "100"));
|
||||
this.volume = Integer.parseInt(props.getProperty("volume", "100")) / 100F; // divide by one hundred because users understand 0 -> 100 better than 0.0 -> 1.0
|
||||
|
||||
reader.close();
|
||||
}
|
||||
catch (FileNotFoundException fnfex)
|
||||
{
|
||||
try
|
||||
{
|
||||
createConfig();
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
System.out.println("Everything went wrong with the config. Do you have read/write perms for this folder?");
|
||||
}
|
||||
}
|
||||
// TODO: Properly handle these exceptions..
|
||||
catch (IOException ioEx)
|
||||
{
|
||||
}
|
||||
catch (NullPointerException npe)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
public void createConfig() throws IOException
|
||||
{
|
||||
File configFile = new File("settings.properties");
|
||||
|
||||
Properties props = new Properties();
|
||||
props.setProperty("windowed_resolution", "800x600");
|
||||
props.setProperty("target_FPS", "100");
|
||||
props.setProperty("volume", "100");
|
||||
|
||||
String[] resolutions = props.getProperty("windowed_resolution", "800x600").split("x");
|
||||
this.resolutionWidth = Integer.parseInt(resolutions[0]);
|
||||
this.resolutionHeight = Integer.parseInt(resolutions[1]);
|
||||
this.targetFPS = Integer.parseInt(props.getProperty("target_FPS", "100"));
|
||||
this.volume = Integer.parseInt(props.getProperty("volume", "100")) / 100F; // divide by one hundred because users understand 0->100 better than 0.0 to 1.0
|
||||
|
||||
FileWriter writer = new FileWriter(configFile);
|
||||
props.store(writer, "game settings");
|
||||
writer.close();
|
||||
}
|
||||
}
|
||||
171
src/edu/utc/gamedev/Util/FileUtils.java
Normal file
171
src/edu/utc/gamedev/Util/FileUtils.java
Normal file
|
|
@ -0,0 +1,171 @@
|
|||
package edu.utc.gamedev.Util;
|
||||
|
||||
|
||||
import java.io.*;
|
||||
import java.net.JarURLConnection;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
import java.util.Enumeration;
|
||||
import java.util.jar.JarEntry;
|
||||
import java.util.jar.JarFile;
|
||||
|
||||
/**
|
||||
* SOURCE: https://stackoverflow.com/a/3348150
|
||||
*/
|
||||
public class FileUtils
|
||||
{
|
||||
public static boolean copyFile(final File toCopy, final File destFile)
|
||||
{
|
||||
try
|
||||
{
|
||||
return FileUtils.copyStream(new FileInputStream(toCopy), new FileOutputStream(destFile));
|
||||
}
|
||||
catch (final FileNotFoundException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private static boolean copyFilesRecusively(final File toCopy, final File destDir)
|
||||
{
|
||||
assert destDir.isDirectory();
|
||||
|
||||
if (!toCopy.isDirectory())
|
||||
{
|
||||
return FileUtils.copyFile(toCopy, new File(destDir, toCopy.getName()));
|
||||
}
|
||||
else
|
||||
{
|
||||
final File newDestDir = new File(destDir, toCopy.getName());
|
||||
if (!newDestDir.exists() && !newDestDir.mkdir())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
for (final File child : toCopy.listFiles())
|
||||
{
|
||||
if (!FileUtils.copyFilesRecusively(child, newDestDir))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static boolean copyJarResourcesRecursively(final File destDir, final JarURLConnection jarConnection) throws IOException
|
||||
{
|
||||
|
||||
final JarFile jarFile = jarConnection.getJarFile();
|
||||
|
||||
for (final Enumeration<JarEntry> e = jarFile.entries(); e.hasMoreElements(); )
|
||||
{
|
||||
final JarEntry entry = e.nextElement();
|
||||
if (entry.getName().startsWith(jarConnection.getEntryName()))
|
||||
{
|
||||
final String filename = removeStart(entry.getName(), //
|
||||
jarConnection.getEntryName());
|
||||
|
||||
final File f = new File(destDir, filename);
|
||||
if (!entry.isDirectory())
|
||||
{
|
||||
final InputStream entryInputStream = jarFile.getInputStream(entry);
|
||||
if (!FileUtils.copyStream(entryInputStream, f))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
entryInputStream.close();
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!FileUtils.ensureDirectoryExists(f))
|
||||
{
|
||||
throw new IOException("Could not create directory: " + f.getAbsolutePath());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static boolean copyResourcesRecursively(final URL originUrl, final File destination)
|
||||
{
|
||||
try
|
||||
{
|
||||
final URLConnection urlConnection = originUrl.openConnection();
|
||||
if (urlConnection instanceof JarURLConnection)
|
||||
{
|
||||
return FileUtils.copyJarResourcesRecursively(destination,
|
||||
(JarURLConnection) urlConnection);
|
||||
}
|
||||
else
|
||||
{
|
||||
return FileUtils.copyFilesRecusively(new File(originUrl.getPath()),
|
||||
destination);
|
||||
}
|
||||
}
|
||||
catch (final IOException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static boolean copyStream(final InputStream is, final File f)
|
||||
{
|
||||
try
|
||||
{
|
||||
return FileUtils.copyStream(is, new FileOutputStream(f));
|
||||
}
|
||||
catch (final FileNotFoundException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private static boolean copyStream(final InputStream is, final OutputStream os)
|
||||
{
|
||||
try
|
||||
{
|
||||
final byte[] buf = new byte[1024];
|
||||
|
||||
int len;
|
||||
while ((len = is.read(buf)) > 0)
|
||||
{
|
||||
os.write(buf, 0, len);
|
||||
}
|
||||
is.close();
|
||||
os.close();
|
||||
return true;
|
||||
}
|
||||
catch (final IOException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private static boolean ensureDirectoryExists(final File f)
|
||||
{
|
||||
return f.exists() || f.mkdir();
|
||||
}
|
||||
|
||||
public static String removeStart(String str, String remove)
|
||||
{
|
||||
if (isEmpty(str) || isEmpty(remove))
|
||||
{
|
||||
return str;
|
||||
}
|
||||
if (str.startsWith(remove))
|
||||
{
|
||||
return str.substring(remove.length());
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
||||
public static boolean isEmpty(CharSequence cs)
|
||||
{
|
||||
return cs == null || cs.length() == 0;
|
||||
}
|
||||
}
|
||||
50
src/edu/utc/gamedev/Util/Helper.java
Normal file
50
src/edu/utc/gamedev/Util/Helper.java
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
package edu.utc.gamedev.Util;
|
||||
|
||||
import org.lwjgl.Sys;
|
||||
import org.lwjgl.util.Color;
|
||||
import org.newdawn.slick.opengl.Texture;
|
||||
import org.newdawn.slick.opengl.TextureLoader;
|
||||
import org.newdawn.slick.util.ResourceLoader;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.Random;
|
||||
|
||||
public class Helper
|
||||
{
|
||||
public static long getTime()
|
||||
{
|
||||
return (Sys.getTime() * 1000) / Sys.getTimerResolution();
|
||||
}
|
||||
|
||||
public static String errTexture()
|
||||
{
|
||||
return "assets/textures/err.png".replace("/", File.separator);
|
||||
}
|
||||
|
||||
public static Color getRandomColor()
|
||||
{
|
||||
Random rand = new Random();
|
||||
return new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256));
|
||||
}
|
||||
|
||||
public static Texture loadTexture(String str)
|
||||
{
|
||||
String location = ("assets/textures/" + str + ".png").replace("/", File.separator);
|
||||
try
|
||||
{
|
||||
return TextureLoader.getTexture("png", ResourceLoader.getResourceAsStream(location));
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
try
|
||||
{
|
||||
return TextureLoader.getTexture("png", ResourceLoader.getResourceAsStream(errTexture()));
|
||||
}
|
||||
catch (IOException f)
|
||||
{
|
||||
throw new RuntimeException(f);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue