Skip to content

Commit

Permalink
several fixes, lock on camera zoom
Browse files Browse the repository at this point in the history
  • Loading branch information
Althoumb committed Jan 19, 2018
1 parent 3661c91 commit 0e2e401
Show file tree
Hide file tree
Showing 8 changed files with 347 additions and 621 deletions.
10 changes: 5 additions & 5 deletions .settings/org.eclipse.jdt.core.prefs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ org.eclipse.jdt.core.formatter.align_fields_grouping_blank_lines=2147483647
org.eclipse.jdt.core.formatter.align_type_members_on_columns=false
org.eclipse.jdt.core.formatter.alignment_for_arguments_in_allocation_expression=16
org.eclipse.jdt.core.formatter.alignment_for_arguments_in_annotation=0
org.eclipse.jdt.core.formatter.alignment_for_arguments_in_enum_constant=0
org.eclipse.jdt.core.formatter.alignment_for_arguments_in_enum_constant=16
org.eclipse.jdt.core.formatter.alignment_for_arguments_in_explicit_constructor_call=16
org.eclipse.jdt.core.formatter.alignment_for_arguments_in_method_invocation=16
org.eclipse.jdt.core.formatter.alignment_for_arguments_in_qualified_allocation_expression=16
Expand All @@ -33,7 +33,7 @@ org.eclipse.jdt.core.formatter.alignment_for_parameters_in_method_declaration=16
org.eclipse.jdt.core.formatter.alignment_for_resources_in_try=80
org.eclipse.jdt.core.formatter.alignment_for_selector_in_method_invocation=16
org.eclipse.jdt.core.formatter.alignment_for_superclass_in_type_declaration=16
org.eclipse.jdt.core.formatter.alignment_for_superinterfaces_in_enum_declaration=0
org.eclipse.jdt.core.formatter.alignment_for_superinterfaces_in_enum_declaration=16
org.eclipse.jdt.core.formatter.alignment_for_superinterfaces_in_type_declaration=16
org.eclipse.jdt.core.formatter.alignment_for_throws_clause_in_constructor_declaration=16
org.eclipse.jdt.core.formatter.alignment_for_throws_clause_in_method_declaration=16
Expand Down Expand Up @@ -283,12 +283,12 @@ org.eclipse.jdt.core.formatter.insert_space_between_empty_parens_in_enum_constan
org.eclipse.jdt.core.formatter.insert_space_between_empty_parens_in_method_declaration=do not insert
org.eclipse.jdt.core.formatter.insert_space_between_empty_parens_in_method_invocation=do not insert
org.eclipse.jdt.core.formatter.join_lines_in_comments=true
org.eclipse.jdt.core.formatter.join_wrapped_lines=true
org.eclipse.jdt.core.formatter.join_wrapped_lines=false
org.eclipse.jdt.core.formatter.keep_else_statement_on_same_line=false
org.eclipse.jdt.core.formatter.keep_empty_array_initializer_on_one_line=false
org.eclipse.jdt.core.formatter.keep_imple_if_on_one_line=false
org.eclipse.jdt.core.formatter.keep_then_statement_on_same_line=false
org.eclipse.jdt.core.formatter.lineSplit=100
org.eclipse.jdt.core.formatter.lineSplit=90
org.eclipse.jdt.core.formatter.never_indent_block_comments_on_first_column=false
org.eclipse.jdt.core.formatter.never_indent_line_comments_on_first_column=false
org.eclipse.jdt.core.formatter.number_of_blank_lines_at_beginning_of_method_body=0
Expand All @@ -306,7 +306,7 @@ org.eclipse.jdt.core.formatter.parentheses_positions_in_try_clause=common_lines
org.eclipse.jdt.core.formatter.put_empty_statement_on_new_line=true
org.eclipse.jdt.core.formatter.tabulation.char=tab
org.eclipse.jdt.core.formatter.tabulation.size=4
org.eclipse.jdt.core.formatter.use_on_off_tags=false
org.eclipse.jdt.core.formatter.use_on_off_tags=true
org.eclipse.jdt.core.formatter.use_tabs_only_for_leading_indentations=false
org.eclipse.jdt.core.formatter.wrap_before_assignment_operator=false
org.eclipse.jdt.core.formatter.wrap_before_binary_operator=true
Expand Down
Binary file added data/characters/sunsprite.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions src/game/MainGameState.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class MainGameState implements DefaultGameState {
@Override
public void enter(GameContainer gc, StateBasedGame sbg) throws SlickException {
gc.getInput().addKeyListener(vp);
gc.getInput().addMouseListener(vp);
}

@Override
Expand Down
152 changes: 152 additions & 0 deletions src/game/RegionGenerator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
package game;

import java.awt.Point;
import java.util.TreeMap;

import org.newdawn.slick.geom.Rectangle;

import game.blocks.Block;
import game.blocks.BlockType;
import game.blocks.SolidBlock;
import util.ImprovedNoise;

public class RegionGenerator {
private static final int CHUNK_HEIGHT = 127;
private static final int CHUNK_SIZE = 63;
private static final int BEDROCK_LAYER = 127;
private static final int CHUNK_BOUNDARY_HEIGHT = (int) (CHUNK_HEIGHT * 0.7);

public static TreeMap<Point, Block> blocks = new TreeMap<>((p1, p2) -> {
if (p1.x == p2.x) {
return p1.y - p2.y;
}
return p1.x - p2.x;
});

RegionGenerator() {

}

RegionGenerator(Rectangle s) {
for (int i = (int) (s.getMinX() - 1); i <= s.getMaxX() + 1; i++) {
for (int j = (int) (s.getMinY() - 1); j <= s.getMaxY() + 1; j++) {
generateWorld(i, j);
}
}
}

public void generateWorld(int x, int y) {
generateWorld(x, y, (int) (1000 * Math.random()));
}

public void generateWorld(int x, int y, int seed) {
Point curpos = new Point(x, y);
if (blocks.containsKey(curpos)) {
return;
}
if (y >= BEDROCK_LAYER) {
blocks.put(curpos, new SolidBlock(BlockType.BEDROCK, x, y));
} else if (y >= 0) {
int chunkStart = x / CHUNK_SIZE * CHUNK_SIZE;
if (x < 0) {
chunkStart -= CHUNK_SIZE;
}
Block[][] chunk = generateChunk(chunkStart, 0, seed++);
for (int i = 0; i < chunk.length; i++) {
for (int j = 0; j < chunk[i].length; j++) {
blocks.put(new Point(i + chunkStart, j), chunk[i][j]);
}
}
} else {
// Do not generate blocks in the air
}
}

private Block[][] generateChunk(int x, int y, int seed) {
long chunkgenerationtime = System.nanoTime();
BiomeType biometype = randombiome(); // selects a random biome

// The blocks
Block[][] blocks = new Block[CHUNK_SIZE][CHUNK_HEIGHT];
int[] heightMap = new int[CHUNK_SIZE];
for (int i = 0; i < heightMap.length; i++) {
heightMap[i] = (int) (20
* ImprovedNoise.noise(seed + 1.0 / CHUNK_SIZE * i, 1, 1)
+ CHUNK_SIZE / 2);
}
for (int i = 0; i < CHUNK_SIZE; i++) {
for (int z = 0; z < heightMap[i]; z++) {
blocks[i][z] = new SolidBlock(BlockType.EMPTY,
(i + x) * Block.BLOCK_SPRITE_SIZE,
(z + y) * Block.BLOCK_SPRITE_SIZE);
}
for (int z = heightMap[i]; z < CHUNK_HEIGHT; z++) {
BlockType type = getType(z - heightMap[i], biometype);
blocks[i][z] = new SolidBlock(type,
(i + x) * Block.BLOCK_SPRITE_SIZE,
(z + y) * Block.BLOCK_SPRITE_SIZE);
}
}

if (biometype == BiomeType.DESERT) {
int height = Math.max(heightMap[0], heightMap[heightMap.length - 1]) + 1;
for (int i = 0; i < heightMap.length; i++) {
for (int z = height; z < CHUNK_HEIGHT; z++) {
if (((SolidBlock) blocks[i][z]).type == BlockType.EMPTY) {
blocks[i][z] = new SolidBlock(BlockType.WATER,
(i + x) * Block.BLOCK_SPRITE_SIZE,
(z + y) * Block.BLOCK_SPRITE_SIZE);
}
}
}
}

if (Viewport.DEBUG_MODE) {
System.out.println((System.nanoTime() - chunkgenerationtime) / 1000000.0
+ " ms to generate chunk of type " + biometype);
}
return blocks;
}

/**
* Get the block type.
*
* @param y
* Y is DISTANCE RELATIVE TO SURFACE.
* @param biome
* @return
*/
private BlockType getType(int y, BiomeType biome) {
if (y < 5 + 2 * Math.random()) {
switch (biome) {
case DESERT:
return BlockType.SAND;
case MOUNTAIN:
return BlockType.STONE;
}

if (y == 0) {
return BlockType.GRASS;
}
return BlockType.DIRT;
}
if (Math.random() < 0.01) {
double val = Math.random();
if (val < 0.1) {
return BlockType.DIAMOND_ORE;
} else if (val < 0.2) {
return BlockType.REDSTONE_ORE;
} else if (val < 0.3) {
return BlockType.GOLD_ORE;
} else {
return BlockType.COAL_ORE;
}
}

return BlockType.STONE;
}

private BiomeType randombiome() { // selects a random biome
return BiomeType.values()[(int) (Math.random() * BiomeType.values().length)];
}
}
83 changes: 76 additions & 7 deletions src/game/Viewport.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package game;

import org.newdawn.slick.Color;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.MouseListener;
import org.newdawn.slick.geom.Rectangle;
import org.newdawn.slick.geom.Shape;
import org.newdawn.slick.geom.Transform;
Expand All @@ -15,11 +17,11 @@
* Useful because it allows the position of the "camera" (viewport) to move
* around
*/
public class Viewport implements DefaultKeyListener {
public class Viewport implements DefaultKeyListener, MouseListener {
private Graphics graphics;
private Vector2f center = new Vector2f(); // in game units
static Vector2f center = new Vector2f(); // in game units
private Vector2f screenDimensions = new Vector2f(); // in pixels
private float scaleFactor = 1f;
private float scaleFactor = 2.5f;
private Vector2f movement = new Vector2f();

private static final float MOVEMENT_FACTOR = 1f;
Expand All @@ -41,7 +43,8 @@ public void draw(Sprite s) {

// Check if the sprite needs to be drawn
Shape resultImageBox = s.getBoundingBox().transform(t);
if (getViewShape().contains(resultImageBox) || getViewShape().intersects(resultImageBox)
if (getViewShape().contains(resultImageBox)
|| getViewShape().intersects(resultImageBox)
|| resultImageBox.contains(getViewShape())) {
Vector2f res = t.transform(s.loc.copy());
int nw = (int) Math.ceil(s.img.getWidth() * scaleFactor);
Expand Down Expand Up @@ -74,6 +77,13 @@ public void setGraphics(Graphics g) {
}

public void update(int delta) {
double darknessvalue = 0.6 + Math
.sin(2.0 * Math.PI * System.currentTimeMillis()
/ World.DAY_NIGHT_DURATION)
* 0.4;
Color BackgroundColor = new Color((int) (darknessvalue * 0),
(int) (darknessvalue * 127), (int) (darknessvalue * 255));
graphics.setBackground(BackgroundColor);
center.add(movement.copy().scale(delta / scaleFactor));
}

Expand Down Expand Up @@ -101,7 +111,8 @@ private Transform getDrawTransform() {
// Note that the transforms are applied in reverse order
// e.g. the first concatenated transform is applied last
Transform[] trans = new Transform[] {
Transform.createTranslateTransform(screenDimensions.x / 2, screenDimensions.y / 2),
Transform.createTranslateTransform(screenDimensions.x / 2,
screenDimensions.y / 2),
Transform.createScaleTransform(scaleFactor, scaleFactor),
Transform.createTranslateTransform(-center.x, -center.y) };

Expand Down Expand Up @@ -157,10 +168,20 @@ public void keyPressed(int key, char c) {
movement.x -= MOVEMENT_FACTOR;
break;
case Input.KEY_MINUS:
scaleFactor *= SCALE_DECREASE;
if (scaleFactor > 20) {
scaleFactor *= SCALE_DECREASE;
}
if (DEBUG_MODE) {
System.out.println("Scale factor of " + scaleFactor);
}
break;
case Input.KEY_EQUALS:
scaleFactor *= SCALE_INCREASE;
if (scaleFactor < 75) {
scaleFactor *= SCALE_INCREASE;
}
if (DEBUG_MODE) {
System.out.println("Scale factor of " + scaleFactor);
}
break;
case Input.KEY_P:
printDebugInfo();
Expand Down Expand Up @@ -189,4 +210,52 @@ public void keyReleased(int key, char c) {
break;
}
}

@Override
public void mouseWheelMoved(int change) {
if (change < 0) {
if (scaleFactor > 20) {
scaleFactor *= SCALE_DECREASE;
}
if (DEBUG_MODE) {
System.out.println("Scale factor of " + scaleFactor);
}
} else {
if (scaleFactor < 75) {
scaleFactor *= SCALE_INCREASE;
}
if (DEBUG_MODE) {
System.out.println("Scale factor of " + scaleFactor);
}
}
}

@Override
public void mouseClicked(int button, int x, int y, int clickCount) {
// TODO Auto-generated method stub
}

@Override
public void mousePressed(int button, int x, int y) {
// TODO Auto-generated method stub

}

@Override
public void mouseReleased(int button, int x, int y) {
// TODO Auto-generated method stub

}

@Override
public void mouseMoved(int oldx, int oldy, int newx, int newy) {
// TODO Auto-generated method stub

}

@Override
public void mouseDragged(int oldx, int oldy, int newx, int newy) {
// TODO Auto-generated method stub

}
}
Loading

0 comments on commit 0e2e401

Please sign in to comment.