initial commit

This commit is contained in:
Mitch Weaver
2017-11-26 03:00:36 +01:00
parent 8eb120ad0d
commit 464bda0b44
60 changed files with 1617 additions and 0 deletions

16
src/core/Console.java Executable file
View File

@@ -0,0 +1,16 @@
package core;
import sjgs.core.DeveloperConsole;
import sjgs.core.Engine;
public class Console extends DeveloperConsole {
public Console(final Engine engine) { super(engine); }
@Override
protected void commands(final String action, final String item, final String value) {
}
}

62
src/core/Main.java Executable file
View File

@@ -0,0 +1,62 @@
package core;
import static sjgs.utils.Utils.pythagoras;
import java.awt.Graphics2D;
import core.input.KeyInput;
import core.input.MouseInput;
import jython.Jython;
import player.Player;
import sjgs.core.Camera;
import sjgs.core.DeveloperConsole;
import sjgs.core.Engine;
import sjgs.core.Handler;
import sjgs.physics.Physics;
import textures.TextureLoader;
import worldgen.WorldGenerator;
public final class Main extends Engine {
public Main(int WIDTH, int HEIGHT, String title) {
super(WIDTH, HEIGHT, title);
}
public static final int TILE_SIZE = 6;
public static final float TILE_DIAGONAL = pythagoras(TILE_SIZE, TILE_SIZE);
public static final double SCALE_FACTOR = 8d;
public static final float GRAVITY = 0.25f, TERIMNAL_VELOCITY = 2f, FRICTION = 0.1175f;
public static Main engine;
public static Camera camera;
public static Player player;
public static void main(final String[] args) {
engine = new Main(1280, 720, "Forlorn Hope");
}
@Override
protected void init() {
new Jython().init();
camera = new Camera(this);
new MouseInput(this);
Physics.init(GRAVITY, TERIMNAL_VELOCITY, FRICTION);
setScaleFactor(SCALE_FACTOR);
new Console(this);
TextureLoader.getTextures();
WorldGenerator.generateWorld();
}
@Override
protected void render(final Graphics2D g2d) {
WorldGenerator.currentBackground.render(g2d, camera, getScaleFactor());
Handler.render(g2d, camera, getScaleFactor());
}
@Override
protected void tick() {
if(DeveloperConsole.CONSOLE_OPEN) return;
camera.tick(player.getCenter(), getScaleFactor());
Handler.tick(camera, getScaleFactor());
KeyInput.tick(player);
}
}

34
src/core/input/KeyInput.java Executable file
View File

@@ -0,0 +1,34 @@
package core.input;
import static sjgs.utils.Utils.exit;
import player.Player;
import player.gun.PlayerGun;
import sjgs.core.input.Keyboard;
import sjgs.enums.Facing;
import worldgen.WorldGenerator;
public class KeyInput implements Keyboard {
public static void tick(final Player player) {
if(Keyboard.D()) { if(!player.getProne()) player.setVelX(Player.WALKING_SPEED); player.setFacing(Facing.RIGHT); }
else if(Keyboard.A()) { if(!player.getProne()) player.setVelX(-Player.WALKING_SPEED); player.setFacing(Facing.LEFT); }
if(Keyboard.S()) {
player.setProne(true);
player.setLookingUp(false);
} else player.setProne(false);
if(Keyboard.W()) { player.setLookingUp(true); player.setProne(false); }
else player.setLookingUp(false);
if(Keyboard.SPACE() && !player.getJumping() && !player.getFalling() && player.getVelY() >= 0) { player.jump(); }
if(Keyboard.F()) PlayerGun.shoot(player);
if(Keyboard.R()) WorldGenerator.generateWorld();
if(Keyboard.Q()) exit();
}
}

38
src/core/input/MouseInput.java Executable file
View File

@@ -0,0 +1,38 @@
package core.input;
import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelEvent;
import core.Main;
import player.gun.PlayerGun;
import sjgs.core.Engine;
import sjgs.core.input.Mouse;
public class MouseInput extends Mouse {
private final Engine engine;
public MouseInput(final Engine engine) { super(engine); this.engine = engine; }
@Override
public void mouseClicked(final MouseEvent e) {
Main.player.setHealth(Main.player.getHealth() - 1);
}
@Override
public void mouseMoved(final MouseEvent e) {}
@Override
public void mousePressed(final MouseEvent e) {
if(e.getButton() == 1) PlayerGun.shoot(Main.player);
}
@Override
public void mouseReleased(final MouseEvent e) {}
@Override
public void mouseWheelMoved(final MouseWheelEvent e) {
// engine.setScaleFactor(engine.getScaleFactor() + -e.getPreciseWheelRotation() / 5f);
// engine.setScaleFactor(clamp(engine.getScaleFactor(), 0.1f, 15f));
}
}

40
src/jython/Jython.java Executable file
View File

@@ -0,0 +1,40 @@
package jython;
import static sjgs.core.jython.Jython.pi;
import static sjgs.utils.Utils.readTextFileAsString;
import mobs.scraveller.Scraveller;
import player.Player;
import player.gun.PlayerGun;
public class Jython {
public void init() {
imports();
loadMethodsIntoInterpretor();
createPyFuncs();
}
// IMPORTS ALL NECESSARY JAVA CLASSES FOR INTERP
private static void imports() {
pi.exec(readTextFileAsString("/jython/forlorn_hope_jython_imports.py"));
}
// RUNS THE MODULES THROUGH THE INTERP TO LOAD THE NAMES OF FUNCS
private static void loadMethodsIntoInterpretor() {
// Player scripts
pi.exec(readTextFileAsString("/player/gun/player_gun.py"));
pi.exec(readTextFileAsString("/player/player_animations.py"));
// MOB AI
pi.exec(readTextFileAsString("/mobs/scraveller/scraveller_ai.py"));
}
private static void createPyFuncs() {
// PlayerScripts
PlayerGun.initPyFuncs();
Player.createPyFuncs();
// MOBS
Scraveller.createPyFuncs();
}
}

View File

@@ -0,0 +1,31 @@
from core.input import MouseInput
from core import Main
from ui.inventory.ItemIDs import *
from player import Player
from player.gun.PlayerBulletDamages import *
from textures.player import PlayerTextures
from player.gun.bullets import PlayerBullet
from player.gun.bullets import DefaultBullet
from player.gun import PlayerGun
from tiles import Tile
from worldgen import WorldGenerator
### MOBS
import mobs.MobDamages
import mobs.MobHealths
import mobs.MobSpeeds
from mobs.MobDamages import *
from mobs.MobHealths import *
from mobs.MobSpeeds import *
from mobs.scraveller import Scraveller
## IMPORT JMP3
from jmp3 import MP3Player
from jmp3 import AdvancedMP3Player

8
src/mobs/MobDamages.java Executable file
View File

@@ -0,0 +1,8 @@
package mobs;
public interface MobDamages {
public static final float SCRAVELLER_DAMAGE = 5;
}

9
src/mobs/MobHealths.java Executable file
View File

@@ -0,0 +1,9 @@
package mobs;
public interface MobHealths {
public static final int SCRAVELLER_HEALTH = 6;
}

7
src/mobs/MobSpeeds.java Executable file
View File

@@ -0,0 +1,7 @@
package mobs;
public interface MobSpeeds {
public static final float SCRAVELLER_SPEED = 0.175f;
}

View File

@@ -0,0 +1,108 @@
package mobs.scraveller;
import static sjgs.core.jython.Jython.pi;
import static sjgs.utils.pyutils.PyUtils.java2py;
import java.awt.Graphics2D;
import org.python.core.PyBoolean;
import org.python.core.PyFloat;
import org.python.core.PyFunction;
import org.python.core.PyObject;
import core.Main;
import jmp3.MP3Player;
import mobs.MobDamages;
import mobs.MobHealths;
import mobs.MobSpeeds;
import sjgs.base_objects.Bullet;
import sjgs.base_objects.Mob;
import sjgs.graphics.Animation;
import sjgs.utils.data_structures.Stack;
import sjgs.utils.data_structures.shapes.Rectangle;
import textures.mobs.ScravellerTextures;
public class Scraveller extends Mob {
/******* SOUNDS ************************************/
public static final String deathSound = "/splat.mp3", walkSound = "???";
/***************************************************/
private static PyFloat speed = new PyFloat(MobSpeeds.SCRAVELLER_SPEED);
public static final int WIDTH = 4, HEIGHT = 4;
public Rectangle territory;
private final PyObject _territory;
public int pointsIndex;
public boolean clockWise;
/** @hitBullets: all the bullets that have already hit the mob as to not
* calc damage on them every tick if they are in the stack */
public final Stack<Bullet> hitBullets;
public Scraveller(final Rectangle territory, final boolean clockWise) {
super(territory.pos.x, territory.pos.y - HEIGHT, WIDTH, HEIGHT);
self = java2py(this);
move = ScravellerTextures.move.clone();
moveFlipped = ScravellerTextures.moveFlipped.clone();
rotatedRight = ScravellerTextures.rotatedRight;
rotatedLeft = ScravellerTextures.rotatedLeft;
this.territory = territory;
this.clockWise = clockWise;
health = MobHealths.SCRAVELLER_HEALTH;
damage = MobDamages.SCRAVELLER_DAMAGE;
hitBullets = new Stack<Bullet>();
_territory = java2py(territory);
}
@Override
protected void init() { }
@Override
public void tick() {
ai.__call__(self, _territory, speed, new PyBoolean(clockWise));
scravellerCollision.__call__(self);
updatePosition();
runAnimations.__call__(self);
checkIfAlive();
}
@Override
public void render(final Graphics2D g2d) {
final float vx = getVelX();
final float vy = getVelY();
if(clockWise) {
if(vx < 0) moveFlipped.drawAnimation(g2d, getBounds());
else if(vx > 0) move.drawAnimation(g2d, getBounds());
else if(vy > 0) rotatedRight.drawAnimation(g2d, getBounds());
else if(vy < 0) rotatedLeft.drawAnimation(g2d, getBounds());
} else {
if(vx < 0) move.drawAnimation(g2d, getBounds());
else if(vx > 0) moveFlipped.drawAnimation(g2d, getBounds());
else if(vy > 0) rotatedLeft.drawAnimation(g2d, getBounds());
else if(vy < 0) rotatedRight.drawAnimation(g2d, getBounds());
}
// g2d.setColor(red);
// territory.draw(g2d);
}
@Override
protected void destroy() {
hitBullets.clear();
removeFromHandler();
final float distance = getCenter().distance(Main.player.getCenter());
final float gain = -(distance / Main.TILE_DIAGONAL)*3;
if (gain > -65) MP3Player.play(deathSound);
}
// --------------------------------- ANIMATIONS ------------------------------------------------------------------------------------------------- //
public final Animation move, moveFlipped, rotatedRight, rotatedLeft;
// ------------------------------------ PYTHON -------------------------------------------------------------------------------------------------- //
private static PyFunction ai, runAnimations, scravellerCollision; private final PyObject self;
public static void createPyFuncs() {
ai = pi.get("mob_travel_around_rectangle_ai", PyFunction.class);
runAnimations = pi.get("scraveller_run_animations", PyFunction.class);
scravellerCollision = pi.get("scraveller_collision", PyFunction.class);
}
public void reverseDirection() { clockWise = !clockWise; }
public void setClockwise(final boolean clockWise) { this.clockWise = clockWise; }
}

View File

@@ -0,0 +1,42 @@
def scraveller_collision(s):
response = s.getCollidedObjects()
bullets = response.collided_bullets;
for b in bullets:
if not s.hitBullets.contains(b):
s.hitBullets.add(b)
# IF IS A PLAYER BULLET
if isinstance(b, PlayerBullet):
# IF IS THE DEFAULT BULLET
if isinstance(b, DefaultBullet):
s.setHealth(s.getHealth() - DEFAULT_GUN_DAMAGE)
players = response.collided_players;
bullets.clear()
players.clear()
response.discard()
#def scraveller_walk_sound(s):
#d = s.getCenter().distance(Main.player.getCenter())
#gain = -(d / Main.TILE_DIAGONAL)*3
#if gain < -60: return
#SoundPlayer.play(s.walkSound, gain)
def scraveller_run_animations(s):
vx = s.getVelX()
vy = s.getVelY()
if s.clockWise:
if vx < 0: s.moveFlipped.runAnimation()
elif vx > 0: s.move.runAnimation()
elif vy > 0: s.rotatedRight.runAnimation()
elif vy < 0: s.rotatedLeft.runAnimation()
else:
if vx < 0: s.move.runAnimation()
elif vx > 0: s.moveFlipped.runAnimation()
elif vy > 0: s.rotatedLeft.runAnimation()
elif vy < 0: s.rotatedRight.runAnimation()

62
src/player/Player.java Executable file
View File

@@ -0,0 +1,62 @@
package player;
import static sjgs.core.jython.Jython.pi;
import static sjgs.utils.pyutils.PyUtils.java2py;
import java.awt.Graphics2D;
import org.python.core.PyFunction;
import org.python.core.PyObject;
import core.Main;
import jmp3.MP3Player;
import sjgs.base_objects.PlayerBase;
import sjgs.physics.structs.CollisionResponse;
public class Player extends PlayerBase {
public static final float WALKING_SPEED = Main.TILE_SIZE/6, JUMPING_SPEED = -Main.TILE_SIZE/2;
public static final float WIDTH = Main.TILE_SIZE, HEIGHT = Main.TILE_SIZE;
private boolean prone, lookingUp;
private final PyObject self;
private final int playerMaxHealth;
public Player(final float x, final float y) {
super(x, y, WIDTH, HEIGHT);
self = java2py(this);
playerMaxHealth = 7;
setHealth(playerMaxHealth);
}
@Override
protected void init() {}
@Override
public void tick() {
final CollisionResponse response = applyPhysics();
response.discard();
manageJumping();
runAnimations.__call__(self);
}
@Override
public void render(final Graphics2D g2d) { PlayerRenderer.render(this, g2d); }
@Override
protected void destroy() { removeFromHandler(); }
// ------------------------- GETTERS AND SETTERS ------------------------------------------------------------------------------//
public boolean getProne() { return prone; }
public boolean getLookingUp() { return lookingUp; }
public void setProne(final boolean prone) { this.prone = prone; }
public void setLookingUp(final boolean lookingUp) { this.lookingUp = lookingUp; }
public PyObject getSelf() { return self; }
// ------------------------ END GETTERS AND SETTERS ---------------------------------------------------------------------------//
public void jump() {
setVelY(Player.JUMPING_SPEED); setJumping(true);
MP3Player.play("/jump_sound.mp3");
}
public float getPlayerMaxHealth() { return playerMaxHealth; }
private static PyFunction runAnimations;
public static void createPyFuncs() { runAnimations = pi.get("run_player_animations", PyFunction.class); }
}

177
src/player/PlayerRenderer.java Executable file
View File

@@ -0,0 +1,177 @@
package player;
import static textures.player.PlayerTextures.*;
import java.awt.Graphics2D;
class PlayerRenderer {
private static final float _white = 0.70f,
_gray = 0.50f,
_pale_green = 0.30f,
_green = 0.10f;
private final static int offset = 2; // NOTE YOU DO NEED THIS!
static void render(final Player player, final Graphics2D g2d) {
final boolean right = player.facingRight();
final boolean lookingUp = player.getLookingUp();
final boolean jumping = player.getJumping();
final boolean falling = player.getFalling();
final boolean prone = player.getProne();
final boolean stopped = player.stopped();
final boolean white = player.getHealth() >= player.getPlayerMaxHealth() * _white;
final boolean gray = player.getHealth() >= player.getPlayerMaxHealth() * _gray;
final boolean pale_green = player.getHealth() >= player.getPlayerMaxHealth() * _pale_green;
final boolean green = player.getHealth() >= player.getPlayerMaxHealth() * _green;
final int x = (int)player.getX();
final int y = (int)player.getY();
if(white) {
if(prone) {
if(falling || jumping) g2d.drawImage(right ? playerLookingDownRight : playerLookingDownLeft, x - offset, y - offset, null);
else g2d.drawImage(right ? playerProneRight : playerProneLeft, x - offset, y - offset, null);
} else if(!lookingUp) {
if(falling) g2d.drawImage(right ? playerFallingRight : playerFallingLeft, x - offset, y - offset, null);
else if(jumping) g2d.drawImage(right ? playerJumpingRight : playerJumpingLeft, x - offset, y - offset, null);
else if(stopped) {
if(right) playerIdleRight.drawAnimation(g2d, x - offset, y - offset);
else playerIdleLeft.drawAnimation(g2d, x - offset, y - offset);
} else {
if(right) playerWalkRight.drawAnimation(g2d, x - offset, y - offset);
else playerWalkLeft.drawAnimation(g2d, x - offset, y - offset);
}
} else {
if(falling) g2d.drawImage(right ? playerLookingUpFallingRight : playerLookingUpFallingLeft, x - offset, y - offset, null);
else if(jumping) g2d.drawImage(right ? playerLookingUpJumpingRight : playerLookingUpJumpingLeft, x - offset, y - offset, null);
else if(stopped) {
if(right) playerLookingUpIdleRight.drawAnimation(g2d, x - offset, y - offset);
else playerLookingUpIdleLeft.drawAnimation(g2d, x - offset, y - offset);
} else {
if(right) playerLookingUpRunningRight.drawAnimation(g2d, x - offset, y - offset);
else playerLookingUpRunningLeft.drawAnimation(g2d, x - offset, y - offset);
}
}
}
else if(gray) {
if(prone) {
if(falling || jumping) g2d.drawImage(right ? GRAYplayerLookingDownRight : GRAYplayerLookingDownLeft, x - offset, y - offset, null);
else g2d.drawImage(right ? GRAYplayerProneRight : GRAYplayerProneLeft, x - offset, y - offset, null);
} else if(!lookingUp) {
if(falling) g2d.drawImage(right ? GRAYplayerFallingRight : GRAYplayerFallingLeft, x - offset, y - offset, null);
else if(jumping) g2d.drawImage(right ? GRAYplayerJumpingRight : GRAYplayerJumpingLeft, x - offset, y - offset, null);
else if(stopped) {
if(right) GRAYplayerIdleRight.drawAnimation(g2d, x - offset, y - offset);
else GRAYplayerIdleLeft.drawAnimation(g2d, x - offset, y - offset);
} else {
if(right) GRAYplayerWalkRight.drawAnimation(g2d, x - offset, y - offset);
else GRAYplayerWalkLeft.drawAnimation(g2d, x - offset, y - offset);
}
} else {
if(falling) g2d.drawImage(right ? GRAYplayerLookingUpFallingRight : GRAYplayerLookingUpFallingLeft, x - offset, y - offset, null);
else if(jumping) g2d.drawImage(right ? GRAYplayerLookingUpJumpingRight : GRAYplayerLookingUpJumpingLeft, x - offset, y - offset, null);
else if(stopped) {
if(right) GRAYplayerLookingUpIdleRight.drawAnimation(g2d, x - offset, y - offset);
else GRAYplayerLookingUpIdleLeft.drawAnimation(g2d, x - offset, y - offset);
} else {
if(right) GRAYplayerLookingUpRunningRight.drawAnimation(g2d, x - offset, y - offset);
else GRAYplayerLookingUpRunningLeft.drawAnimation(g2d, x - offset, y - offset);
}
}
}
else if(pale_green) {
if(prone) {
if(falling || jumping) g2d.drawImage(right ? PALE_GREENplayerLookingDownRight : PALE_GREENplayerLookingDownLeft, x - offset, y - offset, null);
else g2d.drawImage(right ? PALE_GREENplayerProneRight : PALE_GREENplayerProneLeft, x - offset, y - offset, null);
} else if(!lookingUp) {
if(falling) g2d.drawImage(right ? PALE_GREENplayerFallingRight : PALE_GREENplayerFallingLeft, x - offset, y - offset, null);
else if(jumping) g2d.drawImage(right ? PALE_GREENplayerJumpingRight : PALE_GREENplayerJumpingLeft, x - offset, y - offset, null);
else if(stopped) {
if(right) PALE_GREENplayerIdleRight.drawAnimation(g2d, x - offset, y - offset);
else PALE_GREENplayerIdleLeft.drawAnimation(g2d, x - offset, y - offset);
} else {
if(right) PALE_GREENplayerWalkRight.drawAnimation(g2d, x - offset, y - offset);
else PALE_GREENplayerWalkLeft.drawAnimation(g2d, x - offset, y - offset);
}
} else {
if(falling) g2d.drawImage(right ? PALE_GREENplayerLookingUpFallingRight : PALE_GREENplayerLookingUpFallingLeft, x - offset, y - offset, null);
else if(jumping) g2d.drawImage(right ? PALE_GREENplayerLookingUpJumpingRight : PALE_GREENplayerLookingUpJumpingLeft, x - offset, y - offset, null);
else if(stopped) {
if(right) PALE_GREENplayerLookingUpIdleRight.drawAnimation(g2d, x - offset, y - offset);
else PALE_GREENplayerLookingUpIdleLeft.drawAnimation(g2d, x - offset, y - offset);
} else {
if(right) PALE_GREENplayerLookingUpRunningRight.drawAnimation(g2d, x - offset, y - offset);
else PALE_GREENplayerLookingUpRunningLeft.drawAnimation(g2d, x - offset, y - offset);
}
}
}
else if(green) {
if(prone) {
if(falling || jumping) g2d.drawImage(right ? GREENplayerLookingDownRight : GREENplayerLookingDownLeft, x - offset, y - offset, null);
else g2d.drawImage(right ? GREENplayerProneRight : GREENplayerProneLeft, x - offset, y - offset, null);
} else if(!lookingUp) {
if(falling) g2d.drawImage(right ? GREENplayerFallingRight : GREENplayerFallingLeft, x - offset, y - offset, null);
else if(jumping) g2d.drawImage(right ? GREENplayerJumpingRight : GREENplayerJumpingLeft, x - offset, y - offset, null);
else if(stopped) {
if(right) GREENplayerIdleRight.drawAnimation(g2d, x - offset, y - offset);
else GREENplayerIdleLeft.drawAnimation(g2d, x - offset, y - offset);
} else {
if(right) GREENplayerWalkRight.drawAnimation(g2d, x - offset, y - offset);
else GREENplayerWalkLeft.drawAnimation(g2d, x - offset, y - offset);
}
} else {
if(falling) g2d.drawImage(right ? GREENplayerLookingUpFallingRight : GREENplayerLookingUpFallingLeft, x - offset, y - offset, null);
else if(jumping) g2d.drawImage(right ? GREENplayerLookingUpJumpingRight : GREENplayerLookingUpJumpingLeft, x - offset, y - offset, null);
else if(stopped) {
if(right) GREENplayerLookingUpIdleRight.drawAnimation(g2d, x - offset, y - offset);
else GREENplayerLookingUpIdleLeft.drawAnimation(g2d, x - offset, y - offset);
} else {
if(right) GREENplayerLookingUpRunningRight.drawAnimation(g2d, x - offset, y - offset);
else GREENplayerLookingUpRunningLeft.drawAnimation(g2d, x - offset, y - offset);
}
}
}
// BROWN --- below 10% health
else {
if(prone) {
if(falling || jumping) g2d.drawImage(right ? BROWNplayerLookingDownRight : BROWNplayerLookingDownLeft, x - offset, y - offset, null);
else g2d.drawImage(right ? BROWNplayerProneRight : BROWNplayerProneLeft, x - offset, y - offset, null);
} else if(!lookingUp) {
if(falling) g2d.drawImage(right ? BROWNplayerFallingRight : BROWNplayerFallingLeft, x - offset, y - offset, null);
else if(jumping) g2d.drawImage(right ? BROWNplayerJumpingRight : BROWNplayerJumpingLeft, x - offset, y - offset, null);
else if(stopped) {
if(right) BROWNplayerIdleRight.drawAnimation(g2d, x - offset, y - offset);
else BROWNplayerIdleLeft.drawAnimation(g2d, x - offset, y - offset);
} else {
if(right) BROWNplayerWalkRight.drawAnimation(g2d, x - offset, y - offset);
else BROWNplayerWalkLeft.drawAnimation(g2d, x - offset, y - offset);
}
} else {
if(falling) g2d.drawImage(right ? BROWNplayerLookingUpFallingRight : BROWNplayerLookingUpFallingLeft, x - offset, y - offset, null);
else if(jumping) g2d.drawImage(right ? BROWNplayerLookingUpJumpingRight : BROWNplayerLookingUpJumpingLeft, x - offset, y - offset, null);
else if(stopped) {
if(right) BROWNplayerLookingUpIdleRight.drawAnimation(g2d, x - offset, y - offset);
else BROWNplayerLookingUpIdleLeft.drawAnimation(g2d, x - offset, y - offset);
} else {
if(right) BROWNplayerLookingUpRunningRight.drawAnimation(g2d, x - offset, y - offset);
else BROWNplayerLookingUpRunningLeft.drawAnimation(g2d, x - offset, y - offset);
}
}
}
}
}

View File

@@ -0,0 +1,8 @@
package player.gun;
public interface PlayerBulletDamages {
public static float DEFAULT_GUN_DAMAGE = 3;
}

28
src/player/gun/PlayerGun.java Executable file
View File

@@ -0,0 +1,28 @@
package player.gun;
import static sjgs.core.jython.Jython.pi;
import org.python.core.PyFunction;
import player.Player;
public class PlayerGun {
private static long then;
public static void shoot(final Player player) {
if(System.currentTimeMillis() - then > 175) {
shoot.__call__(player.getSelf());
then = System.currentTimeMillis();
}
/* uncomment for interactive scripting */
// pi.exec(readTextFileAsString("/player/gun/player_gun.py"));
// initPyFuncs();
}
private static PyFunction shoot;
public static void initPyFuncs() {
shoot = pi.get("player_shoot", PyFunction.class);
}
}

View File

@@ -0,0 +1,76 @@
package player.gun.bullets;
import static sjgs.utils.Utils.isPositive;
import java.awt.Graphics2D;
import core.Main;
import sjgs.enums.Facing;
import sjgs.graphics.Animation;
import sjgs.physics.structs.CollisionResponse;
import sjgs.utils.data_structures.vectors.Point2f;
import textures.player.PlayerGunTextures;
public class DefaultBullet extends PlayerBullet {
public static final String sound = "/tir.mp3";
private static final int w = 8, h = 1, health_timer = 18;
private final Animation animation;
private final Point2f p;
private final Facing facing;
/** @constructor: velX / velY are the player's velocity at initial shot */
public DefaultBullet(final Point2f p, final Facing facing, final float velX, final float velY) {
// note the +2 and +1, leave them!
super(p.x + 2, p.y + 1, facing == Facing.RIGHT || facing == Facing.LEFT ? w : h, facing == Facing.RIGHT || facing == Facing.LEFT ? h : w, velX, velY, 0, health_timer);
this.p = p; this.facing = facing;
switch(facing) {
case RIGHT: animation = PlayerGunTextures.default_gun_right.clone(); break;
case LEFT: animation = PlayerGunTextures.default_gun_left.clone(); break;
case ABOVE: animation = PlayerGunTextures.default_gun_up.clone(); break;
default: animation = PlayerGunTextures.default_gun_down.clone(); break; // (down)
}
}
@Override
protected void init() {}
@Override
public void tick() {
final CollisionResponse response = getCollidedObjects();
// do collision stuff here
response.discard();
animation.playAnimationOnce();
if(isMoving()) {
// if player changed directions since last shot, set the bullet velocity to zero
final float pvx = Main.player.getVelX(), pvy = Main.player.getVelY(), vx = getVelX(), vy = getVelY();
if(pvx == 0 || isPositive(vx) ^ isPositive(pvx)) setVelX(0);
if(pvy == 0 || isPositive(vy) ^ isPositive(pvy)) setVelY(0);
// update animation position
p.x += getVelX();
p.y += getVelY();
}
// NOTE YOU DO NEED THIS, it updates the bounds pos when running to match animation
updatePosition();
if(timer.tick()) destroy();
}
@Override
public void render(final Graphics2D g2d) {
if(animation != null) animation.drawAnimation(g2d, p);
// g2d.setColor(Color.blue);
// getFullBounds().draw(g2d);
}
@Override
protected void destroy() {
removeFromHandler();
}
}

View File

@@ -0,0 +1,21 @@
package player.gun.bullets;
import java.awt.Graphics2D;
import sjgs.base_objects.Bullet;
public abstract class PlayerBullet extends Bullet {
public PlayerBullet(final float x, final float y, final float w, final float h, final float velX, final float velY, final int elasticity, final int health_timer) {
super(x, y, w, h, velX, velY, elasticity, health_timer);
}
@Override
protected abstract void init();
@Override
public abstract void tick();
@Override
public abstract void render(Graphics2D g2d);
@Override
protected abstract void destroy();
}

136
src/player/gun/player_gun.py Executable file
View File

@@ -0,0 +1,136 @@
# DIFFERENT BULLETS:
DEFAULT_GUN = 1
import textures.player.PlayerTextures as PlayerTextures
# creates the bullet for gun to shoot
def player_shoot(player):
p = getPlayerGunPositionPoint(player)
dir = getPlayerBulletDirection(player)
b = whichPlayerBullet(player)
# create the bullet
if b == DEFAULT_GUN:
down_multiplier = 1/3
if dir == Facing.BELOW or dir == Facing.ABOVE: DefaultBullet(p, dir, 0, player.getVelY() * down_multiplier)
elif (dir == Facing.RIGHT or dir == Facing.LEFT) or dir == Facing.ABOVE and player.getVelX() != 0: DefaultBullet(p, dir, player.getVelX(), 0)
else: DefaultBullet(p, dir, 0, 0)
# play shooting sound
playerBulletSound(b)
# gets the position of the gun barrel location
# NOTE: use (int) as graphics cannot be floating points!
def getPlayerGunPositionPoint(player):
x = 0
y = 0
# get variables for clarity
right = player.getFacing() == Facing.RIGHT
left = not right
jumping = player.getJumping()
falling = player.getFalling()
up = player.getLookingUp()
down = player.getProne() and (player.getFalling() or player.getJumping())
moving = player.getVelX() != 0 or player.getVelY() != 0
prone = player.getProne() and not (player.getFalling() or player.getJumping()) and not moving
cx = int(player.getCenterX())
cy = int(player.getCenterY())
idle = not falling and not jumping and not up and not down and not prone and not moving
idleUp = not falling and not jumping and up and not down and not prone and not moving
running = not falling and not jumping and not up and not down and not prone and moving
runningUp = not falling and not jumping and up and not down and not prone and moving
jumping = (jumping or falling) and not up
jumpingUp = (jumping or falling or player.getVelY() < 0) and up
############ IF FACING RIGHT ############################################################################
if right:
if idle:
x = cx + 4
y = cy - 2
if PlayerTextures.playerIdleRight.currentFrame == PlayerTextures.playerIdleRight.size() - 1: y += 1
elif idleUp:
x = cx - 1
y = cy - 14
if PlayerTextures.playerLookingUpIdleRight.currentFrame == PlayerTextures.playerLookingUpIdleRight.size() - 1: y += 1
elif running:
x = cx + 4
y = cy - 2
if PlayerTextures.playerWalkRight.currentFrame % 2 == 0: y -= 1
if PlayerTextures.playerWalkRight.currentFrame == 1: x += 1
if PlayerTextures.playerWalkRight.currentFrame == 3: x -= 1
elif runningUp:
x = cx
y = cy - 14
if PlayerTextures.playerLookingUpRunningRight.currentFrame % 2 == 0: y -= 1
if PlayerTextures.playerLookingUpRunningRight.currentFrame == 1: x += 1
if PlayerTextures.playerLookingUpRunningRight.currentFrame == 3: x -= 1
elif prone:
x = cx + 5
y = cy
elif down:
x = cx - 2
y = cy + 5
elif jumping:
x = cx + 4
y = cy - 1
elif jumpingUp:
x = cx - 1
y = cy - 9
################## IF FACING LEFT #######################################################################
else:
if idle:
x = cx - 13
y = cy - 2
if PlayerTextures.playerIdleLeft.currentFrame == PlayerTextures.playerIdleLeft.size() - 1: y += 1
elif idleUp:
x = cx - 2
y = cy - 14
if PlayerTextures.playerLookingUpIdleLeft.currentFrame == PlayerTextures.playerLookingUpIdleLeft.size() - 1: y += 1
elif running:
x = cx - 13
y = cy - 2
if PlayerTextures.playerWalkLeft.currentFrame % 2 == 0: y -= 1
if PlayerTextures.playerWalkLeft.currentFrame == 1: x -= 1
if PlayerTextures.playerWalkLeft.currentFrame == 3: x += 1
elif runningUp:
x = cx - 2
y = cy - 14
if PlayerTextures.playerLookingUpRunningLeft.currentFrame % 2 == 0: y -= 1
if PlayerTextures.playerLookingUpRunningLeft.currentFrame == 1: x -= 1
if PlayerTextures.playerLookingUpRunningLeft.currentFrame == 3: x += 1
elif prone:
x = cx - 14
y = cy
elif down:
x = cx - 1
y = cy + 7
elif jumping:
x = cx - 12
y = cy - 1
elif jumpingUp:
x = cx - 2
y = cy - 9
##################################################################################################################
return Point2f(x, y)
def playerBulletSound(b):
if b == DEFAULT_GUN: MP3Player.play(DefaultBullet.sound)
def getPlayerBulletDirection(player):
right = player.getFacing() == Facing.RIGHT
left = not right
prone = player.getProne() and not (player.getFalling() or player.getJumping())
up = player.getLookingUp()
down = player.getProne() and (player.getFalling() or player.getJumping())
if right and not down and not up: return Facing.RIGHT
elif left and not down and not up: return Facing.LEFT
elif down: return Facing.BELOW
elif up: return Facing.ABOVE
# determines which kind of bullet to shoot
def whichPlayerBullet(player):
return DEFAULT_GUN

60
src/player/player_animations.py Executable file
View File

@@ -0,0 +1,60 @@
### NOTE we run them all here as its the easiest way to get them sync'd up, and
# with my animation implementation its only incrementing an int, so its not resource intensive.
def run_player_animations(p):
# IF RUNNING NORMALLY
if not p.getProne() and not p.getFalling() and not p.getLookingUp() and not p.stopped():
if p.facingRight():
PlayerTextures.playerWalkRight.runAnimation()
PlayerTextures.GRAYplayerWalkRight.runAnimation()
PlayerTextures.PALE_GREENplayerWalkRight.runAnimation()
PlayerTextures.GREENplayerWalkRight.runAnimation()
PlayerTextures.BROWNplayerWalkRight.runAnimation()
else:
PlayerTextures.playerWalkLeft.runAnimation()
PlayerTextures.GRAYplayerWalkLeft.runAnimation()
PlayerTextures.PALE_GREENplayerWalkLeft.runAnimation()
PlayerTextures.GREENplayerWalkLeft.runAnimation()
PlayerTextures.BROWNplayerWalkLeft.runAnimation()
# IF RUNNING LOOKING UP
elif not p.getProne() and not p.getFalling() and not p.stopped():
if p.facingRight():
PlayerTextures.playerLookingUpRunningRight.runAnimation()
PlayerTextures.GRAYplayerLookingUpRunningRight.runAnimation()
PlayerTextures.PALE_GREENplayerLookingUpRunningRight.runAnimation()
PlayerTextures.GREENplayerLookingUpRunningRight.runAnimation()
PlayerTextures.BROWNplayerLookingUpRunningRight.runAnimation()
else:
PlayerTextures.playerLookingUpRunningLeft.runAnimation()
PlayerTextures.GRAYplayerLookingUpRunningLeft.runAnimation()
PlayerTextures.PALE_GREENplayerLookingUpRunningLeft.runAnimation()
PlayerTextures.GREENplayerLookingUpRunningLeft.runAnimation()
PlayerTextures.BROWNplayerLookingUpRunningLeft.runAnimation()
# IF IDLE NORMAL
elif not p.getProne() and not p.getFalling() and not p.getLookingUp():
if p.facingRight():
PlayerTextures.playerIdleRight.runAnimation()
PlayerTextures.GRAYplayerIdleRight.runAnimation()
PlayerTextures.PALE_GREENplayerIdleRight.runAnimation()
PlayerTextures.GREENplayerIdleRight.runAnimation()
PlayerTextures.BROWNplayerIdleRight.runAnimation()
else:
PlayerTextures.playerIdleLeft.runAnimation()
PlayerTextures.GRAYplayerIdleLeft.runAnimation()
PlayerTextures.PALE_GREENplayerIdleLeft.runAnimation()
PlayerTextures.GREENplayerIdleLeft.runAnimation()
PlayerTextures.BROWNplayerIdleLeft.runAnimation()
# IF IDLE LOOKING UP
elif not p.getProne() and not p.getFalling() and p.stopped() and p.getLookingUp():
if p.facingRight():
PlayerTextures.playerLookingUpIdleRight.runAnimation()
PlayerTextures.GRAYplayerLookingUpIdleRight.runAnimation()
PlayerTextures.PALE_GREENplayerLookingUpIdleRight.runAnimation()
PlayerTextures.GREENplayerLookingUpIdleRight.runAnimation()
PlayerTextures.BROWNplayerLookingUpIdleRight.runAnimation()
else:
PlayerTextures.playerLookingUpIdleLeft.runAnimation()
PlayerTextures.GRAYplayerLookingUpIdleLeft.runAnimation()
PlayerTextures.PALE_GREENplayerLookingUpIdleLeft.runAnimation()
PlayerTextures.GREENplayerLookingUpIdleLeft.runAnimation()
PlayerTextures.BROWNplayerLookingUpIdleLeft.runAnimation()

31
src/tiles/Tile.java Executable file
View File

@@ -0,0 +1,31 @@
package tiles;
import java.awt.Graphics2D;
import sjgs.base_objects.BaseTile;
import textures.tiles.TileTextures;
public class Tile extends BaseTile {
public Tile(final int x, final int y, final int w, final int h, final float startingHeightPercentage, final float endingHeightPercentage) {
super(x, y, w, h, startingHeightPercentage, endingHeightPercentage);
}
// no args = not angled
public Tile(final int x, final int y, final int w, final int h) { super(x, y, w, h, 0, 0); }
@Override
public void init() {}
@Override
public void tick() {}
@Override
public void render(final Graphics2D g2d) {
g2d.drawImage(TileTextures.tile1, (int)getX(), (int)getY(), null); // NOTE YOU NEED THE +2!!!
}
@Override
public void destroy() { removeFromHandler(); }
}

3
src/ui/HUD.java Executable file
View File

@@ -0,0 +1,3 @@
package ui;
public class HUD {}

48
src/ui/inventory/Inventory.java Executable file
View File

@@ -0,0 +1,48 @@
package ui.inventory;
import java.awt.Graphics2D;
import sjgs.graphics.ui.InventorySystem;
import sjgs.graphics.ui.InventorySystemSlot;
public class Inventory extends InventorySystem {
public Inventory(final float x, final float y, final float width, final float height) {
super(x, y, width, height);
}
@Override
public void init() {
}
@Override
public void onLeftClick() {
baseOnLeftClick();
}
@Override
public int getStackableAmount(final int itemId) { return 0; }
@Override
public boolean isStackable(final int itemId) { return false; }
@Override
public void render(final Graphics2D g2d) {
for(final InventorySystemSlot slot : getSlots()) slot.render(g2d);
}
@Override
public void tick() {
for(final InventorySystemSlot slot : getSlots()) slot.tick();
}
@Override
public void swapSlots(final InventorySystemSlot a, final InventorySystemSlot b) {
baseSwapSlots(a, b);
}
@Override
public void destroy() {}
}

View File

@@ -0,0 +1,35 @@
package ui.inventory;
import java.awt.Graphics2D;
import sjgs.graphics.ui.InventorySystemSlot;
public class InventorySlot extends InventorySystemSlot {
public InventorySlot(final float x, final float y, final float w, final float h, final int slotNumber) {
super(x, y, w, h, slotNumber);
}
@Override
public void init() {
}
@Override
public void render(final Graphics2D g2d) {
}
@Override
public void reset() {
baseReset();
}
@Override
public void tick() {
}
}

17
src/ui/inventory/ItemIDs.java Executable file
View File

@@ -0,0 +1,17 @@
package ui.inventory;
public interface ItemIDs {
public static final int UPGRADED_DEFAULT_GUN = 1,
DOUBLE_JUMP = 2,
NIGHT_VISION = 3,
DASH_BOOSTER = 4,
LAVA_SUIT = 5,
WATER_SUIT = 6,
MAP = 7,
DEFAULT_GUN = 8;
}

View File

@@ -0,0 +1,59 @@
package worldgen;
import static sjgs.utils.Utils.COIN_FLIP;
import static sjgs.utils.Utils.loadImage;
import static sjgs.utils.Utils.unpackBlue;
import static sjgs.utils.Utils.unpackGreen;
import static sjgs.utils.Utils.unpackRed;
import java.awt.image.BufferedImage;
import core.Main;
import mobs.scraveller.Scraveller;
import sjgs.utils.data_structures.shapes.Rectangle;
import sjgs.utils.data_structures.vectors.Point2f;
import sjgs.utils.multithreading.Runner;
import tiles.Tile;
public class TestMapCreator {
public static void createTestMap() {
final int TILE_SIZE = Main.TILE_SIZE;
final BufferedImage map = loadImage("/map.png");
Main.camera.setLimitsWithRect(0, 0, map.getWidth()*6, map.getHeight()*6);
for (int i = 0; i < map.getHeight(); i++) {
for (int j = 0; j < map.getWidth(); j++) {
final int col = i, row = j;
new Runner(() -> {
final int pixel = map.getRGB(col, row);
final int r = unpackRed(pixel);
final int g = unpackGreen(pixel);
final int b = unpackBlue(pixel);
if(r == 64 && g == 64 && b == 64) new Tile(col * TILE_SIZE, row * TILE_SIZE, TILE_SIZE, TILE_SIZE);
else if(r == 0 && g == 0 && b == 255) Main.player.setLocation(col * TILE_SIZE, row * TILE_SIZE);
else if(r == 255 && g == 0 && b == 0) {
new Tile(col * TILE_SIZE, row * TILE_SIZE, TILE_SIZE, TILE_SIZE);
new Scraveller( new Rectangle( new Point2f(col * TILE_SIZE, row * TILE_SIZE), 2*TILE_SIZE, TILE_SIZE ), COIN_FLIP() );
}
else if(r == 255 && g == 255 && b == 255) {
new Tile(col * TILE_SIZE, row * TILE_SIZE, TILE_SIZE, TILE_SIZE);
new Scraveller( new Rectangle( new Point2f(col * TILE_SIZE, row * TILE_SIZE), 3*TILE_SIZE, TILE_SIZE ), COIN_FLIP() );
}
else if(r == 255 && g == 200 && b == 0) {
new Tile(col * TILE_SIZE, row * TILE_SIZE, TILE_SIZE, TILE_SIZE);
new Scraveller( new Rectangle( new Point2f(col * TILE_SIZE, row * TILE_SIZE), TILE_SIZE, TILE_SIZE ), COIN_FLIP() );
}
} ).run();
}
}
}
}

View File

@@ -0,0 +1,36 @@
package worldgen;
import static sjgs.utils.Utils.loadImage;
import core.Main;
import player.Player;
import sjgs.core.Handler;
import sjgs.graphics.backgrounds.Background;
import sjgs.graphics.backgrounds.ParallaxBackground;
public class WorldGenerator {
public static ParallaxBackground currentBackground;
public static void generateWorld() {
Handler.clearAll();
Main.player = new Player(0, 0);
TestMapCreator.createTestMap();
final int startX = 0, startY = 0;
final Background background = new Background(loadImage("/background.png"), startX, startY, 100, 0, true);
final Background midground = new Background(loadImage("/midground.png"), startX, startY, 50, 100, true);
final Background foreground = new Background(loadImage("/foreground.png"), startX, startY, 80, 0, true);
currentBackground = new ParallaxBackground(background, midground, foreground);
// currentBackground = new ParallaxBackground(midground);
}
}