Created
October 1, 2020 13:12
-
-
Save SiAust/ed47d620c1bec3ce3daea3eb60419d5a to your computer and use it in GitHub Desktop.
Weird git merge issue. Duplicated class BaseActor, add "<<< HEAD" to top and my version of BaseActor below, hash at bottom of file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<<<<<<< HEAD | |
package com.mygdx.game; | |
import com.badlogic.gdx.graphics.Camera; | |
import com.badlogic.gdx.graphics.Color; | |
import com.badlogic.gdx.graphics.Texture; | |
import com.badlogic.gdx.graphics.g2d.Animation; | |
import com.badlogic.gdx.graphics.g2d.Batch; | |
import com.badlogic.gdx.graphics.g2d.TextureRegion; | |
import com.badlogic.gdx.math.*; | |
import com.badlogic.gdx.scenes.scene2d.Actor; | |
import com.badlogic.gdx.scenes.scene2d.Stage; | |
import com.badlogic.gdx.utils.Array; | |
import com.badlogic.gdx.utils.viewport.Viewport; | |
import com.sun.tools.javac.code.Attribute; | |
import sun.tools.java.ClassType; | |
import java.util.ArrayList; | |
public class BaseActor extends Actor { | |
private Animation<TextureRegion> animation; | |
private float elapsedTime; | |
private boolean animationPaused; | |
private Vector2 velocity; | |
private Vector2 accelerationVec; | |
private float acceleration; | |
private float maxSpeed; | |
private float deceleration; | |
private float currentMotionAngle; | |
private Polygon boundaryPolygon; | |
private static Rectangle worldBounds; | |
public BaseActor(float x, float y, Stage stage) { | |
super(); | |
this.setPosition(x, y); | |
stage.addActor(this); | |
this.animation = null; | |
this.elapsedTime = 0; | |
this.animationPaused = false; | |
this.velocity = new Vector2(0, 0); | |
this.accelerationVec = new Vector2(0, 0); | |
this.acceleration = 0; | |
this.maxSpeed = 1000; | |
this.deceleration = 0; | |
} | |
public void setAnimation(Animation<TextureRegion> anim) { | |
this.animation = anim; | |
TextureRegion picture = anim.getKeyFrame(0); | |
float width = picture.getRegionWidth(); | |
float height = picture.getRegionHeight(); | |
this.setSize(width, height); | |
this.setOrigin(width/2, height/2); | |
if (this.boundaryPolygon == null) { | |
this.setBoundaryRectangle(); | |
} | |
} | |
public void setAnimationPaused(boolean animationPaused) { | |
this.animationPaused = animationPaused; | |
} | |
public void setDeceleration(float deceleration) { | |
this.deceleration = deceleration; | |
} | |
public void setMaxSpeed(float maxSpeed) { | |
this.maxSpeed = maxSpeed; | |
} | |
@Override | |
public void act(float delta) { | |
super.act(delta); | |
if (!this.animationPaused) this.elapsedTime += delta; | |
} | |
@Override | |
public void draw(Batch batch, float parentAlpha) { | |
super.draw(batch, parentAlpha); | |
Color c = this.getColor(); | |
batch.setColor(c.r, c.g, c.b, c.a); | |
if (this.animation != null && this.isVisible()) { | |
batch.draw(animation.getKeyFrame(this.elapsedTime), | |
this.getX(), this.getY(), this.getOriginX(), this.getOriginY(), | |
this.getWidth(), this.getHeight(), this.getScaleX(), this.getScaleY(), | |
this.getRotation()); | |
} | |
} | |
public Animation<TextureRegion> loadAnimationFromFiles(String[] fileNames, | |
float frameDuration, boolean loop) { | |
Array<TextureRegion> textures = new Array<>(fileNames.length); | |
for (int i = 0; i < fileNames.length; i++) { | |
String fileName = fileNames[i]; | |
Texture texture = new Texture(fileName); | |
texture.setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear); | |
textures.add(new TextureRegion(texture)); | |
} | |
Animation<TextureRegion> anim = new Animation<TextureRegion>(frameDuration, textures); | |
if (loop) { | |
anim.setPlayMode(Animation.PlayMode.LOOP); | |
} else { | |
anim.setPlayMode(Animation.PlayMode.NORMAL); | |
} | |
if (this.animation == null) this.setAnimation(anim); | |
return anim; | |
} | |
public Animation<TextureRegion> loadAnimationFromSheet(String fileName, int rows, int cols, | |
float frameDuration, boolean loop ) { | |
Texture texture = new Texture(fileName); | |
texture.setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear); | |
int width = texture.getWidth() / cols; | |
int height = texture.getHeight() / rows; | |
TextureRegion[][] textures = TextureRegion.split(texture, width, height); | |
Array<TextureRegion> frames = new Array<>(); | |
for (int i = 0; i < rows; i++) { | |
for (int j = 0; j< cols; j++) { | |
frames.add(textures[i][j]); | |
} | |
} | |
Animation<TextureRegion> anim = new Animation<TextureRegion>(frameDuration, frames); | |
if (loop) { | |
anim.setPlayMode(Animation.PlayMode.LOOP); | |
} else { | |
anim.setPlayMode(Animation.PlayMode.NORMAL); | |
} | |
if (this.animation == null) this.setAnimation(anim); | |
return anim; | |
} | |
public Animation<TextureRegion> loadAnimation(String fileName) { | |
return loadAnimationFromFiles(new String[] {fileName}, 1, true); | |
} | |
public boolean isAnimationFinished() { | |
return this.animation.isAnimationFinished(this.elapsedTime); | |
} | |
public void setSpeed(float speed) { | |
if (this.velocity.len() == 0) this.velocity.set(speed, 0); | |
else this.velocity.setLength(speed); | |
} | |
public float getSpeed() { | |
return this.velocity.len(); | |
} | |
public void setMotionAngle(float angle) { | |
this.velocity.setAngle(angle); | |
} | |
public float getMotionAngle() { | |
return this.velocity.angle(); | |
} | |
public boolean isMoving() { | |
return (this.getSpeed() > 0); | |
} | |
public void setAcceleration(float acceleration) { | |
this.acceleration = acceleration; | |
} | |
public void accelerateAtAngle(float angle) { | |
this.accelerationVec.add(new Vector2(this.acceleration, 0).setAngle(angle)); | |
} | |
public void applyPhysics(float dt) { | |
this.velocity.add(this.accelerationVec.x * dt, this.accelerationVec.y * dt); | |
float speed = getSpeed(); | |
if (accelerationVec.len() == 0) speed -= this.deceleration * dt; | |
speed = MathUtils.clamp(speed, 0, this.maxSpeed); | |
this.setSpeed(speed); | |
moveBy(this.velocity.x * dt, this.velocity.y * dt); | |
if (this.velocity.len() != 0) this.setCurrentMotionAngle(this.getMotionAngle()); | |
this.accelerationVec.set(0, 0); | |
} | |
public void setCurrentMotionAngle(float currentMotionAngle) { | |
this.currentMotionAngle = this.getMotionAngle(); | |
} | |
public float getCurrentMotionAngle() { | |
return currentMotionAngle; | |
} | |
public void setBoundaryRectangle() { | |
float w = this.getWidth(); | |
float h = this.getHeight(); | |
float[] vertices = {0, 0, w, 0, w, h, 0, h}; | |
} | |
public void setBoundaryPolygon(int numberOfPoints) { | |
float w = this.getWidth(); | |
float h = this.getHeight(); | |
float[] vertices = new float[2*numberOfPoints]; | |
for (int i = 0; i < numberOfPoints; i++) { | |
float angle = i * 6.28f / numberOfPoints; | |
vertices[2 * i] = (w / 2) * MathUtils.cos(angle) + w / 2; | |
vertices[2 * i + 1] = (h / 2) * MathUtils.sin(angle) + h / 2; | |
} | |
this.boundaryPolygon = new Polygon(vertices); | |
} | |
public Polygon getBoundaryPolygon() { | |
this.boundaryPolygon.setOrigin(this.getOriginX(), this.getOriginY()); | |
this.boundaryPolygon.setPosition(this.getX(), this.getY()); | |
this.boundaryPolygon.setRotation(this.getCurrentMotionAngle()); | |
this.boundaryPolygon.setScale(this.getScaleX(), this.getScaleY()); | |
return this.boundaryPolygon; | |
} | |
public boolean overlaps(BaseActor other) { | |
Polygon actor = this.getBoundaryPolygon(); | |
Polygon anotherActor = other.getBoundaryPolygon(); | |
if (!actor.getBoundingRectangle().overlaps(anotherActor.getBoundingRectangle())) | |
return false; | |
return Intersector.overlapConvexPolygons(actor, anotherActor); | |
} | |
public void centerAtPosition(float x, float y) { | |
this.setPosition(x - this.getWidth() / 2, y - this.getHeight() / 2); | |
} | |
public void centerAtActor(BaseActor other) { | |
this.setPosition(other.getX() - other.getWidth() / 2, | |
other.getY() - other.getHeight() / 2); | |
} | |
public void setOpacity(float opacity) { | |
this.getColor().a = opacity; | |
} | |
public Vector2 preventOverlap(BaseActor other) { | |
Polygon actor = this.getBoundaryPolygon(); | |
Polygon anotherActor = other.getBoundaryPolygon(); | |
if (!actor.getBoundingRectangle().overlaps(anotherActor.getBoundingRectangle())) | |
return null; | |
Intersector.MinimumTranslationVector mtv = new Intersector.MinimumTranslationVector(); | |
boolean overlaps = Intersector.overlapConvexPolygons(actor, anotherActor, mtv); | |
if (!overlaps) return null; | |
this.moveBy(mtv.normal.x * mtv.depth, mtv.normal.y * mtv.depth); | |
return mtv.normal; | |
} | |
public static ArrayList<BaseActor> getList (Stage stage, String className) { | |
ArrayList<BaseActor> listOfActors = new ArrayList<>(); | |
for (Actor actor : stage.getActors()) { | |
if (actor.getClass().getSimpleName().matches(className)) { | |
listOfActors.add((BaseActor) actor); | |
} | |
} | |
return listOfActors; | |
} | |
public static int countSpecificActors(Stage stage, String className) { | |
return BaseActor.getList(stage, className).size(); | |
} | |
public static void setWorldBounds(float width, float height) { | |
worldBounds = new Rectangle(0, 0, width, height); | |
} | |
public static void setWorldBounds(BaseActor background) { | |
BaseActor.setWorldBounds(background.getWidth(), background.getHeight()); | |
} | |
public void boundToWorld() { | |
if (this.getX() < 0) setX(0); | |
if (this.getX() + this.getWidth() > worldBounds.width) setX(worldBounds.width - this.getX()); | |
if (this.getY() < 0) setY(0); | |
if (this.getY() + this.getHeight() > worldBounds.height) setY(worldBounds.height - this.getY()); | |
} | |
public void alignCamera() { | |
Camera cam = this.getStage().getCamera(); | |
cam.position.set(this.getX() + this.getOriginX(), | |
this.getY() + this.getOriginY(), | |
0); | |
cam.position.x = MathUtils.clamp(cam.position.x, | |
cam.viewportWidth/2, | |
worldBounds.width - cam.viewportWidth / 2); | |
cam.position.y = MathUtils.clamp(cam.position.y, | |
cam.viewportHeight/2, | |
worldBounds.height - cam.viewportHeight / 2); | |
} | |
} | |
======= | |
package com.mygdx.game; | |
import com.badlogic.gdx.graphics.Camera; | |
import com.badlogic.gdx.graphics.Color; | |
import com.badlogic.gdx.graphics.Texture; | |
import com.badlogic.gdx.graphics.g2d.Animation; | |
import com.badlogic.gdx.graphics.g2d.Batch; | |
import com.badlogic.gdx.graphics.g2d.TextureRegion; | |
import com.badlogic.gdx.math.*; | |
import com.badlogic.gdx.scenes.scene2d.Actor; | |
import com.badlogic.gdx.scenes.scene2d.Stage; | |
import com.badlogic.gdx.utils.Array; | |
import com.badlogic.gdx.utils.viewport.Viewport; | |
import com.sun.tools.javac.code.Attribute; | |
import sun.tools.java.ClassType; | |
import java.util.ArrayList; | |
public class BaseActor extends Actor { | |
private Animation<TextureRegion> animation; | |
private float elapsedTime; | |
private boolean animationPaused; | |
private Vector2 velocity; | |
private Vector2 accelerationVec; | |
private float acceleration; | |
private float maxSpeed; | |
private float deceleration; | |
private float currentMotionAngle; | |
private Polygon boundaryPolygon; | |
private static Rectangle worldBounds; | |
public BaseActor(float x, float y, Stage stage) { | |
super(); | |
this.setPosition(x, y); | |
stage.addActor(this); | |
this.animation = null; | |
this.elapsedTime = 0; | |
this.animationPaused = false; | |
this.velocity = new Vector2(0, 0); | |
this.accelerationVec = new Vector2(0, 0); | |
this.acceleration = 0; | |
this.maxSpeed = 1000; | |
this.deceleration = 0; | |
} | |
public void setAnimation(Animation<TextureRegion> anim) { | |
this.animation = anim; | |
TextureRegion picture = anim.getKeyFrame(0); | |
float width = picture.getRegionWidth(); | |
float height = picture.getRegionHeight(); | |
this.setSize(width, height); | |
this.setOrigin(width/2, height/2); | |
if (this.boundaryPolygon == null) { | |
this.setBoundaryRectangle(); | |
} | |
} | |
public void setAnimationPaused(boolean animationPaused) { | |
this.animationPaused = animationPaused; | |
} | |
public void setDeceleration(float deceleration) { | |
this.deceleration = deceleration; | |
} | |
public void setMaxSpeed(float maxSpeed) { | |
this.maxSpeed = maxSpeed; | |
} | |
@Override | |
public void act(float delta) { | |
super.act(delta); | |
if (!this.animationPaused) this.elapsedTime += delta; | |
} | |
@Override | |
public void draw(Batch batch, float parentAlpha) { | |
super.draw(batch, parentAlpha); | |
Color c = this.getColor(); | |
batch.setColor(c.r, c.g, c.b, c.a); | |
if (this.animation != null && this.isVisible()) { | |
batch.draw(animation.getKeyFrame(this.elapsedTime), | |
this.getX(), this.getY(), this.getOriginX(), this.getOriginY(), | |
this.getWidth(), this.getHeight(), this.getScaleX(), this.getScaleY(), | |
this.getRotation()); | |
} | |
} | |
public Animation<TextureRegion> loadAnimationFromFiles(String[] fileNames, | |
float frameDuration, boolean loop) { | |
Array<TextureRegion> textures = new Array<>(fileNames.length); | |
for (int i = 0; i < fileNames.length; i++) { | |
String fileName = fileNames[i]; | |
Texture texture = new Texture(fileName); | |
texture.setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear); | |
textures.add(new TextureRegion(texture)); | |
} | |
Animation<TextureRegion> anim = new Animation<TextureRegion>(frameDuration, textures); | |
if (loop) { | |
anim.setPlayMode(Animation.PlayMode.LOOP); | |
} else { | |
anim.setPlayMode(Animation.PlayMode.NORMAL); | |
} | |
if (this.animation == null) this.setAnimation(anim); | |
return anim; | |
} | |
public Animation<TextureRegion> loadAnimationFromSheet(String fileName, int rows, int cols, | |
float frameDuration, boolean loop ) { | |
Texture texture = new Texture(fileName); | |
texture.setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear); | |
int width = texture.getWidth() / cols; | |
int height = texture.getHeight() / rows; | |
TextureRegion[][] textures = TextureRegion.split(texture, width, height); | |
Array<TextureRegion> frames = new Array<>(); | |
for (int i = 0; i < rows; i++) { | |
for (int j = 0; j< cols; j++) { | |
frames.add(textures[i][j]); | |
} | |
} | |
Animation<TextureRegion> anim = new Animation<TextureRegion>(frameDuration, frames); | |
if (loop) { | |
anim.setPlayMode(Animation.PlayMode.LOOP); | |
} else { | |
anim.setPlayMode(Animation.PlayMode.NORMAL); | |
} | |
if (this.animation == null) this.setAnimation(anim); | |
return anim; | |
} | |
public Animation<TextureRegion> loadAnimation(String fileName) { | |
return loadAnimationFromFiles(new String[] {fileName}, 1, true); | |
} | |
public boolean isAnimationFinished() { | |
return this.animation.isAnimationFinished(this.elapsedTime); | |
} | |
public void setSpeed(float speed) { | |
if (this.velocity.len() == 0) this.velocity.set(speed, 0); | |
else this.velocity.setLength(speed); | |
} | |
public float getSpeed() { | |
return this.velocity.len(); | |
} | |
public void setMotionAngle(float angle) { | |
this.velocity.setAngle(angle); | |
} | |
public float getMotionAngle() { | |
return this.velocity.angle(); | |
} | |
public boolean isMoving() { | |
return (this.getSpeed() > 0); | |
} | |
public void setAcceleration(float acceleration) { | |
this.acceleration = acceleration; | |
} | |
public void accelerateAtAngle(float angle) { | |
this.accelerationVec.add(new Vector2(this.acceleration, 0).setAngle(angle)); | |
} | |
public void applyPhysics(float dt) { | |
this.velocity.add(this.accelerationVec.x * dt, this.accelerationVec.y * dt); | |
float speed = getSpeed(); | |
if (accelerationVec.len() == 0) speed -= this.deceleration * dt; | |
speed = MathUtils.clamp(speed, 0, this.maxSpeed); | |
this.setSpeed(speed); | |
moveBy(this.velocity.x * dt, this.velocity.y * dt); | |
if (this.velocity.len() != 0) this.setCurrentMotionAngle(this.getMotionAngle()); | |
this.accelerationVec.set(0, 0); | |
} | |
public void setCurrentMotionAngle(float currentMotionAngle) { | |
this.currentMotionAngle = this.getMotionAngle(); | |
} | |
public float getCurrentMotionAngle() { | |
return currentMotionAngle; | |
} | |
public void setBoundaryRectangle() { | |
float w = this.getWidth(); | |
float h = this.getHeight(); | |
float[] vertices = {0, 0, w, 0, w, h, 0, h}; | |
} | |
public void setBoundaryPolygon(int numberOfPoints) { | |
float w = this.getWidth(); | |
float h = this.getHeight(); | |
float[] vertices = new float[2*numberOfPoints]; | |
for (int i = 0; i < numberOfPoints; i++) { | |
float angle = i * 6.28f / numberOfPoints; | |
vertices[2 * i] = (w / 2) * MathUtils.cos(angle) + w / 2; | |
vertices[2 * i + 1] = (h / 2) * MathUtils.sin(angle) + h / 2; | |
} | |
this.boundaryPolygon = new Polygon(vertices); | |
} | |
public Polygon getBoundaryPolygon() { | |
this.boundaryPolygon.setOrigin(this.getOriginX(), this.getOriginY()); | |
this.boundaryPolygon.setPosition(this.getX(), this.getY()); | |
this.boundaryPolygon.setRotation(this.getCurrentMotionAngle()); | |
this.boundaryPolygon.setScale(this.getScaleX(), this.getScaleY()); | |
return this.boundaryPolygon; | |
} | |
public boolean overlaps(BaseActor other) { | |
Polygon actor = this.getBoundaryPolygon(); | |
Polygon anotherActor = other.getBoundaryPolygon(); | |
if (!actor.getBoundingRectangle().overlaps(anotherActor.getBoundingRectangle())) | |
return false; | |
return Intersector.overlapConvexPolygons(actor, anotherActor); | |
} | |
public void centerAtPosition(float x, float y) { | |
this.setPosition(x - this.getWidth() / 2, y - this.getHeight() / 2); | |
} | |
public void centerAtActor(BaseActor other) { | |
this.setPosition(other.getX() - other.getWidth() / 2, | |
other.getY() - other.getHeight() / 2); | |
} | |
public void setOpacity(float opacity) { | |
this.getColor().a = opacity; | |
} | |
public Vector2 preventOverlap(BaseActor other) { | |
Polygon actor = this.getBoundaryPolygon(); | |
Polygon anotherActor = other.getBoundaryPolygon(); | |
if (!actor.getBoundingRectangle().overlaps(anotherActor.getBoundingRectangle())) | |
return null; | |
Intersector.MinimumTranslationVector mtv = new Intersector.MinimumTranslationVector(); | |
boolean overlaps = Intersector.overlapConvexPolygons(actor, anotherActor, mtv); | |
if (!overlaps) return null; | |
this.moveBy(mtv.normal.x * mtv.depth, mtv.normal.y * mtv.depth); | |
return mtv.normal; | |
} | |
public static ArrayList<BaseActor> getList (Stage stage, String className) { | |
ArrayList<BaseActor> listOfActors = new ArrayList<>(); | |
for (Actor actor : stage.getActors()) { | |
if (actor.getClass().getSimpleName().matches(className)) { | |
listOfActors.add((BaseActor) actor); | |
} | |
} | |
return listOfActors; | |
} | |
public static int countSpecificActors(Stage stage, String className) { | |
return BaseActor.getList(stage, className).size(); | |
} | |
public static void setWorldBounds(float width, float height) { | |
worldBounds = new Rectangle(0, 0, width, height); | |
} | |
public static void setWorldBounds(BaseActor background) { | |
BaseActor.setWorldBounds(background.getWidth(), background.getHeight()); | |
} | |
public void boundToWorld() { | |
if (this.getX() < 0) setX(0); | |
if (this.getX() + this.getWidth() > worldBounds.width) setX(worldBounds.width - this.getX()); | |
if (this.getY() < 0) setY(0); | |
if (this.getY() + this.getHeight() > worldBounds.height) setY(worldBounds.height - this.getY()); | |
} | |
public void alignCamera() { | |
Camera cam = this.getStage().getCamera(); | |
cam.position.set(this.getX() + this.getOriginX(), | |
this.getY() + this.getOriginY(), | |
0); | |
cam.position.x = MathUtils.clamp(cam.position.x, | |
cam.viewportWidth/2, | |
worldBounds.width - cam.viewportWidth / 2); | |
cam.position.y = MathUtils.clamp(cam.position.y, | |
cam.viewportHeight/2, | |
worldBounds.height - cam.viewportHeight / 2); | |
} | |
} | |
>>>>>>> f2e83a8f4dec0ec5085bed149f89b11da9dfa3ea |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment