summaryrefslogtreecommitdiff
path: root/src/main/java/net/tylermurphy/Minecraft/Scene/Player.java
blob: 69e741a073831a1bf2e1309c72398742ae2d8963 (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
package net.tylermurphy.Minecraft.Scene;

import java.io.Serializable;

import net.tylermurphy.Minecraft.Scene.Objects.Transform;
import org.lwjgl.glfw.GLFW;

import net.tylermurphy.Minecraft.Input.Input;
import net.tylermurphy.Minecraft.Scene.Objects.Entity;
import net.tylermurphy.Minecraft.Render.Data.Display;

public class Player extends Entity implements Serializable {
	
	private static final long serialVersionUID = 5135364541978251987L;
	
	public int health = 20;
	
	public boolean isPaused;
	
	public Player(Transform transform) {
		super(transform);
	}

	public void move() {
		if(!isPaused)
			checkInputs();
		else {
			this.currentForwardSpeed = 0;
			this.currentSideSpeed = 0;
		}
		float distance = (float) (currentForwardSpeed * Display.getFrameTimeSeconds() * (isFlying == true ? 2 : 1) * ((isSwimming || isBobbing) && !isFlying  ? .25 : 1) );
		float dx = (float) (distance * Math.sin(Math.toRadians(getTransform().getRotation().y)));
		float dz = (float) (distance * Math.cos(Math.toRadians(getTransform().getRotation().y)));
		float distance2 = (float) (currentSideSpeed * Display.getFrameTimeSeconds() * (isFlying == true ? 2 : 1) * ((isSwimming || isBobbing) && !isFlying  ? .25 : 1) );
		dx += (float) (distance2 * Math.sin(Math.toRadians(90-getTransform().getRotation().y)));
		dz += (float) (distance2 * Math.cos(Math.toRadians(90+getTransform().getRotation().y)));
		if(isInAir && !(isFlying || isSwimming)) upwardsSpeed += GRAVITY * Display.getFrameTimeSeconds();
		upwardsSpeed *= (isSwimming && !isFlying  ? .5 : 1);
		upwardsSpeed *= (isBobbing) ? -1 : 1;
		float dy = (float) (upwardsSpeed * Display.getFrameTimeSeconds());
		
		if(willCollide(dx, 0, 0))
			getTransform().increasePosition(dx, 0, 0);
		
		if(willCollide(0, dy, 0)) {
			getTransform().increasePosition(0, dy, 0);
		}
		
		if(willCollide(0, 0, dz))
			getTransform().increasePosition(0, 0, dz);
		
		if(getTransform().getPosition().y < 0) {
			getTransform().increasePosition(0, 256, 0);
		}
	}
	
	private void checkInputs() {
		int keys_pressed = 0;
		
		if(Input.isKeyDown(GLFW.GLFW_KEY_W)) {
			this.currentForwardSpeed = -RUN_SPEED; keys_pressed++;
		}else if(Input.isKeyDown(GLFW.GLFW_KEY_S)) {
			this.currentForwardSpeed = RUN_SPEED; keys_pressed++;
		}else {
			this.currentForwardSpeed = 0;
		}
		
		if(Input.isKeyDown(GLFW.GLFW_KEY_A)) {
			this.currentSideSpeed = -RUN_SPEED; keys_pressed++;
		}else if(Input.isKeyDown(GLFW.GLFW_KEY_D)) {
			this.currentSideSpeed = RUN_SPEED; keys_pressed++;
		}else {
			this.currentSideSpeed = 0;
		}
		
		if(keys_pressed == 2) {
			this.currentForwardSpeed /=1.25;
			this.currentSideSpeed /=1.25;
		}
		
		if(Input.isKeyDown(GLFW.GLFW_KEY_LEFT_CONTROL)) {
			this.currentForwardSpeed *= 1.5;
			this.currentSideSpeed *= 1.5;
		}
	
		if(Input.isKeyDown(GLFW.GLFW_KEY_SPACE)) {
			if(!isInAir && !(isFlying || isSwimming)) {
				this.upwardsSpeed = JUMP_POWER;
				isInAir = true;
			}else if(isFlying || isSwimming) {
				this.upwardsSpeed = JUMP_POWER;
			}
		} else if(Input.isKeyDown(GLFW.GLFW_KEY_LEFT_SHIFT) && isFlying || isSwimming && !isFlying) {
			this.upwardsSpeed = -JUMP_POWER;
		} else if(isFlying) {
			this.upwardsSpeed = 0;
		}
	}
	
}