Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Requires only the lwjgl.jar, the natives folder packaged with it in the official release 2.8.4 */
- /* and the TWL's PNGDecoder, remaining dependancies can be acquired by installing the actual game */
- /* and extracting the images used by the game */
- package ld25.lechasseur;
- import java.awt.image.BufferedImage;
- import java.io.File;
- import java.io.IOException;
- import java.io.InputStream;
- import java.nio.ByteBuffer;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.Random;
- import javax.imageio.ImageIO;
- import org.lwjgl.BufferUtils;
- import org.lwjgl.LWJGLException;
- import org.lwjgl.LWJGLUtil;
- import org.lwjgl.Sys;
- import org.lwjgl.input.Keyboard;
- import org.lwjgl.input.Mouse;
- import org.lwjgl.opengl.Display;
- import org.lwjgl.opengl.DisplayMode;
- import de.matthiasmann.twl.utils.PNGDecoder;
- import de.matthiasmann.twl.utils.PNGDecoder.Format;
- import static org.lwjgl.opengl.GL11.*;
- import static org.lwjgl.opengl.GL14.*;
- import static org.lwjgl.opengl.GL30.*;
- import static org.lwjgl.util.glu.GLU.*;
- public class LeChasseur {
- static {
- System.setProperty("org.lwjgl.librarypath",
- new File(new File(System.getProperty("user.dir"), "native"),
- LWJGLUtil.getPlatformName()).getAbsolutePath());
- }
- static final int SCREEN_WIDTH = 320, SCREEN_HEIGHT = 160, SCREEN_SCALE = 2;
- static final int MAP_WIDTH = 64, MAP_HEIGHT = 64, TILESET_WIDTH = 4,
- TILESET_HEIGHT = 4;
- int defaultFBO = 0, scaledFBO, scaledFBOTexture;
- HashMap<String, Integer> textures = new HashMap<String, Integer>();
- long lastFrame, lastFPS;
- int fps;
- int mX, mY;
- float rC;
- float camX, camY;
- Tile map[] = new Tile[64 * 64];
- int msgTimer;
- int hCount, sHCount;
- int civPos[][];
- String msg = null;
- ArrayList<Human> humans = new ArrayList<Human>(),
- dHumans = new ArrayList<Human>();;
- ArrayList<Bullet> bullets = new ArrayList<Bullet>(),
- dBullets = new ArrayList<Bullet>();
- Human player = new Human(60, 60, 0, 0x5C9BC8);
- Random rng = new Random();
- int timerMsg2 = 300;
- boolean dead;
- int bCX, bCY;
- public void start() {
- try {
- Display.setDisplayMode(new DisplayMode(SCREEN_WIDTH * SCREEN_SCALE,
- SCREEN_HEIGHT * SCREEN_SCALE));
- updateFPS();
- Display.create();
- Keyboard.enableRepeatEvents(false);
- } catch (LWJGLException e) {
- e.printStackTrace();
- System.exit(0);
- }
- init();
- initGL();
- getDelta();
- lastFPS = getTime();
- while (!Display.isCloseRequested()) {
- int delta = getDelta();
- if (!dead) {
- update(delta);
- } else {
- displayMessage("You are dead, with " + player.cash
- + "$, rest in peace", 1);
- }
- renderGL();
- Display.update();
- Display.sync(60);
- }
- Display.destroy();
- }
- public void update(int delta) {
- timerMsg2--;
- if (timerMsg2 == 0) {
- displayMessage(
- "Make as much money as you can, kill without getting yourself killed",
- 10);
- }
- updateFPS();
- checkInput(delta);
- rC += Math.PI / 10f;
- for (Human human : humans) {
- if (human.x - camX + SCREEN_WIDTH / 2 + 16 > 0
- && human.x - camX + SCREEN_WIDTH / 2 - 16 < SCREEN_WIDTH
- && human.y - camY + SCREEN_HEIGHT / 2 + 16 > 0
- && human.y - camY + SCREEN_HEIGHT / 2 - 16 < SCREEN_HEIGHT) {
- human.update();
- }
- }
- for (Human human : dHumans) {
- if (human == player)
- dead = true;
- humans.remove(human); // MUHEAUHAEHUAwUHUHAAHA
- }
- for (Bullet bullet : bullets) {
- bullet.update();
- }
- for (Bullet bullet : dBullets) {
- bullets.remove(bullet);
- }
- // System.out.println(player.x + ", " + player.y + ", " + player.lRot
- // + ", " + player.rot);
- map[(int) player.x / 16 + ((int) player.y / 16) * MAP_WIDTH].inside();
- if (humans.size() - sHCount - 1 < hCount) {
- // System.out.println("Spawned " + (humans.size() - sHCount - 1));
- int rct = rng.nextInt(hCount);
- Human newH = new AIHuman(civPos[rct][0] * 16 + rng.nextInt(16),
- civPos[rct][1] * 16 + rng.nextInt(16), rng.nextInt(3),
- rng.nextInt(0xFFFFFF));
- newH.lRot = rng.nextInt(360);
- humans.add(newH);
- }
- }
- public void spawnCase() {
- bCX = rng.nextInt(MAP_WIDTH);
- bCY = rng.nextInt(MAP_HEIGHT);
- while (!isCollidable(bCX, bCY)) {
- bCX = rng.nextInt(MAP_WIDTH);
- bCY = rng.nextInt(MAP_HEIGHT);
- }
- setCaseTile(bCX, bCY);
- System.out.println(bCX + ", " + bCY);
- }
- public void init() {
- displayMessage(
- "WASD to move, E to equip, LSHIFT to run and LMB to shoot", 10);
- humans.add(player);
- try {
- BufferedImage map = ImageIO.read(LeChasseur.class
- .getResourceAsStream("/map.png"));
- for (int y = 0; y < MAP_WIDTH; y++) {
- for (int x = 0; x < MAP_WIDTH; x++) {
- setTile(x, y, getTileFromColor(map.getRGB(x, y))
- % TILESET_WIDTH, getTileFromColor(map.getRGB(x, y))
- / TILESET_HEIGHT);
- }
- }
- BufferedImage people = ImageIO.read(LeChasseur.class
- .getResourceAsStream("/people.png"));
- for (int y = 0; y < MAP_WIDTH; y++) {
- for (int x = 0; x < MAP_WIDTH; x++) {
- if (people.getRGB(x, y) == 0xFF0094FF
- || people.getRGB(x, y) == 0xFF7F3300) {
- sHCount++;
- } else if (people.getRGB(x, y) == 0xFFB6FF00) {
- hCount++;
- }
- }
- }
- civPos = new int[hCount][2];
- // System.out.println(hCount);
- int cCt = -1;
- for (int y = 0; y < MAP_WIDTH; y++) {
- for (int x = 0; x < MAP_WIDTH; x++) {
- int col = people.getRGB(x, y);
- if (col == 0xFF0094FF || col == 0xFF7F3300) {
- Human newH = new StationaryHuman(x * 16
- + rng.nextInt(16), y * 16 + rng.nextInt(16),
- rng.nextInt(3), col == 0xFF7F3300);
- newH.lRot = rng.nextInt(360);
- humans.add(newH);
- } else if (col == 0xFFB6FF00) {
- cCt++;
- civPos[cCt][0] = x * 16 + rng.nextInt(16);
- civPos[cCt][1] = y * 16 + rng.nextInt(16);
- Human newH = new AIHuman(x * 16 + rng.nextInt(16), y
- * 16 + rng.nextInt(16), rng.nextInt(3),
- rng.nextInt(0xFFFFFF));
- newH.lRot = rng.nextInt(360);
- humans.add(newH);
- }
- }
- }
- BufferedImage shops = ImageIO.read(LeChasseur.class
- .getResourceAsStream("/shops.png"));
- for (int y = 0; y < MAP_WIDTH; y++) {
- for (int x = 0; x < MAP_WIDTH; x++) {
- int col = shops.getRGB(x, y);
- if (col == 0xFFFF0000) {
- setHealthTile(x, y);
- } else if (col == 0xFF00FF21) {
- setVestTile(x, y);
- } else if (col == 0xFFFFD800) {
- setAmmoTile(x, y);
- }
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- spawnCase();
- }
- public int getTileFromColor(int color) {
- if (color == 0xFF7F0000)
- return 0;
- if (color == 0xFF7F6A00)
- return 1;
- if (color == 0xFF004A7F)
- return 2;
- if (color == 0xFFC0C0C0)
- return 3;
- if (color == 0xFF808080)
- return 4;
- if (color == 0xFF404040)
- return 5;
- if (color == 0xFF7F593F)
- return 6;
- if (color == 0xFF267F00)
- return 7;
- if (color == 0xFFFFB27F)
- return 8;
- if (color == 0xFFA0A0A0)
- return 9;
- if (color == 0xFFFF7F7F)
- return 10;
- if (color == 0xFFFFFFFF)
- return 11;
- if (color == 0xFFFFE97F)
- return 12;
- return 0;
- }
- public int getDelta() {
- long time = getTime();
- int delta = (int) (time - lastFrame);
- lastFrame = time;
- return delta;
- }
- private void checkInput(int delta) {
- mX = Mouse.getX() / SCREEN_SCALE;
- mY = (SCREEN_HEIGHT * SCREEN_SCALE - Mouse.getY()) / SCREEN_SCALE;
- float bRot = -player.lRot - 90;
- player.rot = -player.lRot - 90;
- player.moving = false;
- player.sprinting = false;
- if (Keyboard.isKeyDown(Keyboard.KEY_A)) {
- player.rot -= 90;
- player.moving = true;
- }
- if (Keyboard.isKeyDown(Keyboard.KEY_D)) {
- player.rot += 90;
- player.moving = true;
- }
- if (Keyboard.isKeyDown(Keyboard.KEY_W)) {
- player.rot += (bRot - player.rot) / 2f;
- player.moving = true;
- }
- if (Keyboard.isKeyDown(Keyboard.KEY_S)) {
- player.rot -= (-(player.rot - bRot) / 2f == 0 ? 180
- : -(player.rot - bRot) / 2f);
- player.moving = true;
- }
- if (Keyboard.isKeyDown(Keyboard.KEY_LSHIFT)) {
- player.sprinting = true;
- }
- while (Keyboard.next()) {
- if (Keyboard.getEventKeyState()) {
- if (Keyboard.getEventKey() == Keyboard.KEY_E) {
- player.equip();
- }
- if (Keyboard.getEventKey() == Keyboard.KEY_R) {
- player.reload();
- }
- if (Keyboard.getEventKey() == Keyboard.KEY_Q) {
- map[(int) player.x / 16 + ((int) player.y / 16) * MAP_WIDTH]
- .use();
- }
- }
- }
- while (Mouse.next()) {
- if (Mouse.getEventButtonState()) {
- if (Mouse.getEventButton() == 0) {
- player.shoot();
- }
- }
- }
- }
- public long getTime() {
- return (Sys.getTime() * 1000) / Sys.getTimerResolution();
- }
- public void updateFPS() {
- if (getTime() - lastFPS > 1000) {
- Display.setTitle("Le Chasseur - FPS: " + fps);
- fps = 0;
- lastFPS += 1000;
- }
- fps++;
- }
- public void initGL() {
- glEnable(GL_TEXTURE_2D);
- glEnable(GL_BLEND);
- glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
- initFBO();
- initTextures();
- }
- public void initFBO() {
- scaledFBO = glGenFramebuffers();
- scaledFBOTexture = glGenTextures();
- int scaledFBODBuffer = glGenRenderbuffers();
- glBindFramebuffer(GL_FRAMEBUFFER, scaledFBO);
- glBindTexture(GL_TEXTURE_2D, scaledFBOTexture);
- glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
- glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, SCREEN_WIDTH, SCREEN_HEIGHT,
- 0, GL_RGBA, GL_INT, (java.nio.ByteBuffer) null);
- glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
- GL_TEXTURE_2D, scaledFBOTexture, 0);
- glBindRenderbuffer(GL_RENDERBUFFER, scaledFBODBuffer);
- glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24,
- SCREEN_WIDTH, SCREEN_HEIGHT);
- glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,
- GL_RENDERBUFFER, scaledFBODBuffer);
- glBindFramebuffer(GL_FRAMEBUFFER, 0);
- }
- public void initTextures() {
- textures.put("font", loadTexture("/font.png"));
- textures.put("tiles", loadTexture("/tiles.png"));
- }
- public int loadTexture(String fileDirectory) {
- InputStream in = LeChasseur.class.getResourceAsStream(fileDirectory);
- PNGDecoder decoder = null;
- try {
- decoder = new PNGDecoder(in);
- } catch (IOException e) {
- e.printStackTrace();
- }
- ByteBuffer buffer = BufferUtils.createByteBuffer(4 * decoder.getWidth()
- * decoder.getHeight());
- try {
- decoder.decode(buffer, decoder.getWidth() * 4, Format.RGBA);
- buffer.flip();
- in.close();
- } catch (IOException e) {
- }
- int texture = glGenTextures();
- glBindTexture(GL_TEXTURE_2D, texture);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, decoder.getWidth(),
- decoder.getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
- glBindTexture(GL_TEXTURE_2D, 0);
- glBindTexture(GL_TEXTURE_2D, 0);
- return texture;
- }
- public int nPo2(final int a) {
- int b = 1;
- while (b < a) {
- b = b << 1;
- }
- return b;
- }
- public void renderString(int x, int y, String string) {
- for (int i = 0; i < string.length(); i++) {
- char ch = (char) (string.charAt(i) - 32);
- float chX = (ch % 32) * 1 / 32f, chY = (ch / 32) * 1 / 3f;
- glBindTexture(GL_TEXTURE_2D, textures.get("font"));
- glPushMatrix();
- glTranslatef(x + i * 4, y, 0);
- glBegin(GL_QUADS);
- glTexCoord2f(chX, chY);
- glVertex2f(0, 0);
- glTexCoord2f(chX + 1 / 32f, chY);
- glVertex2f(4, 0);
- glTexCoord2f(chX + 1 / 32f, chY + 1 / 3f);
- glVertex2f(4, 6);
- glTexCoord2f(chX, chY + 1 / 3f);
- glVertex2f(0, 6);
- glEnd();
- glPopMatrix();
- glBindTexture(GL_TEXTURE_2D, 0);
- }
- }
- public void renderQuad(float xS, float yS) {
- glPushMatrix();
- glScalef(xS, yS, 1);
- glBegin(GL_QUADS);
- glVertex2f(-0.5f, -0.5f);
- glVertex2f(0.5f, -0.5f);
- glVertex2f(0.5f, 0.5f);
- glVertex2f(-0.5f, 0.5f);
- glEnd();
- glPopMatrix();
- }
- public void renderCQuad(float xS, float yS) {
- glPushMatrix();
- glScalef(xS, yS, 1);
- glBegin(GL_QUADS);
- glVertex2f(0, 0);
- glVertex2f(1f, 0);
- glVertex2f(1f, 1f);
- glVertex2f(0, 1f);
- glEnd();
- glPopMatrix();
- }
- public void renderTile(int x, int y) {
- float tX = 1f / TILESET_WIDTH * x, tY = 1f / TILESET_HEIGHT * y;
- glBindTexture(GL_TEXTURE_2D, textures.get("tiles"));
- glPushMatrix();
- glBegin(GL_QUADS);
- glTexCoord2f(tX, tY);
- glVertex2f(0, 0);
- glTexCoord2f(tX + 1f / TILESET_WIDTH, tY);
- glVertex2f(16, 0);
- glTexCoord2f(tX + 1f / TILESET_WIDTH, tY + 1f / TILESET_HEIGHT);
- glVertex2f(16, 16);
- glTexCoord2f(tX, tY + 1f / TILESET_HEIGHT);
- glVertex2f(0, 16);
- glEnd();
- glPopMatrix();
- glBindTexture(GL_TEXTURE_2D, 0);
- }
- public void renderCTile(int x, int y) {
- float tX = 1f / TILESET_WIDTH * x, tY = 1f / TILESET_HEIGHT * y;
- glBindTexture(GL_TEXTURE_2D, textures.get("tiles"));
- glPushMatrix();
- glBegin(GL_QUADS);
- glTexCoord2f(tX, tY);
- glVertex2f(-8, -8);
- glTexCoord2f(tX + 1f / TILESET_WIDTH, tY);
- glVertex2f(8, -8);
- glTexCoord2f(tX + 1f / TILESET_WIDTH, tY + 1f / TILESET_HEIGHT);
- glVertex2f(8, 8);
- glTexCoord2f(tX, tY + 1f / TILESET_HEIGHT);
- glVertex2f(-8, 8);
- glEnd();
- glPopMatrix();
- glBindTexture(GL_TEXTURE_2D, 0);
- }
- public void renderHuman(int c1, int c2, int c3, float armRot, float wL) {
- glPushMatrix();
- glPushMatrix();
- glRotate(armRot);
- if (wL != 0) {
- glTranslatef(0, -wL * 1.5f, 0);
- glColori(0x48413E);
- renderQuad(1.75f, wL); // Weapon
- glTranslatef(0, wL * 1.5f, 0);
- glColori(c1);
- glTranslatef(0, -3, 0);
- glPushMatrix();
- glTranslatef(-3, 0, 0);
- glRotate(60);
- renderQuad(7, 3); // ArmL
- glPopMatrix();
- glPushMatrix();
- glTranslatef(3, 0, 0);
- glRotate(-60);
- renderQuad(7, 3); // ArmR
- glPopMatrix();
- } else {
- glColori(c1);
- renderQuad(14, 3); // Arm
- }
- glPopMatrix();
- glColori(c2);
- renderQuad(10, 5); // Body
- glColori(c3);
- renderQuad(4, 5); // Head
- glColori(0xFFFFFF);
- glPopMatrix();
- }
- public void glColori(int color) {
- glColor3f((color >> 16 & 0xFF) / 255f, (color >> 8 & 0xFF) / 255f,
- (color & 0xFF) / 255f);
- }
- public void glRotate(float rot) {
- glRotatef(rot, 0, 0, -1);
- }
- public void setTile(int x, int y, int tX, int tY) {
- map[x + y * MAP_WIDTH] = new Tile(tX, tY);
- }
- public void setAmmoTile(int x, int y) {
- map[x + y * MAP_WIDTH] = new AmmoTile();
- }
- public void setVestTile(int x, int y) {
- map[x + y * MAP_WIDTH] = new VestTile();
- }
- public void setHealthTile(int x, int y) {
- map[x + y * MAP_WIDTH] = new HealthTile();
- }
- public void setCaseTile(int x, int y) {
- map[x + y * MAP_WIDTH] = new CaseTile(map[x + y * MAP_WIDTH].tX, map[x
- + y * MAP_WIDTH].tY);
- }
- public void readyScaledFBO() {
- glViewport(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
- glBindFramebuffer(GL_FRAMEBUFFER, scaledFBO);
- }
- public void clearGL() {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glClearColor(0.1f, 0.1f, 0.1f, 1f);
- }
- public void ready2D() {
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluOrtho2D(0.0f, SCREEN_WIDTH, SCREEN_HEIGHT, 0.0f);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- glTranslatef(0.375f, 0.375f, 0.0f);
- glDisable(GL_DEPTH_TEST);
- }
- public void render2D() {
- glColori(0xFFFFFF);
- camX = player.x;
- camY = player.y;
- float rot = (float) Math.toDegrees(Math.atan2(SCREEN_HEIGHT / 2 - mY,
- mX - SCREEN_WIDTH / 2)) - 90;
- glPushMatrix();
- glTranslatef(SCREEN_WIDTH / 2 - (int) camX, SCREEN_HEIGHT / 2
- - (int) camY, 0);
- for (int x = 0; x < MAP_WIDTH; x++) {
- for (int y = 0; y < MAP_HEIGHT; y++) {
- if (SCREEN_WIDTH / 2 - (int) camX + x * 16 < -16
- || SCREEN_WIDTH / 2 - (int) camX + x * 16 > SCREEN_WIDTH
- || SCREEN_HEIGHT / 2 - (int) camY + y * 16 < -16
- || SCREEN_HEIGHT / 2 - (int) camY + y * 16 > SCREEN_HEIGHT)
- continue;
- glPushMatrix();
- glTranslatef(x * 16, y * 16, 0);
- renderTile(map[x + y * MAP_WIDTH].tX, map[x + y * MAP_WIDTH].tY);
- if (map[x + y * MAP_WIDTH] instanceof CaseTile) {
- glPushMatrix();
- glTranslatef(8, 8, 0);
- glRotate(rC * 10);
- renderCTile(3, 3);
- glPopMatrix();
- }
- glPopMatrix();
- }
- }
- player.lRot = rot;
- for (Human human : humans) {
- human.render();
- }
- for (Bullet bullet : bullets) {
- bullet.render();
- }
- glPopMatrix();
- renderUI();
- }
- public void renderUI() {
- glColori(0xFFFFFF);
- // glColori(0x2C3952);
- // renderString(3 + 1, 48 + 1, "X: " + (int) player.x / 16 + " Y: " +
- // (int) player.y / 16);
- // glColori(0xDFDFDF);
- // renderString(3, 48, "X: " + (int) player.x / 16 + " Y: " + (int)
- // player.y / 16);
- glColori(0x2C3952);
- renderString(3 + 1, 3 + 1, "Blood: " + (int) player.hp + "%");
- glColori(0xDFDFDF);
- renderString(3, 3, "Blood: " + (int) player.hp + "%");
- if ((int) player.v > 0) {
- glColori(0x2C3952);
- renderString(3 + 12 * 4 + 1, 3 + 1, "Vest: " + (int) player.v + "%");
- glColori(0xDFDFDF);
- renderString(3 + 12 * 4, 3, "Vest: " + (int) player.v + "%");
- }
- glColori(0x2C3952);
- renderString(3 + 1, 11 + 1, "Ammo: " + player.mag + "/" + player.ammo);
- glColori(0xDFDFDF);
- renderString(3, 11, "Ammo: " + player.mag + "/" + player.ammo);
- glColori(0x2C3952);
- renderString(3 + 1, 19 + 1, player.cash + "$");
- glColori(0xDFDFDF);
- renderString(3, 19, player.cash + "$");
- glColori(0x2C3952);
- renderString(2 + 1, SCREEN_HEIGHT - 8 + 1,
- "Le Chasseur - Ludum Dare 25");
- glColori(0xDFDFDF);
- renderString(2, SCREEN_HEIGHT - 8, "Le Chasseur - Ludum Dare 25");
- msgTimer--;
- if (msg != null && msgTimer > 0) {
- glColori(0x2C3952);
- renderString((SCREEN_WIDTH - msg.length() * 4) / 2 + 1,
- SCREEN_HEIGHT - 20 + 1, msg);
- glColori(0xDFDFDF);
- renderString((SCREEN_WIDTH - msg.length() * 4) / 2,
- SCREEN_HEIGHT - 20, msg);
- }
- }
- public void displayMessage(String msg, int time) {
- this.msg = msg;
- msgTimer = time * 30;
- }
- public void renderScaledFBO() {
- glViewport(0, 0, SCREEN_WIDTH * SCREEN_SCALE, SCREEN_HEIGHT
- * SCREEN_SCALE);
- glBindFramebuffer(GL_READ_FRAMEBUFFER, scaledFBO);
- glBindFramebuffer(GL_DRAW_FRAMEBUFFER, defaultFBO);
- glBlitFramebuffer(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, 0, SCREEN_WIDTH
- * SCREEN_SCALE, SCREEN_HEIGHT * SCREEN_SCALE,
- GL_COLOR_BUFFER_BIT, GL_NEAREST);
- }
- public void renderGL() {
- readyScaledFBO();
- clearGL();
- ready2D();
- render2D();
- renderScaledFBO();
- }
- public boolean isCollidable(int x, int y) {
- if (x < 0 || y < 0) {
- return false;
- }
- try {
- return map[x + y * MAP_WIDTH].collidable;
- } catch (Exception e) {
- return false;
- }
- }
- public boolean isBCollidable(int x, int y) {
- if (x < 0 || y < 0) {
- return false;
- }
- try {
- return map[x + y * MAP_WIDTH].bCollidable;
- } catch (Exception e) {
- return false;
- }
- }
- public static void main(String[] argv) {
- LeChasseur main = new LeChasseur();
- main.start();
- }
- public class Human {
- float x, y;
- int hp = 100, v = 0;
- float rot, lRot;
- boolean moving, sprinting;
- int race, cc1, cc2;
- int mag = 7, ammo = 49, cash = 50;
- int reloadTimer, rST;
- boolean us = true, weapon = false, wA;
- boolean rS;
- public Human(float x, float y, int race, int cc1, int cc2) {
- this.x = x;
- this.y = y;
- this.race = race;
- this.cc1 = cc1;
- this.cc2 = cc2;
- }
- public Human(float x, float y, int race, int cc2) {
- this.x = x;
- this.y = y;
- this.race = race;
- this.cc2 = cc2;
- us = false;
- }
- public void update() {
- move();
- hitCheck();
- deadCheck();
- }
- public void deadCheck() {
- if (hp <= 0) {
- hp = 0;
- dHumans.add(this);
- }
- }
- public void hitCheck() {
- for (Bullet bullet : bullets) {
- if (bullet.parent != this) {
- float dist = (float) Math.sqrt(Math.pow(bullet.x - x, 2)
- + Math.pow(bullet.y - y, 2));
- if (dist <= 6) {
- int dmg = (int) ((7 - dist) * 2);
- if (v > 0) {
- v -= dmg;
- if (v < 0) {
- hp += v;
- }
- } else {
- hp -= dmg;
- }
- dBullets.add(bullet);
- }
- }
- }
- }
- public void move() {
- float xInc = (float) (moving ? sprinting ? Math.cos(Math
- .toRadians(rot)) * 1.5f : Math.cos(Math.toRadians(rot)) : 0), yInc = (float) (moving ? sprinting ? Math
- .sin(Math.toRadians(rot)) * 1.5f : Math.sin(Math
- .toRadians(rot))
- : 0);
- int nTX = (int) Math.floor((x + xInc) / 16f), nTY = (int) Math
- .floor((y + yInc) / 16f);
- if (isCollidable(nTX, (int) y / 16)) {
- x += xInc;
- }
- if (isCollidable((int) x / 16, nTY)) {
- y += yInc;
- }
- }
- public void render() {
- rST--;
- rS = rST > 0;
- reloadTimer--;
- if (reloadTimer <= 0 && wA == false) {
- wA = true;
- if (ammo < 7 - mag) {
- mag += ammo;
- ammo = 0;
- } else {
- ammo -= 7 - mag;
- mag += 7 - mag;
- }
- }
- glPushMatrix();
- glTranslatef(x, y, 0);
- glRotate(lRot);
- float rot2 = moving ? (float) Math
- .sin(rC * (sprinting ? 1f : 0.7f))
- * (sprinting ? 20f : weapon ? 7f : 15f) : 0;
- renderHuman(us ? cc1 : raceColor() - 0x0A0A0A, cc2, raceColor(),
- rot2, weapon ? 5 : 0);
- glPopMatrix();
- }
- public void shoot() {
- if (weapon) {
- rST = 20;
- rS = true;
- float rot2 = moving ? (float) Math.sin(rC
- * (sprinting ? 1f : 0.7f))
- * (sprinting ? 20f : weapon ? 7f : 15f) : (float) Math
- .sin(rC) * 3f;
- if (mag > 0) {
- mag--;
- if (mag <= 0) {
- reload();
- }
- bullets.add(new Bullet(x
- + (float) Math.cos(Math
- .toRadians(-lRot - 90 + rot2)) * 6, y
- + (float) Math.sin(Math
- .toRadians(-lRot - 90 + rot2)) * 6, -lRot
- - 90 + rot2, this));
- }
- }
- }
- public void equip() {
- if (wA) {
- weapon ^= true;
- } else {
- weapon = false;
- }
- }
- public void reload() {
- reloadTimer = 40;
- weapon = false;
- wA = false;
- }
- public int raceColor() {
- return race == 0 ? 0xDAA873 - 0x0A0A0A
- : race == 1 ? 0xA77F56 - 0x0A0A0A : 0x4B331A - 0x0A0A0A;
- }
- }
- public class AIHuman extends Human {
- int newAT, newA;
- int moveTimer;
- boolean aggro;
- public AIHuman(float x, float y, int race, int cc1, int cc2) {
- super(x, y, race, cc1, cc2);
- ammo = Integer.MAX_VALUE;
- }
- public AIHuman(float x, float y, int race, int cc2) {
- super(x, y, race, cc2);
- us = false;
- }
- public void update() {
- newAT--;
- move();
- hitCheck();
- deadCheck();
- float dist = (float) Math.sqrt(Math.pow(player.x - x, 2)
- + Math.pow(player.y - y, 2));
- rot = -lRot - 90;
- if (!aggro) {
- moving = false;
- weapon = false;
- if (dist < 60) {
- rotateTo((float) -Math.toDegrees(Math.atan2(player.y - y,
- player.x - x)) - 90, 18);
- if (player.weapon) {
- weapon = true;
- }
- } else {
- if (newAT < 0) {
- newA = rng.nextInt(360);
- newAT = 40 + rng.nextInt(40);
- }
- moveTimer--;
- if (moveTimer > 0) {
- moving = true;
- } else if (moveTimer <= 0 && moveTimer > -50) {
- moving = false;
- } else {
- moveTimer = 20 + rng.nextInt(40);
- }
- rotateTo(newA, 20);
- }
- if (dist < 90 && player.rS) {
- aggro = true;
- }
- } else {
- moving = true;
- weapon = true;
- if (dist > 90) {
- aggro = false;
- }
- if (dist < 24) {
- moving = false;
- }
- if (rST < -10) {
- shoot();
- }
- rotateTo((float) -Math.toDegrees(Math.atan2(player.y - y,
- player.x - x)) - 90, 2 + rng.nextInt(3));
- }
- }
- public void rotateTo(float angle, float div) {
- // Normalize angle
- while (angle < 0) {
- angle += 360.f;
- }
- while (angle > 360) {
- angle -= 360.f;
- }
- // Normalize lRot too, just in case (remove if you know it's not
- // needed)
- while (lRot < 0) {
- lRot += 360.f;
- }
- while (lRot > 360) {
- lRot -= 360.f;
- }
- // Difference between angles
- float angdiff = Math.abs(angle - lRot);
- // When angle diff is very small, snap to it directly
- if (angdiff <= 0.25f) {
- lRot = angle;
- } else {
- // Let's decide if we have to turn left or right to reach the
- // target angle the quickest
- // You might have to switch the signs, not sure which is which
- if ((int) ((angle - lRot + 360) % 360) > 180) {
- lRot -= angdiff / div;
- } else {
- lRot += angdiff / div;
- }
- }
- }
- public void deadCheck() {
- if (hp <= 0) {
- hp = 0;
- player.cash += 8 + rng.nextInt(8);
- dHumans.add(this);
- }
- }
- }
- public class StationaryHuman extends Human {
- int newAT, newA;
- public StationaryHuman(float x, float y, int race, boolean dealer) {
- super(x, y, race, dealer ? 0x7A7776 : 0x3F647F, dealer ? 0x565453
- : 0x6099C1);
- }
- public void update() {
- newAT--;
- move();
- hitCheck();
- float dist = (float) Math.sqrt(Math.pow(player.x - x, 2)
- + Math.pow(player.y - y, 2));
- if (dist < 60) {
- rotateTo((float) -Math.toDegrees(Math.atan2(player.y - y,
- player.x - x)) - 90);
- } else {
- if (newAT < 0) {
- newA = rng.nextInt(360);
- newAT = 40 + rng.nextInt(40);
- }
- rotateTo(newA);
- }
- }
- public void rotateTo(float angle) {
- // Normalize angle
- while (angle < 0) {
- angle += 360.f;
- }
- while (angle > 360) {
- angle -= 360.f;
- }
- // Normalize lRot too, just in case (remove if you know it's not
- // needed)
- while (lRot < 0) {
- lRot += 360.f;
- }
- while (lRot > 360) {
- lRot -= 360.f;
- }
- // Difference between angles
- float angdiff = Math.abs(angle - lRot);
- // When angle diff is very small, snap to it directly
- if (angdiff <= 0.25f) {
- lRot = angle;
- } else {
- // Let's decide if we have to turn left or right to reach the
- // target angle the quickest
- // You might have to switch the signs, not sure which is which
- if ((int) ((angle - lRot + 360) % 360) > 180) {
- lRot -= angdiff / 20f;
- } else {
- lRot += angdiff / 20f;
- }
- }
- }
- }
- public class Bullet {
- float x, y;
- float hp = 100;
- float rot;
- Human parent;
- public Bullet(float x, float y, float rot, Human parent) {
- this.x = x;
- this.y = y;
- this.rot = rot;
- this.parent = parent;
- }
- public void update() {
- move();
- deadCheck();
- }
- public void deadCheck() {
- hp--;
- if (hp <= 0) {
- dBullets.add(this);
- }
- }
- public void move() {
- float xInc = (float) Math.cos(Math.toRadians(rot)) * 5f, yInc = (float) Math
- .sin(Math.toRadians(rot)) * 5f;
- int nTX = (int) Math.floor((x + xInc) / 16f), nTY = (int) Math
- .floor((y + yInc) / 16f);
- if (isBCollidable(nTX, (int) y / 16)) {
- x += xInc;
- }
- if (isBCollidable((int) x / 16, nTY)) {
- y += yInc;
- }
- if (!isBCollidable(nTX, (int) y / 16)
- || !isBCollidable((int) x / 16, nTY)) {
- dBullets.add(this);
- }
- }
- public void render() {
- glPushMatrix();
- glTranslatef(x, y, 0);
- glRotate(-rot);
- glColori(0xFFE97F);
- renderQuad(3, 1);
- glPopMatrix();
- }
- }
- public class Tile {
- boolean collidable = true, bCollidable = true;
- int tX, tY;
- public Tile(int tX, int tY) {
- this.tX = tX;
- this.tY = tY;
- if (tX < 3 && tY == 0) {
- collidable = false;
- bCollidable = false;
- }
- if (tX == 0 && tY == 2) {
- collidable = false;
- bCollidable = false;
- }
- if (tX == 2 && tY == 1) {
- collidable = false;
- }
- }
- public void inside() {
- }
- public void use() {
- }
- }
- public class AmmoTile extends Tile {
- int bP = 4, aC = 14;
- public AmmoTile() {
- super(0, 3);
- }
- public void inside() {
- displayMessage(aC + " bullets for " + bP + "$, press Q to buy", 3);
- }
- public void use() {
- if (player.cash >= bP) {
- player.cash -= bP;
- player.ammo += 14;
- displayMessage("Bought " + aC + " bullets for " + bP + "$", 3);
- }
- }
- }
- public class HealthTile extends Tile {
- int bP;
- public HealthTile() {
- super(0, 3);
- }
- public void inside() {
- bP = (int) ((100 - player.hp) / 2);
- if (bP < 1) {
- displayMessage("No need for wound recovery", 3);
- } else {
- displayMessage(
- "Wound recovery for " + bP + "$, press Q to buy", 3);
- }
- }
- public void use() {
- bP = (int) ((100 - player.hp) / 2);
- if (player.cash >= bP) {
- player.cash -= bP;
- player.hp = 100;
- displayMessage("Recovered wounds for " + bP + "$", 3);
- }
- }
- }
- public class VestTile extends Tile {
- int bP = 40;
- public VestTile() {
- super(0, 3);
- }
- public void inside() {
- displayMessage("Kevlar vest for " + bP + "$, press Q to buy", 3);
- }
- public void use() {
- if (player.cash >= bP) {
- player.cash -= bP;
- player.v = 100;
- displayMessage("Bought kevlar vest for " + bP + "$", 3);
- }
- }
- }
- public class CaseTile extends Tile {
- int r = 1 + rng.nextInt(50);
- public CaseTile(int tX, int tY) {
- super(tX, tY);
- }
- public void inside() {
- displayMessage("Briefcase with money inside, press Q to take", 3);
- }
- public void use() {
- player.cash += r;
- displayMessage("Found briefcase with " + r + "$ inside", 20);
- for (Human aggrodude : humans) {
- if (aggrodude instanceof AIHuman) {
- aggrodude.x = player.x - 2 + rng.nextInt(4);
- aggrodude.y = player.y - 2 + rng.nextInt(4);
- ((AIHuman) aggrodude).aggro = true; // AGGRODUDE GO!
- break;
- }
- }
- setTile((int) (player.x / 16), (int) (player.y / 16), tX, tY);
- spawnCase();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement