Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.ru.tgra.shapes;
- /**
- * Created by Svanur on 12.10.2017.
- */
- public class Enemy {
- private float enemyLocX;
- private float enemyLocZ;
- private float destinationX;
- private float destinationZ;
- private boolean moving;
- private int direction;
- private final float SPEED = 3.f;
- private final int MIN = 1;
- private final int MAX = 4;
- Vector3D velocity;
- int [][] maze;
- Shader shader;
- public Enemy(float enemyLocX, float enemyLocZ, int[][] maze)
- {
- this.enemyLocX = enemyLocX;
- this.enemyLocZ = enemyLocZ;
- this.velocity = new Vector3D(0,0,0);
- shader = new Shader();
- this.maze = maze;
- this.moving = false;
- this.direction = 0;
- }
- public void drawEnemy()
- {
- if(!moving)
- {
- direction = whereToMove();
- }
- move(direction);
- //System.out.println(enemyLocX + " " + enemyLocZ);
- shader.setMaterialDiffuse(1, 0, 0, 1);
- ModelMatrix.main.loadIdentityMatrix();
- ModelMatrix.main.addTranslation(enemyLocX, 1, enemyLocZ);
- shader.setModelMatrix(ModelMatrix.main.getMatrix());
- BoxGraphic.drawSolidCube();
- }
- private boolean hasLeft(int x)
- {
- return (x & 2) != 0;
- }
- private boolean hasRight(int x)
- {
- return (x & 1) != 0;
- }
- private boolean hasUp(int x)
- {
- return (x & 8) != 0;
- }
- private boolean hasDown(int x)
- {
- return (x & 4) != 0;
- }
- private int random() {return (int )(Math.random() * MAX + MIN);}
- private int whereToMove()
- {
- Boolean moveFound = false;
- int random = random();
- int x = Math.round(enemyLocX - 2.5f) / 5;
- int z = Math.round(enemyLocZ + 2.5f) / 5;
- if(z < 0)
- {
- z = -z;
- }
- if(x < 0)
- {
- x = -x;
- }
- System.out.println(x + " " + z);
- System.out.println(enemyLocX + " " + enemyLocZ);
- int cell = maze[z][x];
- System.out.println(cell);
- while(!moveFound)
- {
- if(random == 1 && !hasUp(cell))
- {
- moving = true;
- destinationX = enemyLocX;
- destinationZ = enemyLocZ - 5f;
- break;
- }
- if(random == 2 && !hasRight(cell))
- {
- moving = true;
- destinationX = enemyLocX + 5f;
- destinationZ = enemyLocZ;
- break;
- }
- if(random == 3 && !hasDown(cell))
- {
- moving = true;
- destinationX = enemyLocX;
- destinationZ = enemyLocZ + 5f;
- break;
- }
- if(random == 4 && !hasLeft(cell))
- {
- moving = true;
- destinationX = enemyLocX - 5f;
- destinationZ = enemyLocZ;
- break;
- }
- random = random();
- }
- return random;
- }
- private void move(int dir)
- {
- if((Math.round(enemyLocX * 10.0) / 10.0) == destinationX && (Math.round(enemyLocZ * 10.0) / 10.0) == destinationZ)
- {
- moving = false;
- direction = 0;
- enemyLocX = destinationX;
- enemyLocZ = destinationZ;
- return;
- }
- if(dir == 1)
- {
- enemyLocZ -= 0.01f;
- }
- if(dir == 2)
- {
- enemyLocX += 0.01f;
- }
- if(dir == 3)
- {
- enemyLocZ += 0.01f;
- }
- if(dir == 4)
- {
- enemyLocX -= 0.01f;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement