summaryrefslogtreecommitdiff
path: root/src/main/java/net/tylermurphy/Minecraft/Scene/Objects/Entity.java
blob: b2e69f40a638327c54b2fb5908513a90cdf44946 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package net.tylermurphy.Minecraft.Scene.Objects;

import java.io.Serializable;

import net.tylermurphy.Minecraft.Scene.World;
import org.joml.Vector3f;

import net.tylermurphy.Minecraft.Chunk.Cube;

public class Entity extends Renderable implements Serializable{

	private static final long serialVersionUID = -1273546184611580473L;
	
	protected static final float RUN_SPEED = 4;
	protected static final float JUMP_POWER = 6;
	protected static final float GRAVITY = -15;
	
	protected float currentForwardSpeed = 0;
	protected float currentSideSpeed = 0;
	protected float upwardsSpeed = 0;
	
	public boolean isInAir = true;
	public boolean isFlying = false;
	public boolean isSwimming = false;
	public boolean isBobbing = false;
	public boolean isFalling = false;
	public boolean wasFalling = false;
	public boolean isDead = false;
	
	public Entity(Transform transform) {
		super(null,transform,0);
	}
	
	public boolean willCollide(float dx, float dy, float dz) {
		float px = getTransform().getPosition().x;
		float py = getTransform().getPosition().y;
		float pz = getTransform().getPosition().z;
		if(getTransform().getPosition().x<0)
			px--;
		if(getTransform().getPosition().z<0)
			pz--;
		int[] nbc = {
				(int) (px+dx+.25f),
				(int) (py+dy),
				(int) (pz+dz+.25f),
				(int) (px+dx+.75f),
				(int) (py+dy+1.9f),
				(int) (pz+dz+.75f)
			};
		
		for(int x = nbc[0]; x<=nbc[3]; x++) {
			for(int y = nbc[1]; y<=nbc[4]; y++) {
				for(int z = nbc[2]; z<=nbc[5]; z++) {
					byte block_head = World.getBlock(x,y,z);
					byte block_current_feet = World.getBlock(x,(int)(py),z);
					byte block_current_upper = World.getBlock(x,(int)(py+1f),z);
					byte block_current_lower = World.getBlock(x,(int)(py+.75f),z);
					if(block_current_feet == 17 ) {
						isSwimming = true;
						isBobbing = false;
					}
					if(block_current_feet == 17 && block_current_lower == 17 && block_current_upper == Cube.AIR){
						isSwimming = false;
						isBobbing = true;
					}
					if(block_current_feet != 17) {
						isSwimming = false;
						isBobbing = false;
					}
					if(dy != 0) {
						if(block_head != Cube.AIR && block_head != 17) {
							isInAir = !(y <= getTransform().getGlobalPosition().y);
							return false;
						} else isInAir = true;
					}else {
						if(block_head != Cube.AIR && block_head != 17) return false;
					}
				}
			}
		}
		return true;
	}
	
	public boolean collides(float x, float y, float z) {
		float px = getTransform().getPosition().x;
		float py = getTransform().getPosition().y;
		float pz = getTransform().getPosition().z;
		if(getTransform().getPosition().x<0)
			px--;
		if(getTransform().getPosition().z<0)
			pz--;
		int[] nbc = {
				(int) (px+.25f),
				(int) (py),
				(int) (pz+.25f),
				(int) (px+.75f),
				(int) (py+1.9f),
				(int) (pz+.75f)
			};
		return x >= nbc[0] && x <= nbc[3] &&
				y >= nbc[1] && y <= nbc[4] &&
				z >= nbc[2] && z <= nbc[5];
	}

}