Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package hu.cig.vob;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.Iterator;
- import java.util.List;
- import android.content.Context;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.graphics.Rect;
- import cug.hu.vob.R;
- public class Robot {
- private Bitmap icon, currIcon, iconShot;
- private int x, y, speedX = 0, speedY = 0, health = 100;
- private final int MOVESPEED = 3, JumpSpeed = 15;
- private boolean isMovingLeft = false, isMovingRight = false,
- isFalling = true, isJumping = false, canJump = true;
- private Rect bottom, horizontal;
- private Context context;
- private List<Bullet> bullets = Collections
- .synchronizedList(new ArrayList<Bullet>());
- public Robot(int in_x, int in_y, Bitmap i, float sc, Context c) {
- context = c;
- icon = i;
- currIcon = icon;
- iconShot = BitmapFactory.decodeResource(c.getResources(),
- R.drawable.shot_right);
- x = in_x * (icon.getWidth()/2);
- y = in_y * (icon.getHeight()/2);
- y = (int) (sc - y);
- // for colliding
- horizontal = new Rect((int) (x + icon.getWidth() * (31.5 / 100)),
- y + 5, (int) (x + icon.getWidth() - icon.getWidth()
- * (31.5 / 100)), y + icon.getHeight() - 5);
- bottom = new Rect((int) (x + icon.getWidth() * (31.5 / 100) + 5), y
- + icon.getHeight() - 10, (int) (x + icon.getWidth()
- - icon.getWidth() * (31.5 / 100) - 5), y + icon.getHeight());
- }
- public void update(List<Block> blocks) {
- int oldX = x, oldY = y;
- if (!(x + speedX < 0 || x + speedX > LevelView.intScreenWidth)) {
- x += speedX;
- }
- updateRect();
- for (int i = 0; i < blocks.size(); i++) {
- if (horizontal.intersect(blocks.get(i).getRect())) {
- stopMovingRight();
- stopMovingLeft();
- x = oldX;
- updateRect();
- }
- }
- if (bullets.size() > 0) {
- Iterator<Bullet> it = bullets.iterator();
- while (it.hasNext()) {
- Bullet b = it.next();
- if (b.getCx() > LevelView.screenWidth) {
- it.remove();
- } else {
- b.update();
- }
- Iterator<Block> itt = blocks.iterator();
- while (itt.hasNext()) {
- Block bb = itt.next();
- if (bb.getRect().intersect(b.getRect())) {
- it.remove();
- }
- }
- }
- }
- // @graviti"
- if (isJumping) {
- y -= speedY;
- speedY--;
- if (speedY == 0) {
- isFalling = true;
- isJumping = false;
- }
- updateRect();
- }
- if (isFalling) {
- y += JumpSpeed - 5;
- canJump = false;
- updateRect();
- }
- for (Block b : blocks) {
- if (bottom.intersect(b.getRect())) {
- y = oldY;
- updateRect();
- canJump = true;
- }
- }
- }
- private void updateRect() {
- horizontal = new Rect((int) (x + icon.getWidth() * (31.5 / 100)),
- y + 10, (int) (x + icon.getWidth() - icon.getWidth()
- * (31.5 / 100)), y + icon.getHeight() - 10);
- bottom = new Rect((int) (x + icon.getWidth() * (31.5 / 100) + 5), y
- + icon.getHeight() - 10, (int) (x + icon.getWidth()
- - icon.getWidth() * (31.5 / 100) - 5), y + icon.getHeight() - 2);
- }
- public void moveLeft() {
- speedX = -MOVESPEED;
- isMovingLeft = true;
- }
- public void moveRight() {
- speedX = MOVESPEED;
- isMovingRight = true;
- }
- public void stopMovingLeft() {
- isMovingLeft = false;
- stop();
- }
- public void stopMovingRight() {
- isMovingRight = false;
- stop();
- }
- public void stop() {
- if (isMovingRight == false && isMovingLeft == false) {
- speedX = 0;
- }
- if (isMovingRight == true && isMovingLeft == false) {
- moveRight();
- }
- if (isMovingRight == false && isMovingLeft == true) {
- moveLeft();
- }
- }
- public Bitmap getIcon() {
- return currIcon;
- }
- public int getX() {
- return x;
- }
- public int getY() {
- return y;
- }
- public int getSpeed() {
- return speedX;
- }
- public boolean isMovingLeft() {
- return isMovingLeft;
- }
- public boolean isMovingRight() {
- return isMovingRight;
- }
- public Rect getBottomRect() {
- return bottom;
- }
- public Rect getHRect() {
- return horizontal;
- }
- public int getHealth() {
- return health;
- }
- public void setX(int x) {
- this.x = x;
- }
- public void setY(int y) {
- this.y = y;
- }
- public void setHealth(int health) {
- this.health = health;
- }
- public void shot() {
- currIcon = iconShot;
- Bullet b = new Bullet((float) (x + iconShot.getWidth()),
- (float) (y + ((26.31 * iconShot.getHeight()) / 100)),
- Helper.convertPx_Dpi(10, context), 2);
- bullets.add(b);
- }
- public void _shot() {
- currIcon = icon;
- }
- public List<Bullet> getBullets() {
- return bullets;
- }
- public void jump() {
- if (!isJumping && canJump) {
- isJumping = true;
- isFalling = false;
- speedY = JumpSpeed;
- }
- }
- public void increaseHealth(int x){
- health += x;
- if(health > 100){
- health = 100;
- }
- }
- public void degreeseHealth(int a) {
- health -= a;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement