Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package net.flodro.minigame;
- import java.awt.Graphics;
- import java.awt.Point;
- import java.awt.Rectangle;
- import java.awt.event.KeyEvent;
- import java.awt.event.KeyListener;
- import java.util.ArrayList;
- import net.flodro.minigame.graphics.DoubleRectangle;
- import net.flodro.minigame.graphics.Images;
- import net.flodro.minigame.Player;
- import net.flodro.minigame.MiniGame;
- public class Player extends DoubleRectangle implements KeyListener {
- // private int lives = 3;
- private int speed = 1;
- public static int jumpTime = 92;
- private double jumpSpeed = 0.5;
- public static int jumpCount = 0;
- public int moving = 0;
- public static boolean jumped = false;
- public static boolean isJumping = false;
- public static boolean isFalling = false;
- public Point pt1;
- public Point pt2;
- public Point pt3;
- public Point pt4;
- public Point pt5;
- public Point pt6;
- public Point pt7;
- public Point pt8;
- public Point pt9;
- public Point pt10;
- public Point pt11;
- public Point pt12;
- public Point pt13;
- public Point pt14;
- public static boolean topcollide = false;
- public static boolean leftcollide = false;
- public static boolean rightcollide = false;
- public static boolean bottomcollide = false;
- public int x;
- public int y;
- public static ArrayList<Rectangle> floor = new ArrayList<Rectangle>();
- public static ArrayList<String> floorStatus = new ArrayList<String>();
- public Player(int x, int y, int width, int height) {
- setBounds(x, y, width, height);
- this.x = x;
- this.y = y;
- pt1 = new Point(0, 0); // Top Left
- pt2 = new Point(0, (height / 2)); // Middle Left
- pt3 = new Point(0, height); // Bottom Left
- pt4 = new Point(width, 0); // Top Right
- pt5 = new Point(width, (height / 2)); // Middle Right
- pt6 = new Point(width, height); // Bottom Right
- pt7 = new Point(1, 0); // Top Left left+1
- pt8 = new Point(0, 1); // Top Left down+1
- pt9 = new Point(1, height); // Bottom Left left+1
- pt10 = new Point(0, height - 1); // Bottom Left up+1
- pt11 = new Point(width - 1, 0); // Top Right right+1
- pt12 = new Point(width, 1); // Top Right down+1
- pt13 = new Point(width - 1, height); // Bottom Right right+1
- pt14 = new Point(width, height - 1); // Bottom Right up+1
- if(MiniGame.currentScreen == 1) {
- floor.clear();
- floorStatus.clear();
- floor.add(new Rectangle(150, 220, 150, 30));
- floor.add(new Rectangle(0, 150, MiniGame.WIN_WIDTH, 30));
- x=0;
- y=0;
- for(int i = 0; i < floor.size(); i++) { floorStatus.add("Nothing"); }
- } else if(MiniGame.currentScreen == 2) {
- floor.clear();
- floorStatus.clear();
- floor.add(new Rectangle(0, 195, MiniGame.WIN_WIDTH, 30));
- floor.add(new Rectangle(0, 150, MiniGame.WIN_WIDTH, 30));
- x=0;
- y=0;
- for(int i = 0; i < floor.size(); i++) { floorStatus.add("Nothing"); }
- } else {
- floor.clear();
- floorStatus.clear();
- floor.add(new Rectangle(0, 150, MiniGame.WIN_WIDTH, 30));
- x=0;
- y=0;
- for(int i = 0; i < floor.size(); i++) { floorStatus.add("Nothing"); }
- }
- }
- public void tick() {
- if (isJumping && !topcollide) {
- y -= jumpSpeed;
- jumpCount++;
- if(jumpCount == jumpTime) {
- jumpCount = 0;
- isJumping = false;
- jumped = false;
- isFalling = true;
- }
- } else if (isJumping && topcollide) {
- jumpCount = 0;
- topcollide = false;
- isFalling = true;
- }
- if(isFalling) {
- y++;
- }
- if(!bottomcollide && !isJumping) {
- y++;
- topcollide = false;
- isFalling = true;
- }
- if(x < 0) { } else {
- if(moving == 1 && !leftcollide) {
- x -= speed;
- }
- }
- if (x > (MiniGame.WIN_WIDTH - this.width) ) { } else {
- if(moving == 2 && !rightcollide) {
- x += speed;
- }
- }
- pt1.setLocation(x, y);
- pt2.setLocation(x, y + (height / 2));
- pt3.setLocation(x, y + height);
- pt4.setLocation(x + width, y);
- pt5.setLocation(x + width, y + (height / 2));
- pt6.setLocation(x + width, y + height);
- pt7.setLocation(x + 1, y);
- pt8.setLocation(x, y + 1);
- pt9.setLocation(x + 1, y + height);
- pt10.setLocation(x, y + height - 1);
- pt11.setLocation(x + width - 1, y);
- pt12.setLocation(x + width, y + 1);
- pt13.setLocation(x + width - 1, y + height);
- pt14.setLocation(x + width, y + height - 1);
- for (int i = 0; i < floor.size(); i++) {
- if (floor.get(i).contains(pt1) && floor.get(i).contains(pt8) || floor.get(i).contains(pt3) && floor.get(i).contains(pt10)) {
- floorStatus.set(i, "Left Collision");
- } else if (floor.get(i).contains(pt3) && floor.get(i).contains(pt9) || floor.get(i).contains(pt6) && floor.get(i).contains(pt13)) {
- floorStatus.set(i, "Bot Collision");
- } else if (floor.get(i).contains(pt4) && floor.get(i).contains(pt12) || floor.get(i).contains(pt6) && floor.get(i).contains(pt14)) {
- floorStatus.set(i, "Right Collision");
- } else if (floor.get(i).contains(pt1) && floor.get(i).contains(pt7) || floor.get(i).contains(pt4) && floor.get(i).contains(pt11)) {
- floorStatus.set(i, "Top Collision");
- } else {
- floorStatus.set(i, "Nothing");
- }
- }
- if(floorStatus.contains("Bot Collision")) { bottomcollide = true; } else { bottomcollide = false; }
- if(floorStatus.contains("Right Collision")) { rightcollide = true; } else { rightcollide = false; }
- if(floorStatus.contains("Left Collision")) { leftcollide = true; } else { leftcollide = false; }
- if(floorStatus.contains("Top Collision")) { topcollide = true; } else { topcollide = false; }
- System.out.println("TOP " + topcollide + " LEFT " + leftcollide + " RIGHT " + rightcollide + " BOTTOM " + bottomcollide);
- }
- public void render(Graphics g) {
- g.drawImage(Images.player, (int) x, (int) y, (int) width, (int) height, null);
- }
- @Override
- public void keyPressed(KeyEvent e) {
- int key = e.getKeyCode();
- switch(key) {
- case KeyEvent.VK_UP:
- if(!jumped && !Player.isFalling) { Player.isJumping = true; jumped = true; }
- break;
- case KeyEvent.VK_LEFT:
- moving = 1;
- break;
- case KeyEvent.VK_RIGHT:
- moving = 2;
- break;
- }
- }
- @Override
- public void keyReleased(KeyEvent e) {
- int key = e.getKeyCode();
- switch(key) {
- case KeyEvent.VK_LEFT:
- moving = 0;
- break;
- case KeyEvent.VK_RIGHT:
- moving = 0;
- break;
- }
- }
- @Override
- public void keyTyped(KeyEvent e) { }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement