Re-structured Renderer, bugfixes, implemented EnemySpawn system

This commit is contained in:
Anuken
2017-09-23 11:50:50 -04:00
parent 446e70c52b
commit b3ef1e2f2f
13 changed files with 192 additions and 167 deletions

View File

@@ -1,13 +1,16 @@
package io.anuke.mindustry;
import static io.anuke.mindustry.Vars.*;
import static io.anuke.ucore.core.Core.camera;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Buttons;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector2;
import io.anuke.mindustry.GameState.State;
import io.anuke.mindustry.entities.TileEntity;
import io.anuke.mindustry.input.AndroidInput;
import io.anuke.mindustry.world.Tile;
@@ -20,16 +23,103 @@ import io.anuke.ucore.entities.Entities;
import io.anuke.ucore.entities.Entity;
import io.anuke.ucore.graphics.Cache;
import io.anuke.ucore.graphics.Caches;
import io.anuke.ucore.modules.RendererModule;
import io.anuke.ucore.scene.ui.layout.Unit;
import io.anuke.ucore.scene.utils.Cursors;
import io.anuke.ucore.util.GridMap;
import io.anuke.ucore.util.Mathf;
import io.anuke.ucore.util.Tmp;
public class Renderer{
private static int chunksize = 32;
private static GridMap<Cache> caches = new GridMap<>();
public class Renderer extends RendererModule{
int targetscale = baseCameraScale;
int chunksize = 32;
GridMap<Cache> caches = new GridMap<>();
public static void renderTiles(){
public Renderer(){
Core.cameraScale = baseCameraScale;
pixelate();
Draw.addSurface("shadow", Core.cameraScale);
}
@Override
public void update(){
if(Core.cameraScale != targetscale){
float targetzoom = (float)Core.cameraScale / targetscale;
camera.zoom = Mathf.lerp(camera.zoom, targetzoom, 0.2f*Timers.delta());
if(Mathf.in(camera.zoom, targetzoom, 0.005f)){
camera.zoom = 1f;
Core.cameraScale = targetscale;
camera.viewportWidth = Gdx.graphics.getWidth() / Core.cameraScale;
camera.viewportHeight = Gdx.graphics.getHeight() / Core.cameraScale;
AndroidInput.mousex = Gdx.graphics.getWidth()/2;
AndroidInput.mousey = Gdx.graphics.getHeight()/2;
}
}
if(GameState.is(State.menu)){
clearScreen();
}else{
if(World.core.block() == ProductionBlocks.core){
smoothCamera(player.x, player.y, android ? 0.3f : 0.14f);
}else{
smoothCamera(World.core.worldx(), World.core.worldy(), 0.4f);
}
float prex = camera.position.x, prey = camera.position.y;
updateShake(0.75f);
float prevx = camera.position.x, prevy = camera.position.y;
clampCamera(-tilesize / 2f, -tilesize / 2f, World.pixsize - tilesize / 2f, World.pixsize - tilesize / 2f);
float deltax = camera.position.x - prex, deltay = camera.position.y - prey;
if(android){
player.x += camera.position.x-prevx;
player.y += camera.position.y-prevy;
}
float lastx = camera.position.x, lasty = camera.position.y;
if(android){
camera.position.set((int)camera.position.x, (int)camera.position.y, 0);
if(Gdx.graphics.getHeight()/Core.cameraScale % 2 == 1){
camera.position.add(0, -0.5f, 0);
}
}
drawDefault();
camera.position.set(lastx - deltax, lasty - deltay, 0);
if(Vars.debug){
record();
}
}
}
@Override
public void draw(){
renderTiles();
Entities.draw();
renderPixelOverlay();
}
@Override
public void resize(int width, int height){
super.resize(width, height);
AndroidInput.mousex = Gdx.graphics.getWidth()/2;
AndroidInput.mousey = Gdx.graphics.getHeight()/2;
camera.position.set(player.x, player.y, 0);
}
void renderTiles(){
int chunksx = World.width()/chunksize, chunksy = World.height()/chunksize;
//render the entire map
@@ -48,7 +138,7 @@ public class Renderer{
}
}
OrthographicCamera camera = control.camera;
OrthographicCamera camera = Core.camera;
Draw.end();
@@ -105,14 +195,14 @@ public class Renderer{
}
}
public static void clearTiles(){
public void clearTiles(){
for(Cache cache : caches.values())
cache.dispose();
caches.clear();
}
public static void renderPixelOverlay(){
void renderPixelOverlay(){
if(player.recipe != null && Inventory.hasItems(player.recipe.requirements) && (!ui.hasMouse() || android)){
float x = 0;
@@ -202,11 +292,11 @@ public class Renderer{
}
}
public static void drawHealth(float x, float y, float health, float maxhealth){
void drawHealth(float x, float y, float health, float maxhealth){
drawBar(Color.RED, x, y, health/maxhealth);
}
public static void drawBar(Color color, float x, float y, float fraction){
public void drawBar(Color color, float x, float y, float fraction){
float len = 3;
float offset = 7;
@@ -226,4 +316,20 @@ public class Renderer{
Draw.line(x - len + 1, y - offset, x - len + w, y - offset);
Draw.reset();
}
public void setCameraScale(int amount){
targetscale = amount;
clampScale();
Draw.getSurface("pixel").setScale(targetscale);
Draw.getSurface("shadow").setScale(targetscale);
}
public void scaleCamera(int amount){
setCameraScale(targetscale + amount);
}
public void clampScale(){
targetscale = Mathf.clamp(targetscale, Math.round(Unit.dp.inPixels(3)), Math.round(Unit.dp.inPixels((5))));
}
}