/
Player.cs
86 lines (83 loc) · 2.37 KB
/
Player.cs
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
using System;
using System.Timers;
namespace BrainBot
{
public class Player:Entity
{
public string name;
public XYZ<double> position;
public Look<float> look;
public double height;
private Minecraft minecraft;
private double speed;
public bool invulnerability = false;
public bool isFlying = false;
public bool canFly = false;
public bool instantDestroy = false;
public bool onGround = false;
public short health = 20;
public short food = 10;
public float saturation = 0;
public XYZ<double> endPosition;
private Timer moving;
public Player (string name, Minecraft minecraft)
{
this.name = name;
this.minecraft = minecraft;
entityID = 0;
this.position = new XYZ<double> (0, 0, 0);
look = new Look<float> (0, 0);
moving = new Timer (50);
moving.Elapsed += delegate( object source, ElapsedEventArgs e ) {
this.nextMove ();
};
moving.Start ();
speed = 5;//per 1 second
//endPosition = new XYZ<double> (0, 0, 0);
}
public void MoveTo (XYZ<double> end)
{
lock (this) {
this.endPosition = end;
}
}
public void changeGround (bool ground)
{
lock (this) {
this.onGround = ground;
this.minecraft.SendPacket (new object[]{(byte)PacketID.Player,this.onGround});
}
}
private double getDistance (XYZ<double> first, XYZ<double> second)
{
return Math.Sqrt(Math.Pow (second.x - first.x, 2) + Math.Pow (second.y - first.y, 2) + Math.Pow (second.z - first.z,2));
}
private void nextMove ()
{
lock (this) {
if (this.endPosition != null && !this.position.Equals (this.endPosition)) {
double realSpeed = this.speed / 1000.0 * moving.Interval;
double distance = getDistance (endPosition, position);
double u = realSpeed / (distance - realSpeed);
XYZ<double> nextPosition = new XYZ<double> (0, 0, 0);
nextPosition.x = (position.x + u * endPosition.x) / (1 + u);
nextPosition.y = (position.y + u * endPosition.y) / (1 + u);
nextPosition.z = (position.z + u * endPosition.z) / (1 + u);
this.position = nextPosition;
if (realSpeed >= distance)
this.position = this.endPosition;
this.minecraft.SendPacket (new object[] {
(byte)PacketID.PlayerPosition,
this.position.x,
this.position.y,
this.position.y + this.height,
this.position.z,
this.onGround
}
);
this.minecraft.map.updateMap ();
}
}
}
}
}