Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- private int x;
- private int y;
- private int life=70;
- private Input input;
- private int velocity = 5;
- private Shape hitbox;
- private boolean moving;
- private Image image;
- private double mouseX;
- private double mouseY;
- private boolean spawn;
- private ArrayList <MeleeWeapon> attack;
- private Shape weapon;
- private SpriteSheet weaponWalk;
- private SpriteSheet walk;
- private Animation walkingAnim;
- private Animation weaponWalkAnim;
- private SpriteSheet meleeAttack;
- private Animation attackAnim;
- private boolean attackCheck;
- private int attackTimer;
- private int stamina=100;
- private static final int maxLife = 100;
- private double xDistance;
- private double yDistance;
- private SpriteSheet sprites;
- private Animation walkAnim;
- private SpriteSheet bulletSheet;
- private Animation shootingAnimation;
- private boolean discarded;
- public boolean hit;
- private boolean shotCheck;
- public Player(int x, int y, Input input) throws SlickException{
- this.x = x;
- this.y = y;
- this.input = input;
- hitbox = new Circle(x, y, 15, 20);
- this.image = new Image("player.png");
- attack = new ArrayList <MeleeWeapon>();
- this.weapon = new Rectangle(x+25,y+15,15,5);
- sprites = new SpriteSheet("walking.png",32,32);
- walkAnim = new Animation(sprites,100);
- weaponWalk = new SpriteSheet("pistolSheet.png",32,32);
- weaponWalkAnim = new Animation(weaponWalk,100);
- walk = new SpriteSheet("feet.png",32,32);
- walkingAnim = new Animation(walk,100);
- meleeAttack = new SpriteSheet("unarmed.png",32,32);
- attackAnim = new Animation(meleeAttack,80);
- bulletSheet = new SpriteSheet("shotm16.png",32,32);
- shootingAnimation = new Animation(bulletSheet,100);
- }
- public void move(int delta){
- if(input.isKeyDown(Input.KEY_W)==true){
- y-=5;
- walkingAnim.getCurrentFrame().setRotation(90);
- }
- if(input.isKeyDown(Input.KEY_A)==true){
- x-=5;
- walkingAnim.getCurrentFrame().setRotation(30);
- }
- if(input.isKeyDown(Input.KEY_S)==true){
- y+=5;
- walkingAnim.getCurrentFrame().setRotation(60);
- }
- if(input.isKeyDown(Input.KEY_D)==true){
- x+=5;
- walkingAnim.getCurrentFrame().setRotation(0);
- }
- if(input.isKeyDown(Input.KEY_D)==true||input.isKeyDown(Input.KEY_S)==true||input.isKeyDown(Input.KEY_W)==true||input.isKeyDown(Input.KEY_A)==true){
- moving = true;
- walkingAnim.start();
- }
- else{
- moving=false;
- }
- if(moving==false){
- walkingAnim.setCurrentFrame(0);
- walkingAnim.stop();
- stamina++;
- if(stamina>=100){
- stamina=100;
- }
- if(stamina<=0){
- stamina=0;
- }
- }
- if(attackCheck==true){
- attackAnim.start();
- }
- if(spawn==true){
- spawn=false;
- }
- hitbox.setLocation(x, y);
- weapon.setLocation(x+25, y+15);
- }
- public boolean getPlayerMovement(){
- return moving;
- }
- //this is where the player and the animations rotate
- public void rotate(GameContainer gc){
- mouseX = input.getMouseX();
- mouseY = input.getMouseY();
- xDistance = mouseX-x;
- yDistance = mouseY-y;
- double angle = Math.toDegrees(Math.atan2(yDistance, xDistance));
- System.out.println("------------------");
- System.out.println("Mouse x: " +mouseX);
- System.out.println("Mouse y: " +mouseY);
- System.out.println("Mouses y: " +angle);
- System.out.println("------------------");
- weaponWalkAnim.getCurrentFrame().setRotation((float) angle);
- walkAnim.getCurrentFrame().setRotation((float) angle);
- attackAnim.getCurrentFrame().setRotation((float) angle);
- shootingAnimation.getCurrentFrame().setRotation((float) angle);
- image.setRotation((float)angle);
- }
- public int getVel(){
- return velocity;
- }
- public void spawnAttack(Input input, Player player){
- for (MeleeWeapon weapon : attack){
- weapon.attack(input, player);
- }
- }
- public void cooldown(int delta){
- if(attackCheck==true&&input.isMouseButtonDown(Input.MOUSE_RIGHT_BUTTON)==false){
- attackTimer+=delta/10;
- if(attackTimer>=10){
- attackTimer=0;
- attackCheck=false;
- }
- }
- else if(shotCheck==true){
- attackTimer+=delta/10;
- if(attackTimer>=10){
- attackTimer=0;
- shotCheck=false;
- }
- }
- }
- public void attack(int delta) throws SlickException{
- if(input.isMouseButtonDown(Input.MOUSE_RIGHT_BUTTON)&&attackCheck==false&&stamina>50&&discarded==true){
- attackCheck=true;
- attackAnim.setLooping(true);
- attack.add(new MeleeWeapon (x, y, 0));
- attackTimer=0;
- }
- if(input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON)&&shotCheck==false&&discarded==false){
- shotCheck=true;
- attackTimer=0;
- }
- }
- public boolean hit(Shape s){
- boolean hit = false;
- for (int i = 0; i < attack.size(); i++){
- if (!hit){
- hit = attack.get(i).getHitbox().intersects(s);
- }
- if(attack.get(i).getHitbox().intersects(s)){
- attack.remove(i);
- hit=true;
- }
- }
- return hit;
- }
- public void setDiscarded(Firearm weapon){
- discarded=weapon.discarded();
- }
- public void setAnimations(String imagePath, String animation, int spriteHeight, int spriteWidth,String bulletSprites) throws SlickException{
- image=new Image(imagePath);
- weaponWalk= new SpriteSheet(animation,32,32);
- bulletSheet = new SpriteSheet(bulletSprites,32,32);
- }
- public void draw(Graphics gfx, Firearm weapon) throws SlickException{
- if(moving==true&&weapon.getShooting()==true&&shotCheck==true&&attackCheck==false&&discarded==false&&weapon.getmagazin()>0||input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON)&&attackCheck==false&&shotCheck==true&&discarded==false&&weapon.getShooting()==true&&weapon.getmagazin()>0){
- walkingAnim.draw(x,y);
- shootingAnimation.draw(x, y);
- }
- else if(moving==true&&attackCheck==false&&discarded==true){
- walkingAnim.draw(x,y);
- walkAnim.draw(x,y);
- }
- else if(moving==false&&attackCheck==false){
- image.draw(x,y);
- if(moving==true){
- }
- }
- else if(discarded==false&&attackCheck==false){
- walkingAnim.draw(x,y);
- weaponWalkAnim.draw(x, y);
- }
- else{
- walkingAnim.draw(x, y);
- attackAnim.draw(x,y);
- attackAnim.setLooping(false);
- }
- for (int i = 0; i < attack.size(); i++){
- gfx.draw(attack.get(i).getHitbox());
- spawn=true;
- if(spawn=true&&attack.get(i).getHitbox().getX()>=x+10||attack.get(i).getHitbox().getY()>=y+10){
- attack.clear();
- }
- else if(spawn=true&&attack.get(i).getHitbox().getX()<=x-10||attack.get(i).getHitbox().getY()<=y-10){
- attack.clear();
- }
- }
- }
- public Shape getHitbox(){
- return hitbox;
- }
- public int getMaxLife(){
- return maxLife;
- }
- public Shape getWeapon(){
- return weapon;
- }
- public int getLife(){
- return life;
- }
- public void setLife(int life){
- this.life=life;
- }
- public boolean getHit(){
- return hit;
- }
- public void setHit(boolean hit){
- this.hit=hit;
- }
- public int getX(){
- return x;
- }
- public int getY(){
- return y;
- }
- public boolean getMoving(){
- return moving;
- }
- import org.newdawn.slick.GameContainer;
- this.numTilesX = map.getWidth();
- this.numTilesY = map.getHeight();
- this.tileWidth = map.getTileWidth();
- this.tileHeight = map.getTileHeight();
- this.mapHeight = this.numTilesX * this.tileWidth;
- this.mapWidth = this.numTilesY * this.tileHeight;
- this.gc = gc;
- //if the camera is at the right or left edge lock it to prevent a black bar
- if(cameraX < 0) cameraX = 0;
- if(cameraX + gc.getWidth() > mapWidth) cameraX = mapWidth - gc.getWidth();
- //if the camera is at the top or bottom edge lock it to prevent a black bar
- if(cameraY < 0) cameraY = 0;
- if(cameraY + gc.getHeight() > mapHeight) cameraY = mapHeight - gc.getHeight();
- //calculate the index of the leftmost tile that is being displayed
- int tileIndexX = (int) (cameraX / tileWidth);
- int tileIndexY = (int) (cameraY / tileHeight);
- //finally draw the section of the map on the screen
- map.render(
- tileOffsetX + offsetX,
- tileOffsetY + offsetY,
- tileIndexX,
- tileIndexY,
- (gc.getWidth() - tileOffsetX) / tileWidth + 1,
- (gc.getHeight() - tileOffsetY) / tileHeight + 1);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement