Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Color;
- import java.awt.Font;
- import java.awt.Graphics;
- import java.awt.Graphics2D;
- import java.awt.Rectangle;
- import java.util.LinkedList;
- public class Player extends GameObject{
- /*
- *
- Powerup ideas:
- > Build a temporary wall
- > Lock on aim;
- > Rapid fire
- > Burst Shot;
- */
- private float _acc = 0.1f;
- private float _dcc = 0.2f;
- private KeyInput input;
- private int width = 32;
- private int height = 32;
- private float health = 32;
- public int frequency = 50;
- public int iterator = 0;
- private final float MAX_SPEED = 20;
- private float gravity = 1f;
- private Handler handler;
- public Player(float x, float y, ID id, KeyInput input, Handler handler) {
- super(x,y,id);
- this.input = input;
- this.handler = handler;
- }
- public void tick(LinkedList<GameObject> object) {
- iterator++; //For Regular shooting intervals. Yet to be added
- x += velX;
- y += velY;
- if(falling || jumping) {
- velY += gravity;
- if(velY > MAX_SPEED) {
- velY = MAX_SPEED;
- }
- }
- Collision(object);
- bulletCollision(object);
- //Horizontal
- if(input.keys[0]) velX = 5;
- else if(input.keys[1]) velX = -5;
- else if(!input.keys[0] && !input.keys[1]) {
- if(velX > 0 ) velX = 0;
- else if(velX < 0) velX = 0;
- }
- velX = clamp(velX, 8 , -8);
- velY = clamp(velY, MAX_SPEED, -MAX_SPEED);
- if(x >= Game.WIDTH - 38) x = Game.WIDTH - 38;
- if(x <= 0) x = 0;
- if(y >= Game.HEIGHT - 60) y = Game.HEIGHT - 60;
- if(y <= 0) {
- velY = 0;
- velX = 0;
- y = 1;
- }
- if(health <= 0) {
- handler.setPlaying(false);
- }
- }
- private void bulletCollision(LinkedList<GameObject> object) {
- for(int i = 0; i < handler.object.size(); i++) {
- GameObject tempObject = handler.object.get(i);
- if(tempObject.getId() == ID.EnemyBullet) {
- if(tempObject.getBounds().intersects(getBoundsTop()) || tempObject.getBounds().intersects(getBoundsBottom()) || tempObject.getBounds().intersects(getBoundsLeft()) || tempObject.getBounds().intersects(getBoundsRight())) {
- health -= 2;
- }
- }
- }
- }
- private void Collision(LinkedList<GameObject> object) {
- for(int i = 0; i < handler.object.size(); i++) {
- GameObject tempObject = handler.object.get(i);
- if(tempObject.getId() == ID.Block) {
- if(getBoundsTop().intersects(tempObject.getBounds())) {
- y = tempObject.getY() + height;
- velY = 0;
- }
- if(getBoundsBottom().intersects(tempObject.getBounds())) {
- y = tempObject.getY() - height;
- velY = 0;
- falling = false;
- jumping = false;
- }else falling = true;
- //Right
- if(getBoundsRight().intersects(tempObject.getBounds())) {
- x = tempObject.getX() - width;
- }
- //Left
- if(getBoundsLeft().intersects(tempObject.getBounds())) {
- x = tempObject.getX() + width;
- }
- }
- }
- }
- private float clamp(float value, float max, float min) {
- if(value > max) value = max;
- else if( value <= min) value = min;
- return value;
- }
- public void render(Graphics g) {
- Graphics2D g2d = (Graphics2D) g;
- g.setColor(Color.blue);
- g.fillRect((int)x, (int)y, width, height+1);
- g.setFont(new Font("Arial", Font.PLAIN, 10));
- g.setColor(Color.RED);
- g.fillRect((int)x, (int)y - 10, (int)health, 5);
- g.setColor(Color.WHITE);
- g.drawRect((int)x, (int)y - 10, width, 5);
- //g.drawString("x: "+x + " y: "+y, (int)x, (int)y-5);
- g2d.draw(getBoundsBottom());
- g2d.draw(getBoundsLeft());
- g2d.draw(getBoundsRight());
- g2d.draw(getBoundsTop());
- }
- public int getWidth() {
- return width;
- }
- public void setWidth(int width) {
- this.width = width;
- }
- public int getHeight() {
- return height;
- }
- public void setHeight(int height) {
- this.height = height;
- }
- public float getHealth() {
- return health;
- }
- public void setHealth(float health) {
- this.health = health;
- }
- public Rectangle getBoundsTop() {
- return new Rectangle((int)x+width/2-((width/2)/2),(int)y, width/2, height/2);
- }
- public Rectangle getBoundsBottom() {
- return new Rectangle((int)x+width/2-((width/2)/2),(int)y+(height/2), width/2, height/2);
- }
- public Rectangle getBoundsLeft() {
- return new Rectangle((int)x,(int)y, 5, height-2);
- }
- public Rectangle getBoundsRight() {
- return new Rectangle((int)x+width-5,(int)y, 5, height-2);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement