From 385255cc7b49fbfd3290497367cbc69919b24d4f Mon Sep 17 00:00:00 2001 From: fred-boy Date: Fri, 20 Apr 2018 22:26:25 +0700 Subject: [PATCH] Add pigs --- android/assets/mobs/pig.png | Bin 0 -> 1665 bytes .../src/ru/deadsoftware/cavecraft/Assets.java | 58 ++++++++++++------ .../cavecraft/game/GamePhysics.java | 1 - .../deadsoftware/cavecraft/game/GameProc.java | 5 +- .../cavecraft/game/GameRenderer.java | 45 +++++++------- .../deadsoftware/cavecraft/game/mobs/Mob.java | 15 ++++- .../deadsoftware/cavecraft/game/mobs/Pig.java | 56 +++++++++++++++++ .../cavecraft/game/objects/Player.java | 2 + 8 files changed, 137 insertions(+), 45 deletions(-) create mode 100644 android/assets/mobs/pig.png create mode 100644 core/src/ru/deadsoftware/cavecraft/game/mobs/Pig.java diff --git a/android/assets/mobs/pig.png b/android/assets/mobs/pig.png new file mode 100644 index 0000000000000000000000000000000000000000..826d7254d952c25974076f31ccfb9a5578e6e386 GIT binary patch literal 1665 zcmV-{27dX8P)VGd000McNliru;sg{94KY(qC0qai1`bI? zK~z}7?UzfBTvru^zw@}Ss;+)izuXRr1Q8YpCYJIikeDJSC7iBqYp613}2iu=UMv7@m+kfGtbflLOMMMcYIk1}1(NlEFC3lavKe z2UWewJ4cEAZo$gN2J@RY>F(S{`}AZD9A3SqoCN-HW1rXm{0Bl0c*}TekNV;sZtR|@ z0gn^#o;(Z~z&Rxk1J)I=7?5nZ@zP5=-dG{sJ3;y$Q-TW>s&m4k$BjU{1d9Q)JHcd+ zUs*wBE%Ow)^s8T+hZ_(FBgdD%{2Z_U^^dqB<9&(51m1vCs;UA!vwL^Rl8C)hl|npe zQOUS_iQyz*nv&^SjW#8ufDFR=h3V1>7=ZoX{6eJ{R(CIutl)d$-i;fqKeJ67GN$jy zYVeZ5WH5%*_h<;Hap-J7+hhE6#E=xP77B+k;EI~vmtTJP1P-rWBP#e!Lyi&K16f$R zcmaCBs)BX6n2AHe)^POhEqoIC5HKmCN!Z-lL9~Ew24#hxt|BKrW~Dki17%T}zrTH* zH{bXZmoGoh7j`$Xu0RWm&5ksv7F(YDUckAE_01iuH%wAbY6pl02?Jg5!fbq&8A)jPb*ObRC(DOR(rVD!@66s!LmB&0sv)TA(`n6z_cNg{2?e`q`_- ztb1(NK~uq^$9j*Z9<9deR!11?IeO8;w7W|u9ck9n ztTq(&I`hN3kTTh>QfcI|4M-fYO+nR6S$p>V4AcW_8&mpLh;7T-=9J(@ym4b6=N->~ z{#kGZVSjcujLy=s43u?=4H2u3adXOzzr6J(f4jL4K>geob?F3dUwK7MWLSbA}P{DY{-bF)Ay6|<2NE10=Nc07$gbZ>;TCL zFCH}!&6%i~!h7T}Vr`~gonoCOtNj!znYRp!Id0^Uu%Mc5Qlw0Xk)kQFLqG;YY#dD+ z!M?xmd8DwJr~1Fwr~1ov>&j1Yb@>sRH%8Pe>mQ2DG#Z{)(zQ#`{Kz;yw}D`m zqWO_=d|sV}=EJMkmQUdJl~*_qnsZd_2%NRZeP|vY0nHy@!2{4d*}bSBsH^aGL7JVI zWoUlw?YESeq4Zei2r-&vX#SW1=RgKwygU66%|yYijTsgJXA5+ZPtiOwNEaX*{5(ME zI6k-uqL`*aLc|)!_rCH~WY*&*6E?p6!txA!faa(M`ov;(aM}xE!aI*xXwyA}migTU z8anU_e1v8I6~S6ZHh@vIG!I4d|4iTkXwE`z2ec~jNtmo{E<-av|N7U^0q_o+GdnN7 zc>Y%WAT+y}$kyYmCG`PfUW#U$Git#YPmbvvG`!!y1JGQT6{hgSzQtA*NB`Mhie^cI zahA-0^~EPM@E|n%lud|{!j;%|&e~*qDVl%yyWg9qgnO!|`Xu@<8$PzXDN%rj00000 LNkvXXu0mjf&cF*X literal 0 HcmV?d00001 diff --git a/core/src/ru/deadsoftware/cavecraft/Assets.java b/core/src/ru/deadsoftware/cavecraft/Assets.java index 7dedb4b..acdebdf 100644 --- a/core/src/ru/deadsoftware/cavecraft/Assets.java +++ b/core/src/ru/deadsoftware/cavecraft/Assets.java @@ -13,7 +13,10 @@ public class Assets { public static BitmapFont minecraftFont; public static Texture charTexture; - public static Sprite[][] playerSkin = new Sprite[2][4]; + public static Sprite[][] playerSprite = new Sprite[2][4]; + + public static Texture pigTexture; + public static Sprite[][] pigSprite = new Sprite[2][2]; public static Sprite shade; @@ -35,36 +38,53 @@ public class Assets { public static TextureRegion touchToggleMode; public static TextureRegion touchSpace; - public static void load() { - minecraftFont = new BitmapFont(Gdx.files.internal("font.fnt"), true); + private static void loadPlayer() { charTexture = new Texture(Gdx.files.internal("mobs/char.png")); //LOOK TO LEFT //head - playerSkin[0][0] = new Sprite(new TextureRegion(charTexture, 0,0,12,12)); - playerSkin[0][0].flip(false,true); + playerSprite[0][0] = new Sprite(new TextureRegion(charTexture, 0,0,12,12)); + playerSprite[0][0].flip(false,true); //body - playerSkin[0][1] = new Sprite(new TextureRegion(charTexture, 0,13,12,12)); - playerSkin[0][1].flip(false,true); + playerSprite[0][1] = new Sprite(new TextureRegion(charTexture, 0,13,12,12)); + playerSprite[0][1].flip(false,true); //hand - playerSkin[0][2] = new Sprite(new TextureRegion(charTexture, 25,5,20,20)); - playerSkin[0][2].flip(false,true); + playerSprite[0][2] = new Sprite(new TextureRegion(charTexture, 25,5,20,20)); + playerSprite[0][2].flip(false,true); //leg - playerSkin[0][3] = new Sprite(new TextureRegion(charTexture, 25,27,20,20)); - playerSkin[0][3].flip(false,true); + playerSprite[0][3] = new Sprite(new TextureRegion(charTexture, 25,27,20,20)); + playerSprite[0][3].flip(false,true); //LOOK TO RIGHT //head - playerSkin[1][0] = new Sprite(new TextureRegion(charTexture, 13,0,12,12)); - playerSkin[1][0].flip(false,true); + playerSprite[1][0] = new Sprite(new TextureRegion(charTexture, 13,0,12,12)); + playerSprite[1][0].flip(false,true); //body - playerSkin[1][1] = new Sprite(new TextureRegion(charTexture, 13,13,12,12)); - playerSkin[1][1].flip(false,true); + playerSprite[1][1] = new Sprite(new TextureRegion(charTexture, 13,13,12,12)); + playerSprite[1][1].flip(false,true); //hand - playerSkin[1][2] = new Sprite(new TextureRegion(charTexture, 37,5,20,20)); - playerSkin[1][2].flip(false,true); + playerSprite[1][2] = new Sprite(new TextureRegion(charTexture, 37,5,20,20)); + playerSprite[1][2].flip(false,true); //leg - playerSkin[1][3] = new Sprite(new TextureRegion(charTexture, 37,27,20,20)); - playerSkin[1][3].flip(false,true); + playerSprite[1][3] = new Sprite(new TextureRegion(charTexture, 37,27,20,20)); + playerSprite[1][3].flip(false,true); + } + + private static void loadPig() { + pigTexture = new Texture(Gdx.files.internal("mobs/pig.png")); + pigSprite[0][0] = new Sprite(new TextureRegion(pigTexture, 0, 0, 25, 12)); + pigSprite[0][0].flip(false,true); + pigSprite[1][0] = new Sprite(new TextureRegion(pigTexture, 0, 12, 25, 12)); + pigSprite[1][0].flip(false,true); + pigSprite[0][1] = new Sprite(new TextureRegion(pigTexture, 4, 26, 12, 12)); + pigSprite[0][1].flip(false,true); + pigSprite[1][1] = new Sprite(new TextureRegion(pigTexture, 16, 26, 12, 12)); + pigSprite[1][1].flip(false,true); + } + + public static void load() { + minecraftFont = new BitmapFont(Gdx.files.internal("font.fnt"), true); + loadPlayer(); + loadPig(); shade = new Sprite(new Texture(Gdx.files.internal("shade.png"))); diff --git a/core/src/ru/deadsoftware/cavecraft/game/GamePhysics.java b/core/src/ru/deadsoftware/cavecraft/game/GamePhysics.java index 8e12e72..3bcbc9b 100644 --- a/core/src/ru/deadsoftware/cavecraft/game/GamePhysics.java +++ b/core/src/ru/deadsoftware/cavecraft/game/GamePhysics.java @@ -131,7 +131,6 @@ public class GamePhysics { } public void update(float delta) { - for (Mob mob : gameProc.mobs) { mob.ai(); mobPhy(mob); diff --git a/core/src/ru/deadsoftware/cavecraft/game/GameProc.java b/core/src/ru/deadsoftware/cavecraft/game/GameProc.java index 47b3100..7ded79f 100644 --- a/core/src/ru/deadsoftware/cavecraft/game/GameProc.java +++ b/core/src/ru/deadsoftware/cavecraft/game/GameProc.java @@ -6,8 +6,8 @@ import com.badlogic.gdx.math.RandomXS128; import com.badlogic.gdx.utils.Array; import com.badlogic.gdx.utils.TimeUtils; import ru.deadsoftware.cavecraft.*; -import ru.deadsoftware.cavecraft.game.mobs.Human; import ru.deadsoftware.cavecraft.game.mobs.Mob; +import ru.deadsoftware.cavecraft.game.mobs.Pig; import ru.deadsoftware.cavecraft.game.objects.Player; public class GameProc { @@ -37,6 +37,9 @@ public class GameProc { physics = new GamePhysics(this); player = new Player(world.getSpawnPoint()); mobs = new Array(); + for (int i=0; i<1024/64; i++) { + mobs.add(new Pig(i*16*64, 0, world)); + } if (!CaveGame.TOUCH) ctrlMode = 1; } diff --git a/core/src/ru/deadsoftware/cavecraft/game/GameRenderer.java b/core/src/ru/deadsoftware/cavecraft/game/GameRenderer.java index 7eed8a7..d104b2f 100644 --- a/core/src/ru/deadsoftware/cavecraft/game/GameRenderer.java +++ b/core/src/ru/deadsoftware/cavecraft/game/GameRenderer.java @@ -6,7 +6,6 @@ import com.badlogic.gdx.graphics.GL20; import com.badlogic.gdx.graphics.OrthographicCamera; import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.graphics.glutils.ShapeRenderer; -import com.badlogic.gdx.math.MathUtils; import com.badlogic.gdx.math.Vector2; import ru.deadsoftware.cavecraft.Assets; import ru.deadsoftware.cavecraft.CaveGame; @@ -108,47 +107,47 @@ public class GameRenderer { } private void drawPlayer(Player pl) { - if (!pl.moveX.equals(Vector2.Zero) || Assets.playerSkin[0][2].getRotation()!=0) { - Assets.playerSkin[0][2].rotate(Mob.ANIM_SPEED); - Assets.playerSkin[1][2].rotate(-Mob.ANIM_SPEED); - Assets.playerSkin[0][3].rotate(-Mob.ANIM_SPEED); - Assets.playerSkin[1][3].rotate(Mob.ANIM_SPEED); + if (!pl.moveX.equals(Vector2.Zero) || Assets.playerSprite[0][2].getRotation()!=0) { + Assets.playerSprite[0][2].rotate(Player.ANIM_SPEED); + Assets.playerSprite[1][2].rotate(-Player.ANIM_SPEED); + Assets.playerSprite[0][3].rotate(-Player.ANIM_SPEED); + Assets.playerSprite[1][3].rotate(Player.ANIM_SPEED); } else { - Assets.playerSkin[0][2].setRotation(0); - Assets.playerSkin[1][2].setRotation(0); - Assets.playerSkin[0][3].setRotation(0); - Assets.playerSkin[1][3].setRotation(0); + Assets.playerSprite[0][2].setRotation(0); + Assets.playerSprite[1][2].setRotation(0); + Assets.playerSprite[0][3].setRotation(0); + Assets.playerSprite[1][3].setRotation(0); } - if (Assets.playerSkin[0][2].getRotation()>=60 || Assets.playerSkin[0][2].getRotation()<=-60) - Mob.ANIM_SPEED = -Mob.ANIM_SPEED; + if (Assets.playerSprite[0][2].getRotation()>=60 || Assets.playerSprite[0][2].getRotation()<=-60) + Player.ANIM_SPEED = -Player.ANIM_SPEED; //back hand - Assets.playerSkin[1][2].setPosition( + Assets.playerSprite[1][2].setPosition( pl.position.x - camera.position.x - 6, pl.position.y - camera.position.y); - Assets.playerSkin[1][2].draw(spriteBatch); + Assets.playerSprite[1][2].draw(spriteBatch); //back leg - Assets.playerSkin[1][3].setPosition( + Assets.playerSprite[1][3].setPosition( pl.position.x - camera.position.x - 6, pl.position.y - camera.position.y + 10); - Assets.playerSkin[1][3].draw(spriteBatch); + Assets.playerSprite[1][3].draw(spriteBatch); //front leg - Assets.playerSkin[0][3].setPosition( + Assets.playerSprite[0][3].setPosition( pl.position.x - camera.position.x - 6, pl.position.y - camera.position.y + 10); - Assets.playerSkin[0][3].draw(spriteBatch); + Assets.playerSprite[0][3].draw(spriteBatch); //head - spriteBatch.draw(Assets.playerSkin[pl.dir][0], + spriteBatch.draw(Assets.playerSprite[pl.dir][0], pl.position.x - camera.position.x - 2, pl.position.y - camera.position.y - 2); //body - spriteBatch.draw(Assets.playerSkin[pl.dir][1], + spriteBatch.draw(Assets.playerSprite[pl.dir][1], pl.position.x - camera.position.x - 2, pl.position.y - camera.position.y + 8); //front hand - Assets.playerSkin[0][2].setPosition( + Assets.playerSprite[0][2].setPosition( pl.position.x - camera.position.x - 6, pl.position.y - camera.position.y); - Assets.playerSkin[0][2].draw(spriteBatch); + Assets.playerSprite[0][2].draw(spriteBatch); } private void drawCreative() { @@ -199,6 +198,7 @@ public class GameRenderer { private void drawGamePlay() { drawWorld(); + Mob.animateMobs(); for (Mob mob : gameProc.mobs) drawMob(mob); drawPlayer(gameProc.player); drawWorldForeground(); @@ -239,6 +239,7 @@ public class GameRenderer { drawString("X: "+(int)(gameProc.player.position.x/16), 0, 40); drawString("Y: "+(int)(gameProc.player.position.y/16), 0, 60); drawString("Seed: "+WorldGen.getSeed(), 0, 80); + drawString("Mobs: "+gameProc.mobs.size, 0, 100); fontBatch.end(); } diff --git a/core/src/ru/deadsoftware/cavecraft/game/mobs/Mob.java b/core/src/ru/deadsoftware/cavecraft/game/mobs/Mob.java index e0ff0e2..c52e78f 100644 --- a/core/src/ru/deadsoftware/cavecraft/game/mobs/Mob.java +++ b/core/src/ru/deadsoftware/cavecraft/game/mobs/Mob.java @@ -3,16 +3,27 @@ package ru.deadsoftware.cavecraft.game.mobs; import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.math.Rectangle; import com.badlogic.gdx.math.Vector2; +import com.badlogic.gdx.utils.Array; +import ru.deadsoftware.cavecraft.Assets; public abstract class Mob { public static int ANIM_SPEED = 6; - + public static int ANIMATION = 0; public Vector2 position; public Vector2 moveX, moveY; - public int width, height, dir, animation, anim_d; + public int width, height, dir; public boolean canJump; + public static void animateMobs() { + Assets.pigSprite[0][1].setRotation(ANIMATION); + Assets.pigSprite[1][1].setRotation(-ANIMATION); + ANIMATION+=ANIM_SPEED; + if (ANIMATION>=60 || ANIMATION<=-60) { + ANIM_SPEED = -ANIM_SPEED; + } + } + public abstract void ai(); public abstract void draw(SpriteBatch spriteBatch, float x, float y); public abstract Rectangle getRect(); diff --git a/core/src/ru/deadsoftware/cavecraft/game/mobs/Pig.java b/core/src/ru/deadsoftware/cavecraft/game/mobs/Pig.java new file mode 100644 index 0000000..0400be0 --- /dev/null +++ b/core/src/ru/deadsoftware/cavecraft/game/mobs/Pig.java @@ -0,0 +1,56 @@ +package ru.deadsoftware.cavecraft.game.mobs; + +import com.badlogic.gdx.graphics.g2d.SpriteBatch; +import com.badlogic.gdx.math.MathUtils; +import com.badlogic.gdx.math.Rectangle; +import com.badlogic.gdx.math.Vector2; +import ru.deadsoftware.cavecraft.Assets; +import ru.deadsoftware.cavecraft.game.GameWorld; + +public class Pig extends Mob{ + + private GameWorld world; + + public Pig(int x, int y, GameWorld world) { + this.world = world; + position = new Vector2(x, y); + moveX = new Vector2(0, 0); + moveY = new Vector2(0, 0); + width = 25; + height = 18; + dir = 0; + canJump = false; + } + + @Override + public void ai() { + if (canJump && position.x>16 && position.x<(world.getWidth()-1)*16 && + world.getForeMap((int)(position.x/16)+(dir*2-1), (int)((position.y+height)/16))>0 && + world.getForeMap((int)(position.x/16)+(dir*2-1), (int)((position.y)/16))==0) + moveY.add(0, -8); + if (MathUtils.randomBoolean(.0001f)) dir++; + if (dir>1) dir = 0; + moveX.set(-1.5f+3*dir,0); + } + + @Override + public void draw(SpriteBatch spriteBatch, float x, float y) { + //back legs + Assets.pigSprite[1][1].setPosition(x-4+(9-dir*9),y+6); + Assets.pigSprite[1][1].draw(spriteBatch); + Assets.pigSprite[1][1].setPosition(x+17-(9*dir),y+6); + Assets.pigSprite[1][1].draw(spriteBatch); + //front legs + Assets.pigSprite[0][1].setPosition(x-4+(9-dir*9),y+6); + Assets.pigSprite[0][1].draw(spriteBatch); + Assets.pigSprite[0][1].setPosition(x+17-(9*dir),y+6); + Assets.pigSprite[0][1].draw(spriteBatch); + //head & body + spriteBatch.draw(Assets.pigSprite[dir][0], x, y); + } + + @Override + public Rectangle getRect() { + return new Rectangle(position.x, position.y, width, height); + } +} diff --git a/core/src/ru/deadsoftware/cavecraft/game/objects/Player.java b/core/src/ru/deadsoftware/cavecraft/game/objects/Player.java index 92c8171..415ed3d 100644 --- a/core/src/ru/deadsoftware/cavecraft/game/objects/Player.java +++ b/core/src/ru/deadsoftware/cavecraft/game/objects/Player.java @@ -8,6 +8,8 @@ import ru.deadsoftware.cavecraft.game.WorldGen; public class Player { + public static int ANIM_SPEED = 6; + public Vector2 position; public Vector2 moveX, moveY; public int width, height, dir, texWidth; -- 2.29.2