Basic unit rendering

This commit is contained in:
Anuken
2020-02-06 22:08:31 -05:00
parent f83b6728cf
commit eeae5149a1
16 changed files with 195 additions and 233 deletions

View File

@@ -1,57 +0,0 @@
package mindustry.entities.def;
import arc.graphics.g2d.*;
import arc.math.*;
import arc.scene.ui.layout.*;
import arc.util.*;
import mindustry.annotations.Annotations.*;
import mindustry.gen.*;
import mindustry.graphics.*;
import mindustry.ui.*;
import static mindustry.Vars.itemSize;
@Component
abstract class DrawItemsComp implements Drawc, Itemsc, Posc, Rotc{
transient float x, y, rotation;
float itemTime;
//drawn after base
@Override
@MethodPriority(3)
public void draw(){
boolean number = isLocal();
itemTime = Mathf.lerpDelta(itemTime, Mathf.num(hasItem()), 0.05f);
//draw back items
if(itemTime > 0.01f){
float backTrns = 5f;
float size = (itemSize + Mathf.absin(Time.time(), 5f, 1f)) * itemTime;
Draw.mixcol(Pal.accent, Mathf.absin(Time.time(), 5f, 0.5f));
Draw.rect(item().icon(Cicon.medium),
x + Angles.trnsx(rotation + 180f, backTrns),
y + Angles.trnsy(rotation + 180f, backTrns),
size, size, rotation);
Draw.mixcol();
Lines.stroke(1f, Pal.accent);
Lines.circle(
x + Angles.trnsx(rotation + 180f, backTrns),
y + Angles.trnsy(rotation + 180f, backTrns),
(3f + Mathf.absin(Time.time(), 5f, 1f)) * itemTime);
if(isLocal()){
Fonts.outline.draw(stack().amount + "",
x + Angles.trnsx(rotation + 180f, backTrns),
y + Angles.trnsy(rotation + 180f, backTrns) - 3,
Pal.accent, 0.25f * itemTime / Scl.scl(1f), false, Align.center
);
}
Draw.reset();
}
}
}

View File

@@ -1,9 +0,0 @@
package mindustry.entities.def;
import mindustry.annotations.Annotations.*;
import mindustry.gen.*;
@Component
abstract class DrawOverComp implements Drawc{
void drawOver(){}
}

View File

@@ -1,24 +0,0 @@
package mindustry.entities.def;
import arc.graphics.*;
import arc.graphics.g2d.*;
import mindustry.annotations.Annotations.*;
import mindustry.gen.*;
@Component
abstract class DrawShadowComp implements Drawc, Rotc, Flyingc, DrawLayerFlyingShadowsc{
static final float shadowTX = -12, shadowTY = -13, shadowColor = Color.toFloatBits(0, 0, 0, 0.22f);
transient float x, y, rotation;
abstract TextureRegion getShadowRegion();
@Override
public void drawFlyingShadows(){
if(isFlying()){
Draw.color(shadowColor);
Draw.rect(getShadowRegion(), x + shadowTX * elevation(), y + shadowTY * elevation(), rotation - 90);
Draw.color();
}
}
}

View File

@@ -1,9 +0,0 @@
package mindustry.entities.def;
import mindustry.annotations.Annotations.*;
import mindustry.gen.*;
@Component
abstract class DrawUnderComp implements Drawc{
void drawUnder(){}
}

View File

@@ -8,12 +8,14 @@ import mindustry.type.*;
@Component
abstract class ItemsComp implements Posc{
@ReadOnly ItemStack stack = new ItemStack();
float itemTime;
abstract int itemCapacity();
@Override
public void update(){
stack.amount = Mathf.clamp(stack.amount, 0, itemCapacity());
itemTime = Mathf.lerpDelta(itemTime, Mathf.num(hasItem()), 0.05f);
}
Item item(){

View File

@@ -1,48 +1,16 @@
package mindustry.entities.def;
import arc.graphics.*;
import arc.graphics.g2d.*;
import arc.math.*;
import mindustry.annotations.Annotations.*;
import mindustry.gen.*;
import mindustry.world.blocks.*;
@Component
abstract class LegsComp implements Posc, Flyingc, Hitboxc, DrawLayerGroundUnderc{
abstract class LegsComp implements Posc, Flyingc, Hitboxc, DrawLayerGroundUnderc, Unitc, Legsc{
transient float x, y;
float baseRotation, walkTime;
abstract TextureRegion legRegion();
abstract TextureRegion baseRegion();
@Override
public void drawGroundUnder(){
Draw.mixcol(Color.white, hitAlpha());
float ft = Mathf.sin(walkTime * vel().len() * 5f, 6f, 2f + hitSize() / 15f);
Floor floor = floorOn();
if(floor.isLiquid){
Draw.color(Color.white, floor.color, 0.5f);
}
for(int i : Mathf.signs){
Draw.rect(legRegion(),
x + Angles.trnsx(baseRotation, ft * i),
y + Angles.trnsy(baseRotation, ft * i),
legRegion().getWidth() * i * Draw.scl, legRegion().getHeight() * Draw.scl - Mathf.clamp(ft * i, 0, 2), baseRotation - 90);
}
if(floor.isLiquid){
Draw.color(Color.white, floor.color, drownTime() * 0.4f);
}else{
Draw.color(Color.white);
}
Draw.rect(baseRegion(), x, y, baseRotation - 90);
Draw.mixcol();
type().drawLegs(this);
}
}

View File

@@ -66,9 +66,7 @@ abstract class StatusComp implements Posc, Flyingc{
return hasEffect(StatusEffects.boss);
}
boolean isImmune(StatusEffect effect){
return false;
}
abstract boolean isImmune(StatusEffect effect);
Color statusColor(){
if(statuses.size == 0){

View File

@@ -1,9 +1,6 @@
package mindustry.entities.def;
import arc.*;
import arc.graphics.*;
import arc.graphics.g2d.*;
import arc.math.*;
import arc.util.*;
import mindustry.annotations.Annotations.*;
import mindustry.content.*;
@@ -19,25 +16,13 @@ import mindustry.world.blocks.*;
import static mindustry.Vars.*;
@Component
abstract class UnitComp implements Healthc, Velc, Statusc, Teamc, Itemsc, Hitboxc, Rotc, Massc, Unitc, Weaponsc, DrawShadowc, DrawLayerGroundc, DrawLayerFlyingc{
abstract class UnitComp implements Healthc, Velc, Statusc, Teamc, Itemsc, Hitboxc, Rotc, Massc, Unitc, Weaponsc, Drawc,
DrawLayerGroundc, DrawLayerFlyingc, DrawLayerGroundShadowsc, DrawLayerFlyingShadowsc{
transient float x, y, rotation;
private UnitController controller;
private UnitDef type;
TextureRegion baseRegion(){
return type.baseRegion;
}
TextureRegion legRegion(){
return type.legRegion;
}
@Override
public TextureRegion getShadowRegion(){
return type.region;
}
@Override
public float clipSize(){
return type.region.getWidth() * 2f;
@@ -110,29 +95,27 @@ abstract class UnitComp implements Healthc, Velc, Statusc, Teamc, Itemsc, Hitbox
}
@Override
public void drawLight(){
//TODO move
if(type.lightRadius > 0){
renderer.lights.add(x, y, type.lightRadius, type.lightColor, 0.6f);
}
public boolean isImmune(StatusEffect effect){
return type.immunities.contains(effect);
}
@Override
public void draw(){
drawCell();
if(type.lightRadius > 0){
renderer.lights.add(x, y, type.lightRadius, type.lightColor, type.lightOpacity);
}
type.drawBody(this);
type.drawWeapons(this);
if(type.drawCell) type.drawCell(this);
if(type.drawItems) type.drawItems(this);
type.drawLight(this);
}
@Override
public void drawBody(){
Draw.mixcol(Color.white, hitAlpha());
public void drawFlyingShadows(){
if(isFlying()) type.drawShadow(this);
}
Draw.rect(type.region, x, y, rotation - 90);
Draw.reset();
@Override
public void drawGroundShadows(){
type.drawOcclusion(this);
}
@Override
@@ -145,14 +128,6 @@ abstract class UnitComp implements Healthc, Velc, Statusc, Teamc, Itemsc, Hitbox
if(isGrounded()) draw();
}
@Override
public void drawCell(){
//draw power cell - TODO move
Draw.color(Color.black, team().color, healthf() + Mathf.absin(Time.time(), Math.max(healthf() * 5f, 1f), 1f - healthf()));
Draw.rect(type.cellRegion, x, y, rotation() - 90);
Draw.color();
}
@Override
public void killed(){
float explosiveness = 2f + item().explosiveness * stack().amount;

View File

@@ -1,6 +1,5 @@
package mindustry.entities.def;
import arc.graphics.g2d.*;
import arc.math.*;
import arc.util.*;
import mindustry.annotations.Annotations.*;
@@ -14,8 +13,6 @@ import mindustry.type.*;
abstract class WeaponsComp implements Teamc, Posc, Rotc{
transient float x, y, rotation;
/** 1 */
static final int[] one = {1};
/** minimum cursor distance from player, fixes 'cross-eyed' shooting */
static final float minAimDst = 20f;
/** temporary weapon sequence number */
@@ -50,60 +47,40 @@ abstract class WeaponsComp implements Teamc, Posc, Rotc{
public void update(){
for(WeaponMount mount : mounts){
Weapon weapon = mount.weapon;
mount.reload -= Time.delta();
mount.reload = Math.max(mount.reload - Time.delta(), 0);
float rotation = this.rotation - 90;
if(mount.shoot){
float rotation = this.rotation - 90;
//rotate if applicable
if(weapon.rotate){
float axisXOffset = weapon.mirror ? 0f : weapon.x;
float axisX = this.x + Angles.trnsx(rotation, axisXOffset, weapon.y),
axisY = this.y + Angles.trnsy(rotation, axisXOffset, weapon.y);
//rotate if applicable
if(weapon.rotate){
float axisXOffset = weapon.mirror ? 0f : weapon.x;
float axisX = this.x + Angles.trnsx(rotation, axisXOffset, weapon.y),
axisY = this.y + Angles.trnsy(rotation, axisXOffset, weapon.y);
mount.rotation = Angles.moveToward(mount.rotation, Angles.angle(axisX, axisY, mount.aimX, mount.aimY), weapon.rotateSpeed);
}
//shoot if applicable
//TODO only shoot if angle is reached, don't shoot inaccurately
if(mount.reload <= 0){
for(int i : (weapon.mirror && !weapon.alternate ? Mathf.signs : one)){
i *= Mathf.sign(weapon.flipped) * Mathf.sign(mount.side);
//m a t h
float weaponRotation = rotation + (weapon.rotate ? mount.rotation : 0);
float mountX = this.x + Angles.trnsx(rotation, weapon.x * i, weapon.y),
mountY = this.y + Angles.trnsy(rotation, weapon.x * i, weapon.y);
float shootX = mountX + Angles.trnsx(weaponRotation, weapon.shootX * i, weapon.shootY),
shootY = mountY + Angles.trnsy(weaponRotation, weapon.shootX * i, weapon.shootY);
float shootAngle = weapon.rotate ? weaponRotation : Angles.angle(shootX, shootY, mount.aimX, mount.aimY);
shoot(weapon, shootX, shootY, shootAngle);
mount.rotation = Angles.moveToward(mount.rotation, Angles.angle(axisX, axisY, mount.aimX, mount.aimY), weapon.rotateSpeed);
}
mount.side = !mount.side;
mount.reload = weapon.reload;
}
}
}
//shoot if applicable
//TODO only shoot if angle is reached, don't shoot inaccurately
if(mount.reload <= 0.0001f){
for(int i : (weapon.mirror && !weapon.alternate ? Mathf.signs : Mathf.one)){
i *= Mathf.sign(weapon.flipped) * Mathf.sign(mount.side);
/** Draw weapon mounts. */
void drawWeapons(){
for(WeaponMount mount : mounts){
Weapon weapon = mount.weapon;
//m a t h
float weaponRotation = rotation + (weapon.rotate ? mount.rotation : 0);
float mountX = this.x + Angles.trnsx(rotation, weapon.x * i, weapon.y),
mountY = this.y + Angles.trnsy(rotation, weapon.x * i, weapon.y);
float shootX = mountX + Angles.trnsx(weaponRotation, weapon.shootX * i, weapon.shootY),
shootY = mountY + Angles.trnsy(weaponRotation, weapon.shootX * i, weapon.shootY);
float shootAngle = weapon.rotate ? weaponRotation : Angles.angle(shootX, shootY, mount.aimX, mount.aimY);
for(int i : (weapon.mirror ? Mathf.signs : one)){
i *= Mathf.sign(weapon.flipped);
shoot(weapon, shootX, shootY, shootAngle);
}
float rotation = this.rotation - 90 + (weapon.rotate ? mount.rotation : 0);
float trY = weapon.y - (mount.reload / weapon.reload * weapon.recoil) * (weapon.alternate ? Mathf.num(i == Mathf.sign(mount.side)) : 1);
float width = i > 0 ? -weapon.region.getWidth() : weapon.region.getWidth();
Draw.rect(weapon.region,
x + Angles.trnsx(rotation, weapon.x * i, trY),
y + Angles.trnsy(rotation, weapon.x * i, trY),
width * Draw.scl,
weapon.region.getHeight() * Draw.scl,
rotation - 90);
mount.side = !mount.side;
mount.reload = weapon.reload;
}
}
}
}