Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.vali.lib;
- import com.badlogic.gdx.Gdx;
- import com.badlogic.gdx.graphics.Texture;
- import com.badlogic.gdx.graphics.g2d.SpriteBatch;
- import com.badlogic.gdx.math.MathUtils;
- import com.badlogic.gdx.math.Vector2;
- public class Entity {
- public Texture tex;
- public float x;
- public float y;
- public Vector2 acceleration;
- public Vector2 velocity;
- public Vector2 drag;
- public Vector2 maxVelocity;
- public Vector2 origin;
- public float width;
- public float height;
- public float rotation;
- public float offsetX;
- public float offsetY;
- public float centerX;
- public float centerY;
- public boolean col_top;
- public boolean col_bot;
- public boolean col_left;
- public boolean col_right;
- // DONT MODIFY THESE //
- public static int TOP = 0;
- public static int RIGHT = 1;
- public static int BOT = 2;
- public static int LEFT = 3;
- // END OF RESTRICTION //
- public State currentState;
- Animation currentAnimation;
- private boolean _flipX;
- private boolean _flipY;
- private float _scaleX;
- private float _scaleY;
- public boolean solid;
- String name;
- public String tag;
- public boolean visible = true;
- public boolean alive;
- private boolean in_path = false;
- public float getX(){
- return x;
- }
- public float getY(){
- return y;
- }
- public void setX(float x){
- this.x = x;
- }
- public void setY(float y){
- this.y = y;
- }
- public Entity(float XX,float YY, String path, float spriteWidth, float spriteHeight){
- x = XX;
- y = YY;
- tag = "untagged";
- tex = new Texture(Gdx.files.internal(path));
- solid = true;
- acceleration = new Vector2();
- velocity = new Vector2();
- _scaleX = 1;
- _scaleY = 1;
- drag = new Vector2();
- maxVelocity = new Vector2();
- width = spriteWidth;
- height = spriteHeight;
- alive = true;
- origin = new Vector2();
- origin.x = x + width / 2;
- origin.y = y + height / 2;
- centerX = 0;
- centerY = 0;
- col_bot = false;
- col_top = false;
- col_right = false;
- col_left = false;
- _flipX = true;
- _flipY = false;
- }
- public void update(){
- updateMotion();
- updateAnimation();
- }
- public void updateSelf(){
- updateMotion();
- updateAnimation();
- }
- protected void updateAnimation(){
- if(currentAnimation != null){
- currentAnimation.update();
- }
- }
- protected void updateMotion(){
- velocity.x = MathUtils.clamp(velocity.x, -maxVelocity.x, maxVelocity.x);
- velocity.y = MathUtils.clamp(velocity.y, -maxVelocity.y, maxVelocity.y);
- float d = 0;
- d = drag.x * Gdx.graphics.getDeltaTime();
- if(velocity.x - d > 0)
- velocity.x -= d;
- else if(velocity.x + d < 0)
- velocity.x += d;
- else
- velocity.x = 0;
- d = drag.y * Gdx.graphics.getDeltaTime();
- if(velocity.y - d > 0)
- velocity.y -= d;
- else if(velocity.y + d < 0)
- velocity.y += d;
- else
- velocity.y = 0;
- origin.x = x + width / 2;
- origin.y = y + height / 2;
- x += velocity.x * Gdx.graphics.getDeltaTime();
- y += velocity.y * Gdx.graphics.getDeltaTime();
- }
- public void Collide(String TAG) {
- if(Collision.place_entity(x + velocity.x * Gdx.graphics.getDeltaTime(), y ,width,height, TAG)){
- while(!Collision.place_entity(x + Math.signum(velocity.x), y,width,height, TAG))
- x += Math.signum(velocity.x);
- velocity.x = 0;
- }
- if(Collision.place_entity(x, y + velocity.y * Gdx.graphics.getDeltaTime(), width, height,TAG)){
- while(!Collision.place_entity(x, y + Math.signum(velocity.y) ,width, height , TAG))
- y += Math.signum(velocity.y);
- velocity.y = 0;
- }
- }
- public void setFacingX(boolean FLIP){
- _flipX = FLIP;
- }
- public void setFacingY(boolean FLIP){
- _flipY = FLIP;
- }
- public void playAnimation(Animation anim){
- if(currentAnimation != anim)
- {
- currentAnimation = anim;
- }
- }
- public void setScaleX(float scale){
- _scaleX = scale;
- }
- public void setScaleY(float scale){
- _scaleY = scale;
- }
- public float getScaleX(){
- return _scaleX;
- }
- public float getScaleY(){
- return _scaleY;
- }
- public boolean isTouching(int Direction){
- if(Direction == TOP)
- return col_top;
- if(Direction == BOT)
- return col_bot;
- if(Direction == LEFT)
- return col_left;
- if(Direction == RIGHT)
- return col_right;
- return false;
- }
- public void drawSelf(SpriteBatch sb){
- if(currentAnimation != null)
- sb.draw(tex, x, y, (float)currentAnimation.getSpriteWidth() / 2, (float)currentAnimation.getSpriteHeight() / 2, (float)currentAnimation.getSpriteWidth(), (float)currentAnimation.getSpriteHeight(), _scaleX, _scaleY, rotation, currentAnimation.getSpriteFrame(), 0, (int)currentAnimation.getSpriteWidth(), (int)currentAnimation.getSpriteHeight(), _flipX, _flipY);
- else{
- sb.draw(tex,x,y,centerX, centerY,(float)tex.getWidth(), (float)tex.getHeight(),_scaleX,_scaleY,rotation,0,0,tex.getWidth(),tex.getHeight(),_flipX,_flipY);
- }
- }
- public void draw(SpriteBatch sb){
- drawSelf(sb);
- }
- public static Animation addAnimation(Texture tex, int[] FRAMES, int speed, boolean LOOP, int spriteWidth, int spriteHeight){
- return new Animation(tex,FRAMES,speed,LOOP,spriteWidth, spriteHeight,null);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement