DEADSOFTWARE

38eac46d69d9b70180df37400c86c70038bfbe34
[cavedroid.git] / core / src / ru / deadsoftware / cavecraft / game / mobs / Pig.java
1 package ru.deadsoftware.cavecraft.game.mobs;
3 import com.badlogic.gdx.graphics.g2d.SpriteBatch;
4 import com.badlogic.gdx.math.MathUtils;
5 import com.badlogic.gdx.math.Rectangle;
6 import com.badlogic.gdx.math.Vector2;
7 import ru.deadsoftware.cavecraft.misc.Assets;
9 public class Pig extends Mob {
11 public Pig(int x, int y) {
12 dir = MathUtils.random(1);
13 pos = new Vector2(x, y);
14 move = new Vector2(-1 + dir * 2, 0);
15 width = 25;
16 height = 18;
17 canJump = false;
18 dead = false;
19 }
21 @Override
22 public void changeDir() {
23 dir = -dir + 1;
24 move.x = -1 + 2 * dir;
25 }
27 @Override
28 public void ai() {
29 if (MathUtils.randomBoolean(.0025f)) changeDir();
30 else if (MathUtils.randomBoolean(.0025f)) {
31 if (move.x != 0f) move.x = 0;
32 else move.x = -1 + 2 * dir;
33 }
34 if (move.x != 0f) anim += ANIM_SPEED;
35 else anim = 0;
36 if (anim >= 60 || anim <= -60) {
37 ANIM_SPEED = -ANIM_SPEED;
38 }
39 }
41 @Override
42 public void draw(SpriteBatch spriteBatch, float x, float y) {
43 Assets.pigSprite[0][1].setRotation(anim);
44 Assets.pigSprite[1][1].setRotation(-anim);
45 //back legs
46 Assets.pigSprite[1][1].setPosition(x - 4 + (9 - dir * 9), y + 6);
47 Assets.pigSprite[1][1].draw(spriteBatch);
48 Assets.pigSprite[1][1].setPosition(x + 17 - (9 * dir), y + 6);
49 Assets.pigSprite[1][1].draw(spriteBatch);
50 //front legs
51 Assets.pigSprite[0][1].setPosition(x - 4 + (9 - dir * 9), y + 6);
52 Assets.pigSprite[0][1].draw(spriteBatch);
53 Assets.pigSprite[0][1].setPosition(x + 17 - (9 * dir), y + 6);
54 Assets.pigSprite[0][1].draw(spriteBatch);
55 //head & body
56 spriteBatch.draw(Assets.pigSprite[dir][0], x, y);
57 }
59 @Override
60 public Rectangle getRect() {
61 return new Rectangle(pos.x, pos.y, width, height);
62 }
64 @Override
65 public int getType() {
66 return 0;
67 }
69 }