Added unit ammo support, multi-ammo inventories
This commit is contained in:
@@ -1,9 +1,11 @@
|
||||
package io.anuke.mindustry.entities;
|
||||
|
||||
import com.badlogic.gdx.utils.Array;
|
||||
import io.anuke.mindustry.resource.*;
|
||||
|
||||
public class UnitInventory {
|
||||
private final AmmoEntry ammo = new AmmoEntry(AmmoType.getByID(0), 0);
|
||||
private Array<AmmoEntry> ammos = new Array<>();
|
||||
private int totalAmmo;
|
||||
private ItemStack item;
|
||||
private int capacity, ammoCapacity;
|
||||
|
||||
@@ -13,15 +15,18 @@ public class UnitInventory {
|
||||
}
|
||||
|
||||
public AmmoType getAmmo() {
|
||||
return ammo.type;
|
||||
return ammos.size == 0 ? null : ammos.peek().type;
|
||||
}
|
||||
|
||||
public boolean hasAmmo(){
|
||||
return ammo.amount > 0;
|
||||
return totalAmmo > 0;
|
||||
}
|
||||
|
||||
public void useAmmo(){
|
||||
ammo.amount --;
|
||||
AmmoEntry entry = ammos.peek();
|
||||
entry.amount --;
|
||||
if(entry.amount == 0) ammos.pop();
|
||||
totalAmmo --;
|
||||
}
|
||||
|
||||
public int ammoCapacity(){
|
||||
@@ -29,13 +34,27 @@ public class UnitInventory {
|
||||
}
|
||||
|
||||
public boolean canAcceptAmmo(AmmoType type){
|
||||
return ammo.amount + type.quantityMultiplier <= ammoCapacity;
|
||||
return totalAmmo + type.quantityMultiplier <= ammoCapacity;
|
||||
}
|
||||
|
||||
public void addAmmo(AmmoType type){
|
||||
if(ammo.type != type) ammo.amount = 0;
|
||||
ammo.type = type;
|
||||
ammo.amount += Math.min((int)type.quantityMultiplier, ammoCapacity - ammo.amount);
|
||||
totalAmmo += type.quantityMultiplier;
|
||||
|
||||
//find ammo entry by type
|
||||
for(int i = ammos.size - 1; i >= 0; i --){
|
||||
AmmoEntry entry = ammos.get(i);
|
||||
|
||||
//if found, put it to the right
|
||||
if(entry.type == type){
|
||||
entry.amount += type.quantityMultiplier;
|
||||
ammos.swap(i, ammos.size-1);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
//must not be found
|
||||
AmmoEntry entry = new AmmoEntry(type, (int)type.quantityMultiplier);
|
||||
ammos.add(entry);
|
||||
}
|
||||
|
||||
public int capacity(){
|
||||
@@ -60,8 +79,8 @@ public class UnitInventory {
|
||||
|
||||
public void clear(){
|
||||
item = null;
|
||||
ammo.amount = 0;
|
||||
ammo.type = AmmoType.getByID(0);
|
||||
ammos.clear();
|
||||
totalAmmo = 0;
|
||||
}
|
||||
|
||||
public boolean hasItem(){
|
||||
|
||||
@@ -40,17 +40,16 @@ public class BaseUnit extends Unit{
|
||||
rotation = Mathf.slerpDelta(rotation, angle, type.rotatespeed);
|
||||
}
|
||||
|
||||
//TODO
|
||||
@Override
|
||||
public boolean acceptsAmmo(Item item) {
|
||||
return false;
|
||||
return type.ammo.containsKey(item) && inventory.canAcceptAmmo(type.ammo.get(item));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addAmmo(Item item) {
|
||||
|
||||
inventory.addAmmo(type.ammo.get(item));
|
||||
}
|
||||
//TODO
|
||||
|
||||
@Override
|
||||
public float getSize() {
|
||||
return 8;
|
||||
|
||||
@@ -2,10 +2,10 @@ package io.anuke.mindustry.entities.units;
|
||||
|
||||
import com.badlogic.gdx.math.Vector2;
|
||||
import com.badlogic.gdx.utils.ObjectSet;
|
||||
import io.anuke.mindustry.content.bullets.TurretBullets;
|
||||
import io.anuke.mindustry.content.fx.Fx;
|
||||
import io.anuke.mindustry.entities.Unit;
|
||||
import io.anuke.mindustry.game.TeamInfo.TeamData;
|
||||
import io.anuke.mindustry.resource.AmmoType;
|
||||
import io.anuke.mindustry.world.Tile;
|
||||
import io.anuke.ucore.core.Effects;
|
||||
import io.anuke.ucore.core.Timers;
|
||||
@@ -52,7 +52,6 @@ public class FlyingUnitType extends UnitType {
|
||||
public void behavior(BaseUnit unit) {
|
||||
vec.set(unit.target.x - unit.x, unit.target.y - unit.y);
|
||||
|
||||
float ang = vec.angle();
|
||||
float len = vec.len();
|
||||
|
||||
float circleLength = 40f;
|
||||
@@ -65,8 +64,11 @@ public class FlyingUnitType extends UnitType {
|
||||
|
||||
unit.velocity.add(vec); //TODO clamp it so it doesn't glitch out at low fps
|
||||
|
||||
if(unit.timer.get(timerReload, reload) && len < range){
|
||||
shoot(unit, TurretBullets.basicIron, unit.rotation, 4f);
|
||||
if(unit.inventory.hasAmmo() && unit.timer.get(timerReload, reload) && len < range){
|
||||
AmmoType ammo = unit.inventory.getAmmo();
|
||||
unit.inventory.useAmmo();
|
||||
|
||||
shoot(unit, ammo, unit.rotation, 4f);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,13 +1,15 @@
|
||||
package io.anuke.mindustry.entities.units;
|
||||
|
||||
import com.badlogic.gdx.utils.Array;
|
||||
import com.badlogic.gdx.utils.ObjectMap;
|
||||
import io.anuke.mindustry.content.fx.ExplosionFx;
|
||||
import io.anuke.mindustry.entities.Bullet;
|
||||
import io.anuke.mindustry.entities.BulletType;
|
||||
import io.anuke.mindustry.entities.Unit;
|
||||
import io.anuke.mindustry.entities.Units;
|
||||
import io.anuke.mindustry.content.fx.ExplosionFx;
|
||||
import io.anuke.mindustry.net.Net;
|
||||
import io.anuke.mindustry.net.NetEvents;
|
||||
import io.anuke.mindustry.resource.AmmoType;
|
||||
import io.anuke.mindustry.resource.Item;
|
||||
import io.anuke.ucore.core.Effects;
|
||||
import io.anuke.ucore.core.Timers;
|
||||
import io.anuke.ucore.util.Angles;
|
||||
@@ -41,6 +43,7 @@ public abstract class UnitType {
|
||||
protected float drag = 0.1f;
|
||||
protected float maxVelocity = 5f;
|
||||
protected float reload = 40f;
|
||||
protected ObjectMap<Item, AmmoType> ammo = new ObjectMap<>();
|
||||
|
||||
public UnitType(String name){
|
||||
this.id = lastid++;
|
||||
@@ -48,12 +51,14 @@ public abstract class UnitType {
|
||||
types.add(this);
|
||||
}
|
||||
|
||||
public abstract void draw(BaseUnit unit);
|
||||
|
||||
public void drawOver(BaseUnit unit){
|
||||
//TODO doesn't do anything
|
||||
protected void setAmmo(AmmoType... types){
|
||||
for(AmmoType type : types){
|
||||
ammo.put(type.item, type);
|
||||
}
|
||||
}
|
||||
|
||||
public abstract void draw(BaseUnit unit);
|
||||
|
||||
public boolean isFlying(){
|
||||
return isFlying;
|
||||
}
|
||||
@@ -93,10 +98,14 @@ public abstract class UnitType {
|
||||
}
|
||||
}
|
||||
|
||||
public void shoot(BaseUnit unit, BulletType type, float rotation, float translation){
|
||||
Bullet.create(type, unit,
|
||||
public void shoot(BaseUnit unit, AmmoType type, float rotation, float translation){
|
||||
Bullet.create(type.bullet, unit,
|
||||
unit.x + Angles.trnsx(rotation, translation),
|
||||
unit.y + Angles.trnsy(rotation, translation), rotation);
|
||||
Effects.effect(type.shootEffect, unit.x + Angles.trnsx(rotation, translation),
|
||||
unit.y + Angles.trnsy(rotation, translation), rotation, unit);
|
||||
Effects.effect(type.smokeEffect, unit.x + Angles.trnsx(rotation, translation),
|
||||
unit.y + Angles.trnsy(rotation, translation), rotation, unit);
|
||||
}
|
||||
|
||||
public void onDeath(BaseUnit unit){
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package io.anuke.mindustry.entities.units.types;
|
||||
|
||||
import io.anuke.mindustry.content.AmmoTypes;
|
||||
import io.anuke.mindustry.entities.Unit;
|
||||
import io.anuke.mindustry.entities.units.BaseUnit;
|
||||
import io.anuke.mindustry.entities.units.FlyingUnitType;
|
||||
@@ -10,6 +11,7 @@ public class Vtol extends FlyingUnitType {
|
||||
|
||||
public Vtol(){
|
||||
super("vtol");
|
||||
setAmmo(AmmoTypes.basicIron);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -30,6 +30,11 @@ public class CoreBlock extends StorageBlock {
|
||||
itemCapacity = 2000;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean acceptItem(Item item, Tile tile, Tile source) {
|
||||
return tile.entity.items.totalItems() < itemCapacity;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void drawSelect(Tile tile){
|
||||
Draw.color("accent");
|
||||
|
||||
Reference in New Issue
Block a user