com.gamemaker.GameObjects.Player.java Source code

Java tutorial

Introduction

Here is the source code for com.gamemaker.GameObjects.Player.java

Source

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package com.gamemaker.GameObjects;

import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.utils.BooleanArray;

public class Player {
    private Rectangle rect;
    private Vector2 position;
    private Vector2 velocity;
    private int width;
    private int height;
    private int lane = -1;
    private boolean fixToOrigin = false;
    private boolean isRunning;
    private Rectangle BoundRect;
    private Boolean isrestart;

    public Player(float x, float y, int lane, int width, int height) {
        this.width = width;
        this.height = height;
        this.lane = lane;
        position = new Vector2(x, y);
        rect = new Rectangle();
        isRunning = false;
        isrestart = false;
        velocity = new Vector2(0, 0);
        BoundRect = new Rectangle(x, y + 4 * height / 5, width, height / 5);
    }

    public void update(float delta) {

        if ((position.y < 357 && position.y > 355) && lane == 2) {
            if (fixToOrigin = true) {
                position.y = 356;
                velocity.set(0, 0);
                fixToOrigin = false;
            }
        }
        if (position.y > 466) {
            position.y = 466;
            velocity.set(0, 0);
        } else if (position.y < 250) {
            position.y = 250;
            velocity.set(0, 0);
        }

        position.add(velocity.cpy().scl(delta));
        BoundRect.set(position.x, position.y + 4 * height / 5, width, height / 5);
    }

    public void setVelocity(float x, float y) {
        this.velocity.set(x, y);
    }

    public void setRunning(boolean isRunning) {
        this.isRunning = isRunning;
    }

    public float getX() {
        return position.x;
    }

    public float getY() {
        return position.y;
    }

    public int getWidth() {
        return width;
    }

    public int getHeight() {
        return height;
    }

    public int getPannel() {
        return lane;
    }

    public Rectangle getRect() {
        return BoundRect;
    }

    public boolean getIsRunning() {
        return isRunning;
    }

    public void playerSwitchUpToOne() {
        if (lane == -1)
            lane = 1;
        else
            lane--;

    }

    public void playerSwitchUpToTwo() {
        lane--;
        fixToOrigin = true;
    }

    public void playerSwitchDownToTwo() {
        lane++;
        fixToOrigin = true;
    }

    public void playerSwitchDownToThree() {
        if (lane == -1)
            lane = 3;
        else
            lane++;
    }

    public float getUpOrDown() {
        return velocity.y;
    }

    public void restart(float x, float y, int lane) {
        isRunning = false;
        this.lane = lane;
        position.x = x;
        position.y = y;
        velocity.x = 0;
        velocity.y = 0;

    }

    public boolean getisrestart() {
        return isrestart;
    }

    public void setrestart() {
        isrestart = true;
    }
}