Skip to content

Commit

Permalink
Adding block breaking.
Browse files Browse the repository at this point in the history
  • Loading branch information
mk12 committed Dec 15, 2011
1 parent b9c9d4c commit d7a65df
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 22 deletions.
23 changes: 16 additions & 7 deletions src/com/hecticcraft/mycraft/GameController.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,14 @@ final class GameController {
private GameState state;

/**
* Used for detecting mouse clicks.
* Used for detecting left mouse clicks.
*/
private boolean wasMouseButtonDown = false;
private boolean wasLeftMouseButtonDown = false;

/**
* Used for detecting right mouse clicks.
*/
private boolean wasRightMouseButtonDown = false;

/**
* Used for calculating delta time between frames.
Expand Down Expand Up @@ -136,12 +141,15 @@ private float getDeltaTime() {
* @return true if it is down and it wasn't last time this method was called
*/
private boolean wasMouseClicked(MouseButton button) {
// Check if the specified button is down
boolean buttonDown = Mouse.isButtonDown(button.ordinal());
// Check if it is was up before and down now
boolean clicked = !wasMouseButtonDown && buttonDown;
// New becomes old for next call
wasMouseButtonDown = buttonDown;
boolean clicked = false;
if (button == MouseButton.LEFT) {
clicked = !wasLeftMouseButtonDown && buttonDown;
wasLeftMouseButtonDown = buttonDown;
} else if (button == MouseButton.RIGHT) {
clicked = !wasRightMouseButtonDown && buttonDown;
wasRightMouseButtonDown = buttonDown;
}

return clicked;
}
Expand All @@ -161,6 +169,7 @@ void run() {
Keyboard.isKeyDown(Keyboard.KEY_D),
Keyboard.isKeyDown(Keyboard.KEY_SPACE),
Mouse.getDX(), Mouse.getDY(), Mouse.getDWheel() / -120,
wasMouseClicked(MouseButton.LEFT),
wasMouseClicked(MouseButton.RIGHT)),
getDeltaTime());

Expand Down
11 changes: 8 additions & 3 deletions src/com/hecticcraft/mycraft/GameState.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,14 @@ void update(GameStateInputData input, float deltaTime) {

calculateSelectedBlock(chunk);

if (input.placeBlock && selectedBlock != null && newBlock != null) { // won't need newBlock==null when all faces are detected
chunk.setBlockType(newBlock, (byte)1);
listener.gameStateChunkChanged(chunk);
if (selectedBlock != null && newBlock != null) {
if (input.breakBlock) {
chunk.setBlockType(selectedBlock, (byte)0);
listener.gameStateChunkChanged(chunk);
} else if (input.placeBlock) {
chunk.setBlockType(newBlock, (byte)1);
listener.gameStateChunkChanged(chunk);
}
}
}

Expand Down
8 changes: 7 additions & 1 deletion src/com/hecticcraft/mycraft/GameStateInputData.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ final class GameStateInputData {

final int cycleBlock;

final boolean breakBlock;
final boolean placeBlock;

/**
Expand All @@ -68,7 +69,7 @@ final class GameStateInputData {
* @param cycleBlock how many times the Player should cycle the block being held (wraps around)
* @param placeBlock if the Player should place a block
*/
GameStateInputData(boolean forward, boolean backward, boolean left, boolean right, boolean jump, float lookDeltaX, float lookDeltaY, int cycleBlock, boolean placeBlock) {
GameStateInputData(boolean forward, boolean backward, boolean left, boolean right, boolean jump, float lookDeltaX, float lookDeltaY, int cycleBlock, boolean breakBlock, boolean placeBlock) {
this.forward = forward;
this.backward = backward;
this.left = left;
Expand All @@ -77,6 +78,11 @@ final class GameStateInputData {
this.lookDeltaX = lookDeltaX * lookSensitivity;
this.lookDeltaY = lookDeltaY * lookSensitivity;
this.cycleBlock = cycleBlock;

if (breakBlock && placeBlock) {
throw new IllegalArgumentException();
}
this.breakBlock = breakBlock;
this.placeBlock = placeBlock;
}

Expand Down
14 changes: 3 additions & 11 deletions src/com/hecticcraft/mycraft/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,6 @@ public class Player {
*/
private static final float INITAL_JUMP_VELOCITY = 0.25f;

/**
* Whether this Player is on something solid. If false, this Player is in
* the air (either jumping or falling).
*/
private boolean isGrounded = false;

/**
* The height of what the Player is currently standing on.
*/
Expand Down Expand Up @@ -100,7 +94,6 @@ void jump() {
if (height == ground) {
ground = 0;
height += 0.0001f;
isGrounded = false;
velocity = INITAL_JUMP_VELOCITY;
}
}
Expand Down Expand Up @@ -150,10 +143,10 @@ void collision(Chunk chunk) {
camera.setPositionZ((int)Math.round(position.z) + 0.5f);
}
}
/*

if (chunk.getBlockType(new Block((int)Math.round(position.x), (int)(position.y-CAMERA_HEIGHT)-1, (int)(position.z))) != 0) {
ground = ((int)position.y-CAMERA_HEIGHT)-1;
}*/
ground = ((int)position.y-CAMERA_HEIGHT);
}
} catch (ArrayIndexOutOfBoundsException aioobe) {}
// request adjacent chunk...
}
Expand Down Expand Up @@ -187,7 +180,6 @@ void move(GameStateInputData input, float multiplier) {
if (height < ground) {
height = ground;
velocity = 0;
isGrounded = true;
}

camera.setPositionY(height+CAMERA_HEIGHT);
Expand Down

0 comments on commit d7a65df

Please sign in to comment.