I am trying to program a minecraft clone. In the current state i have a little world and i can also walk around but now i am stuck with the tick method. I'm calling this tick method in the main method in the while loop.
While loop in the main method:
boolean closeRequested = false;
while(!closeRequested){
mc.tick();
if(Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)){
closeRequested = true;
}
if(Display.isCloseRequested()){
closeRequested = true;
}
Tick Method:
public void tick()
{
while (Keyboard.next()) {
if (Keyboard.getEventKeyState())
{
if (Keyboard.getEventKey() == 28) {
Minecraft.level.save();
}
if (Keyboard.getEventKey() == 2) {
this.paintTexture = 1;
}
if (Keyboard.getEventKey() == 3) {
this.paintTexture = 3;
}
if (Keyboard.getEventKey() == 4) {
this.paintTexture = 4;
}
if (Keyboard.getEventKey() == 5) {
this.paintTexture = 5;
}
if (Keyboard.getEventKey() == 7) {
this.paintTexture = 6;
}
}
}
Minecraft.level.tick();
Minecraft.particleEngine.tick();
Minecraft.player.tick(); //This is the movement
}
Player Tick Method:
public void tick()
{
this.xo = this.x;
this.yo = this.y;
this.zo = this.z;
float xa = 0.0F;
float ya = 0.0F;
if (Keyboard.isKeyDown(19)) {
resetPos();
}
if ((Keyboard.isKeyDown(200)) || (Keyboard.isKeyDown(17))) {
ya -= 1.0F;
}
if ((Keyboard.isKeyDown(208)) || (Keyboard.isKeyDown(31))) {
ya += 1.0F;
}
if ((Keyboard.isKeyDown(203)) || (Keyboard.isKeyDown(30))) {
xa -= 1.0F;
}
if ((Keyboard.isKeyDown(205)) || (Keyboard.isKeyDown(32))) {
xa += 1.0F;
}
if ((Keyboard.isKeyDown(57)) || (Keyboard.isKeyDown(219))) {
if (this.onGround) {
this.yd = 0.5F;
}
}
moveRelative(xa, ya, this.onGround ? 0.1F : 0.02F);
this.yd = ((float)(this.yd - 0.08D));
move(this.xd, this.yd, this.zd);
this.xd *= 0.91F;
this.yd *= 0.98F;
this.zd *= 0.91F;
if (this.onGround)
{
this.xd *= 0.7F;
this.zd *= 0.7F;
}
}
When i call this method like this the player moves like sonic and also its depending on the fps how fast the player moves.
I've got some of this code from a minecraft alpha version but i couldn't find the point where the tick()
method is called.
I want the player to move in a constant speed.
I also have a timer where i can get some variables like timepassed, fps or also the ticks (the timer is also from the original minecraft code).
Add this to your code:
while(!closeRequested){
mc.tick();
//do stuff here that runs every 20 times in a second
Thread.sleep(50); //Sleep for 50 milliseconds, which is default minecraft gametick
}
Without the sleep() the loop will run as fast as it can, causing the problem of yours. A simple way sleep() will make it rest for a moment before countiuing the loop again.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.