summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 9988706)
raw | patch | inline | side by side (parent: 9988706)
author | fred-boy <fred-boy@protonmail.com> | |
Wed, 11 Apr 2018 09:26:47 +0000 (16:26 +0700) | ||
committer | fred-boy <fred-boy@protonmail.com> | |
Wed, 11 Apr 2018 09:26:47 +0000 (16:26 +0700) |
core/src/ru/deadsoftware/cavecraft/game/GamePhysics.java | patch | blob | history | |
core/src/ru/deadsoftware/cavecraft/game/GameProc.java | patch | blob | history | |
core/src/ru/deadsoftware/cavecraft/game/GameRenderer.java | patch | blob | history | |
core/src/ru/deadsoftware/cavecraft/game/mobs/Human.java | [new file with mode: 0644] | patch | blob |
core/src/ru/deadsoftware/cavecraft/game/mobs/Mob.java | [new file with mode: 0644] | patch | blob |
diff --git a/core/src/ru/deadsoftware/cavecraft/game/GamePhysics.java b/core/src/ru/deadsoftware/cavecraft/game/GamePhysics.java
index bfde61c6a93f6d503e5d7771a68832d4838319a8..f49eb178b9f9501ebfb783cc0382debde6ac190e 100644 (file)
package ru.deadsoftware.cavecraft.game;
-import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
+import ru.deadsoftware.cavecraft.game.mobs.Mob;
import ru.deadsoftware.cavecraft.game.objects.Player;
public class GamePhysics {
return false;
}
- private void movePlayer(Player pl) {
+ private void playerPhy(Player pl) {
pl.position.add(pl.moveY);
if (checkColl(pl.getRect())) {
pl.canJump = true;
gameProc.renderer.camTargetPos.x = gameProc.world.getWidth()*16-gameProc.renderer.camera.viewportWidth;
}
+ private void mobPhy(Mob mob) {
+ mob.position.add(mob.moveY);
+ if (checkColl(mob.getRect())) {
+ mob.canJump = true;
+ int d = -1;
+ if (mob.moveY.y<0) d=1; else if (mob.moveY.y>0) d=-1;
+ mob.position.y = MathUtils.round(mob.position.y);
+ while (checkColl(mob.getRect())) mob.position.y+=d;
+ mob.moveY.setZero();
+ } else {
+ mob.canJump = false;
+ }
+ mob.moveY.add(gravity);
+ mob.position.add(mob.moveX);
+ if (mob.position.x<32 ||
+ mob.position.x+mob.width>gameProc.world.getWidth()*16-32)
+ mob.position.sub(mob.moveX);
+ if (checkColl(mob.getRect())) {
+ int d = 0;
+ if (mob.moveX.x<0) d=1; else if (mob.moveX.x>0) d=-1;
+ mob.position.x = MathUtils.round(mob.position.x);
+ while (checkColl(mob.getRect())) mob.position.x+=d;
+ }
+}
+
public void update(float delta) {
- movePlayer(gameProc.player);
+
+ for (Mob mob : gameProc.mobs) {
+ mob.ai();
+ mobPhy(mob);
+ }
+ playerPhy(gameProc.player);
+
if (gameProc.renderer.camera.position.x - gameProc.renderer.camTargetPos.x <= 8 &&
gameProc.renderer.camera.position.x - gameProc.renderer.camTargetPos.x >= -8) {
gameProc.renderer.camera.position.x = gameProc.renderer.camTargetPos.x;
diff --git a/core/src/ru/deadsoftware/cavecraft/game/GameProc.java b/core/src/ru/deadsoftware/cavecraft/game/GameProc.java
index 6fc7dd956592fd37fe372bf8ef4e177de3ca77db..4c639125283b841f9baa82ec8a365a046f91f122 100644 (file)
package ru.deadsoftware.cavecraft.game;
+import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.TimeUtils;
+import ru.deadsoftware.cavecraft.game.mobs.Human;
+import ru.deadsoftware.cavecraft.game.mobs.Mob;
import ru.deadsoftware.cavecraft.game.objects.Player;
public class GameProc {
public Player player;
+ public Array<Mob> mobs;
+
public GameWorld world;
public GameRenderer renderer;
public GamePhysics physics;
renderer = new GameRenderer(this);
physics = new GamePhysics(this);
player = new Player();
+ mobs = new Array<Mob>();
+ for (int i=0; i<6; i++) {
+ mobs.add(new Human(64*(i+1),0, this));
+ }
}
public void resetRenderer() {
diff --git a/core/src/ru/deadsoftware/cavecraft/game/GameRenderer.java b/core/src/ru/deadsoftware/cavecraft/game/GameRenderer.java
index 4ab0704af7a016e18dc63956affbe4bd5c4f279b..6a41ea26f1a25e56005adb997dea168b9f1714ec 100644 (file)
import ru.deadsoftware.cavecraft.CaveGame;
import ru.deadsoftware.cavecraft.Items;
import ru.deadsoftware.cavecraft.GameScreen;
+import ru.deadsoftware.cavecraft.game.mobs.Mob;
import ru.deadsoftware.cavecraft.game.objects.Player;
public class GameRenderer {
}
}
+ private void drawMob(Mob mob) {
+ spriteBatch.draw(Assets.playerSkin[mob.dir],
+ mob.position.x - camera.position.x, mob.position.y - camera.position.y);
+ }
+
private void drawPlayer(Player pl) {
spriteBatch.draw(Assets.playerSkin[pl.dir],
pl.position.x - camera.position.x, pl.position.y - camera.position.y);
spriteBatch.begin();
drawWorld();
+ for (Mob mob : gameProc.mobs) {
+ drawMob(mob);
+ }
drawPlayer(gameProc.player);
drawGUI();
spriteBatch.end();
diff --git a/core/src/ru/deadsoftware/cavecraft/game/mobs/Human.java b/core/src/ru/deadsoftware/cavecraft/game/mobs/Human.java
--- /dev/null
@@ -0,0 +1,38 @@
+package ru.deadsoftware.cavecraft.game.mobs;
+
+import com.badlogic.gdx.math.RandomXS128;
+import com.badlogic.gdx.math.Rectangle;
+import com.badlogic.gdx.math.Vector2;
+import ru.deadsoftware.cavecraft.game.GameProc;
+
+public class Human extends Mob{
+
+ private RandomXS128 rand = new RandomXS128();
+ private GameProc gameProc;
+
+ public Human(int x, int y, GameProc gameProc) {
+ this.gameProc = gameProc;
+ position = new Vector2(x, y);
+ moveX = new Vector2(0, 0);
+ moveY = new Vector2(0, 0);
+ width = 8;
+ height = 30;
+ dir = 1;
+ canJump = false;
+ }
+
+ @Override
+ public void ai() {
+ if (canJump && gameProc.world.getForeMap(
+ (int)(position.x/16)+(dir*2-1), (int)(position.y/16)+1)>0)
+ moveY.add(0, -8);
+ if (rand.nextInt(500)>490) dir++;
+ if (dir>1) dir = 0;
+ moveX.setZero();
+ moveX.add(-2+4*dir, 0);
+ }
+
+ public Rectangle getRect() {
+ return new Rectangle(position.x, position.y, width, height);
+ }
+}
diff --git a/core/src/ru/deadsoftware/cavecraft/game/mobs/Mob.java b/core/src/ru/deadsoftware/cavecraft/game/mobs/Mob.java
--- /dev/null
@@ -0,0 +1,16 @@
+package ru.deadsoftware.cavecraft.game.mobs;
+
+import com.badlogic.gdx.math.Rectangle;
+import com.badlogic.gdx.math.Vector2;
+
+public abstract class Mob {
+
+ public Vector2 position;
+ public Vector2 moveX, moveY;
+ public int width, height, dir;
+ public boolean canJump;
+
+ public abstract void ai();
+ public abstract Rectangle getRect();
+
+}