Skip to content

Instantly share code, notes, and snippets.

/EntityClass Secret

Last active May 23, 2016 14:17
Show Gist options
  • Save anonymous/08ed8d43717edb12d9535b75d5bdbfb8 to your computer and use it in GitHub Desktop.
Save anonymous/08ed8d43717edb12d9535b75d5bdbfb8 to your computer and use it in GitHub Desktop.
public Vector2 position;
public Vector2 networkPosition;
boolean isTagged;
public float width = 67;
public float height = 76;
public float maxLinearSpeed = 3f;
public float maxLinearAcceleration = 100f;
public float maxAngularSpeed = 3f;
public float maxAngularAcceleration = 100f;
public float boundingRadius = 0.2f;
public int state;
public int networkState;
public int lastState;
public int lastNetworkState;
public Rectangle hitbox;
public Health hp;
public HealthBar healthBar;
public Vector2 healthBarPosition;
public Vector2 healthBarSize;
public float stateTime;
public boolean doOnce = false;
public SpriteBatch batch;
public TextureRegion currentFrame;
public TextureRegion splitIt;
public Animator animMaker;
public Animation idle;
public Animation walkLeft;
public Animation walkRight;
public Animation idleBackward;
public Animation walkForward;
public Animation walkBackwards;
public Animation walkBackwardsIdle;
public Animation walkLeftIdle;
public Animation walkRightIdle;
public Body entityBody;
public StateManagment stateManagment;
public AttackManagment attackManagment;
public StateMachine<Entity, EntityState> stateMachine;
public Texture emptyT;
public Texture fullT;
public MainGame gameScreen;
public MenuCreate game;
public Timer timer;
public Entity(MainGame gameScreen) {
this.gameScreen = gameScreen;
this.game = gameScreen.game;
position = new Vector2(0,0);
networkPosition = new Vector2(1,1);
state = 0;
networkState = 0;
lastState = 0;
lastNetworkState = 0;
hitbox = new Rectangle();
hitbox.set(position.x, position.y, width, height);
hp = new Health(100, 0, 100);
emptyT = game.manager.get("load/HealthBarEmpty.png",Texture.class);
fullT = game.manager.get("load/HealthBarFull.png",Texture.class);
healthBarPosition = new Vector2(0, 0);
healthBarSize = new Vector2(100,5);
healthBar = new HealthBar(gameScreen, emptyT, fullT, healthBarPosition, healthBarSize, 100f);
batch = new SpriteBatch();
splitIt = game.manager.get("DuengonGainersAtlas/DuengonGainersAtlas",TextureAtlas.class).findRegion("knight");
currentFrame = new TextureRegion();
animMaker = new Animator(0, splitIt);
createAnimations();
timer = new Timer();
}
@Override
public void show() {
}
public void createAnimations(){
}
@Override
public void update() {
stateTime += Gdx.graphics.getDeltaTime();
hp.update();
healthBar.update(hp.currentHealthPoints);
hitbox.set(entityBody.getPosition().x, entityBody.getPosition().y, 67/Box2dVars.UNIT, 76/Box2dVars.UNIT);
timer.scheduleTask(new Task() {
@Override
public void run() {
//updateNetworkPositions();
//updateNetworkStates();
}
}, 1f);
}
public void updateAnimations(){
}
@Override
public void render(OrthographicCamera camera) {
if(!hp.dead()){
update();
updateAnimations();
healthBar.draw();
}
else if(hp.dead() && !doOnce){
onDead();
doOnce = true;
}
}
@Override
public void onDead() {
}
@Override
public void dispose() {
batch.dispose();
fullT.dispose();
emptyT.dispose();
healthBar.dispose();
}
public interface Entity extends Location<Vector2> {
public void show();
public void update();
public void render(OrthographicCamera camera);
public void onDead();
public void dispose();
public void addNetworkEntity();
public void updateNetworkPositions();
public void updateNetworkStates();
public void removeNetworkEntity();
public Vector2 getPosition();
public void setPosition(Vector2 newPosition);
public Vector2 getNetworkPosition();
public void setNetworkPosition(Vector2 newNetworkPosition);
public int getState();
public void setState(int newState);
public int getNetworkState();
public void setNetworkState(int newNetworkState);
public int getLastState();
public void setLastState(int newLastState);
public int getLastNetworkState();
public void setLastNetworkState(int newLastNetworkState);
public Rectangle getHitbox();
public void reduceHealthPoints(float amount);
public void addHealthPoints(float amount);
public float getHealthPoints();
public float getStateTime();
public void setCurrentFrame(TextureRegion frame);
public boolean isAlive();
public void setAlive();
}
@Override
public void render(final float delta) {
lightManager.doStep(delta);
Gdx.gl.glClearColor(0, 0, 0, 0);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
launcher.update();
//------------------------------------------------------------------------
//------------------------Draws the Map-----------------------------------
//------------------------------------------------------------------------
duengon.drawCacher(camera);
//------------------------------------------------------------------------
//------------------------Draws the Players-------------------------------
//------------------------------------------------------------------------
for(MPPlayer mpPlayer : launcher.getPlayersValue()){
otherGuy.pl.setActive(true);
otherGuy.keyPressed = mpPlayer.keyPressed;
otherGuy.lastState = mpPlayer.lastState;
otherGuy.draw(mpPlayer.x, mpPlayer.y, mpPlayer.state, camera);
}
mySelf.render(launcher.getPlayerX(), launcher.getPlayerY(), camera);
//playerEntity.render(camera);
//------------------------------------------------------------------------
//------------------------Draws the Lights--------------------------------
//------------------------------------------------------------------------
p2.setPosition(400/Box2dVars.UNIT,400/Box2dVars.UNIT);
lightManager.rayHandler.setCombinedMatrix(camera.combined,camera.position.x, camera.position.y,camera.viewportWidth, camera.viewportHeight);
lightManager.rayHandler.updateAndRender();
duengon.drawWallLayer(camera);
//------------------------------------------------------------------------
//------------------------Update/Draw Enemys------------------------------
//------------------------------------------------------------------------
if(game.hostServer && launcher.network.client.isConnected() || !game.hostServer && !launcher.network.client.isConnected()){
for(int mobCounter = 0; mobCounter < entitys.size(); mobCounter++){
Mobs mob = entitys.get(mobCounter);
((Enemy) mob).updateHealthBar();
if(mob.isAlive()){
if(!((Enemy)mob).doOnce){
mob.onDead();
PacketRemoveMob removeMob = new PacketRemoveMob();
removeMob.listId = mobCounter;
launcher.network.client.sendTCP(removeMob);
((Enemy)mob).doOnce = true;
}
}
else{
if(camera.frustum.sphereInFrustum(mob.getPosition().x,mob.getPosition().y, 0, 20/Box2dVars.UNIT)){
mob.update(delta);
}
}
};
}
for(int itemCounter = 0; itemCounter < items.size(); itemCounter++){
Item item = items.get(itemCounter);
if(item.pickedUp){
items.remove(itemCounter);
}
else{
if(camera.frustum.sphereInFrustum(item.getPosition().x, item.getPosition().y, 0, 10/Box2dVars.UNIT)){
item.update();
}
}
}
if(!game.hostServer && launcher.network.client.isConnected()){
for(int serverMob = 0; serverMob < launcher.mobsList.size(); serverMob++){
if(camera.frustum.sphereInFrustum((launcher.mobsList.get(serverMob).position.x)/Box2dVars.UNIT,(launcher.mobsList.get(serverMob).position.y)/Box2dVars.UNIT, 0, 20/Box2dVars.UNIT)){
if(launcher.mobsList.get(serverMob).isAlive){
serverEnemyList.get(serverMob).state = launcher.mobsList.get(serverMob).state;
serverEnemyList.get(serverMob).update();
serverEnemyList.get(serverMob).draw(launcher.mobsList.get(serverMob).position);
}
}
}
}
//------------------------------------------------------------------------
//------------------------Call Plugin Render------------------------------
//------------------------------------------------------------------------
for(int i = 0; i < game.loadedPlugins.size(); i++){
try {
game.pluginManager.callRender(camera, i);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
chest.update();
camera.update();
framesPerSecond = Gdx.graphics.getFramesPerSecond();
fps = Integer.toString(framesPerSecond);
/*
batch.begin();
renderer.render(lightManager.world, camera.combined);
batch.end();
*/
batch.begin();
font.draw(batch, fps, Gdx.graphics.getWidth()-100, Gdx.graphics.getHeight()-100);
batch.end();
if(game.hostServer){
batch.begin();
try {
font.draw(batch, "Server hosted on : "+Inet4Address.getLocalHost().getHostAddress(), 0, Gdx.graphics.getHeight()-100);
} catch (UnknownHostException e) {
e.printStackTrace();
}
batch.end();
}
deadScreen.draw();
}
public PlayerEntity(MainGame gameScreen) {
super(gameScreen);
Gdx.input.setInputProcessor(this);
searchFreeSpot();
createBody();
rumble = new Rumble();
for(int i = 0; i < moveDirections.length; i++){ moveDirections[i] = true; }
random = new Random();
setUpLight();
}
//-------------------------------------------------------------
//-------------------Entity Methods----------------------------
//-------------------------------------------------------------
@Override
public void update(){
super.update();
updateRumble();
updateMovement();
updateHealthBarPosition();
lightDelay();
}
@Override
public void render(OrthographicCamera camera){
super.render(camera);
lerpCamera(camera);
batch.setProjectionMatrix(camera.combined);
batch.begin();
batch.draw(currentFrame, (float) (entityBody.getPosition().x-32.5/Box2dVars.UNIT), entityBody.getPosition().y-30/Box2dVars.UNIT, 76/Box2dVars.UNIT, 67/Box2dVars.UNIT);
batch.end();
}
@Override
public void onDead() {
Gdx.app.exit();
}
@Override
public void dispose(){
super.dispose();
}
//-------------------------------------------------------------
//----------------Entity Methods(Update Packets)---------------
//-------------------------------------------------------------
@Override
public void updateNetworkPositions() {
if(getNetworkPosition().x != getPosition().x){
PacketUpdateX packet = new PacketUpdateX();
packet.x = getPosition().x * Box2dVars.UNIT;
gameScreen.launcher.network.client.sendUDP(packet);
setNetworkPosition(new Vector2(getPosition().x * Box2dVars.UNIT, getNetworkPosition().y));
}
if(getNetworkPosition().y != getPosition().y){
PacketUpdateY packet = new PacketUpdateY();
packet.y = getPosition().y * Box2dVars.UNIT;
gameScreen.launcher.network.client.sendUDP(packet);
setNetworkPosition(new Vector2(getNetworkPosition().x, getPosition().y * Box2dVars.UNIT));
}
//System.err.println("Called");
}
@Override
public void updateNetworkStates() {
if(getNetworkState() != getState()){
PacketUpdateState packet = new PacketUpdateState();
packet.state = getState();
gameScreen.launcher.network.client.sendUDP(packet);
setNetworkState(getState());
}
if(getLastNetworkState() != getLastState()){
PacketUpdateState packet = new PacketUpdateState();
packet.state = getState();
packet.lastState = getLastState();
gameScreen.launcher.network.client.sendUDP(packet);
setLastNetworkState(getLastState());
}
if(networkKeyPressed != keyPressed ){
PacketKeyPressed updateKeyPressed = new PacketKeyPressed();
updateKeyPressed.id = gameScreen.launcher.network.client.getID();
updateKeyPressed.pressed = keyPressed;
gameScreen.launcher.network.client.sendTCP(updateKeyPressed);
networkKeyPressed = keyPressed;
}
//System.err.println("Called");
}
@Override
public void render(float posX, float posY, OrthographicCamera camera) {
if(!hp.dead()){
stateTime += Gdx.graphics.getDeltaTime();
lerpCamera(camera);
callWalkAnimations();
lastMovementDirection();
float delay = random.nextFloat()*(0.4f - 0.1f) + 0.1f;
Timer.schedule(new Task(){
@Override
public void run() {
pl.setDistance((float) random.nextFloat()*(550f/Box2dVars.UNIT - 350f/Box2dVars.UNIT) + 350f/Box2dVars.UNIT);
Timer.instance().clear();
}
}, delay);
Gdx.app.postRunnable(new Runnable() {
@Override
public void run() {
pl.setPosition((position.x)/Box2dVars.UNIT,(position.y)/Box2dVars.UNIT);
}
});
batch.setProjectionMatrix(camera.combined);
batch.begin();
batch.draw(currentFrame, (float) (player.getPosition().x-32.5/Box2dVars.UNIT), player.getPosition().y-30/Box2dVars.UNIT, playerWidth/Box2dVars.UNIT, playerHeight/Box2dVars.UNIT);
batch.end();
updateHitbox();
updateHealthBar();
doRumble();
}
else{
pl.setActive(false);
}
}
@Override
public void update(float delta){
if(!hp.dead()){
float x = 0;
float y = 0;
keyPressed = false;
if(Gdx.input.isKeyPressed(Keys.A)){
if(moveDirections[0]){
x -= 2.5f;
}
keyPressed = true;
}
if(Gdx.input.isKeyPressed(Keys.D) ){
if(moveDirections[1]){
x += 2.5f;
}
keyPressed = true;
}
if(Gdx.input.isKeyPressed(Keys.W)){
if(moveDirections[2]){
y += 2.5f;
}
keyPressed = true;
}
if(Gdx.input.isKeyPressed(Keys.S)){
if(moveDirections[3]){
y -= 2.5f;
}
keyPressed = true;
}
player.setLinearVelocity(0f,0f);
player.setAngularVelocity(0f);
player.resetMassData();
if(x != 0){
Vector2 vel = player.getLinearVelocity();
player.setLinearVelocity(x, vel.y);
keyPressed = true;
}
if(y != 0){
Vector2 vel = player.getLinearVelocity();
player.setLinearVelocity(vel.x, y );
keyPressed = true;
}
}
else{
onDead();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment