Implemented a basic unit

This commit is contained in:
Anuken 2018-03-17 00:41:56 -04:00
parent ed0b796c5a
commit ba84bb82b4
12 changed files with 82 additions and 25 deletions

View File

@ -1,7 +1,7 @@
#Autogenerated file. Do not modify.
#Fri Mar 16 22:18:26 EDT 2018
#Sat Mar 17 00:38:14 EDT 2018
version=release
androidBuildCode=531
androidBuildCode=533
name=Mindustry
code=3.4
build=custom build

View File

@ -6,6 +6,7 @@ import com.badlogic.gdx.graphics.Color;
import io.anuke.mindustry.core.GameState.State;
import io.anuke.mindustry.entities.Player;
import io.anuke.mindustry.game.EventType.*;
import io.anuke.mindustry.game.Team;
import io.anuke.mindustry.game.Tutorial;
import io.anuke.mindustry.game.UpgradeInventory;
import io.anuke.mindustry.graphics.Fx;
@ -158,7 +159,7 @@ public class Control extends Module{
Events.on(ResetEvent.class, () -> {
upgrades.reset();
player.weaponLeft = player.weaponRight = Weapon.blaster;
player.team = state.team;
player.team = Team.blue;
player.add();
player.heal();

View File

@ -22,9 +22,8 @@ public class GameState{
public GameMode mode = GameMode.waves;
public Difficulty difficulty = Difficulty.normal;
public boolean friendlyFire;
public Team team = Team.none; //the team that the player is on
public ObjectSet<Team> enemyTeams = new ObjectSet<>(), //enemies to the player team
allyTeams = new ObjectSet<>(); //allies to the player team
allyTeams = new ObjectSet<>(); //allies to the player team, includes the player team
public void set(State astate){
Events.fire(StateChangeEvent.class, state, astate);

View File

@ -55,9 +55,9 @@ public class Logic extends Module {
state.gameOver = false;
state.inventory.clearItems();
state.allyTeams.clear();
state.allyTeams.add(Team.blue);
state.enemyTeams.clear();
state.enemyTeams.add(Team.red);
state.team = Team.blue;
Timers.clear();
Entities.clear();
@ -129,7 +129,15 @@ public class Logic extends Module {
Entities.update(playerGroup);
for(EntityGroup group : unitGroups){
if(!group.isEmpty()) Entities.collideGroups(bulletGroup, group);
if(!group.isEmpty()){
Entities.collideGroups(bulletGroup, group);
for(EntityGroup other : unitGroups){
if(!other.isEmpty()){
Entities.collideGroups(group, other);
}
}
}
}
Entities.collideGroups(bulletGroup, playerGroup);

View File

@ -2,6 +2,7 @@ package io.anuke.mindustry.entities;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.math.Vector2;
import io.anuke.mindustry.game.Team;
import io.anuke.mindustry.graphics.Fx;
import io.anuke.mindustry.net.Net;
import io.anuke.mindustry.net.NetEvents;
@ -291,6 +292,7 @@ public class Player extends Unit{
buffer.putInt(Color.rgba8888(color));
buffer.putFloat(x);
buffer.putFloat(y);
buffer.put((byte)team.ordinal());
}
@Override
@ -306,6 +308,7 @@ public class Player extends Unit{
color.set(buffer.getInt());
x = buffer.getFloat();
y = buffer.getFloat();
team = Team.values()[buffer.get()];
setNet(x, y);
}

View File

@ -33,7 +33,8 @@ public class Units {
}
}
public static Unit getClosestEnemies(Team team, float x, float y, float range, Predicate<Unit> predicate){
/**Returns the closest enemy of this team. Filter by predicate.*/
public static Unit getClosestEnemy(Team team, float x, float y, float range, Predicate<Unit> predicate){
Unit[] result = {null};
float[] cdist = {0};
@ -70,7 +71,11 @@ public class Units {
/**Iterates over all units that are enemies of this team.*/
public static void getNearbyEnemies(Team team, Rectangle rect, Consumer<Unit> cons){
//check if it's an ally team to the 'main team'
boolean ally = state.team == team || state.allyTeams.contains(team);
boolean ally = state.allyTeams.contains(team);
boolean enemy = state.enemyTeams.contains(team);
//this team isn't even in the game, so target nothing!
if(!ally && !enemy) return;
ObjectSet<Team> targets = ally ? state.enemyTeams : state.allyTeams;
@ -92,8 +97,8 @@ public class Units {
/**Returns whether these two teams are enemies.*/
public static boolean areEnemies(Team team, Team other){
if(team == other) return false; //fast fail to be more efficient
boolean ally = state.team == team || state.allyTeams.contains(team);
boolean ally2 = state.team == other || state.allyTeams.contains(other);
boolean ally = state.allyTeams.contains(team);
boolean ally2 = state.allyTeams.contains(other);
return ally == ally2;
}
}

View File

@ -3,8 +3,10 @@ package io.anuke.mindustry.entities.units;
import io.anuke.mindustry.entities.Bullet;
import io.anuke.mindustry.entities.BulletType;
import io.anuke.mindustry.entities.Unit;
import io.anuke.ucore.entities.Entity;
import io.anuke.mindustry.game.Team;
import io.anuke.ucore.core.Timers;
import io.anuke.ucore.entities.SolidEntity;
import io.anuke.ucore.util.Mathf;
import io.anuke.ucore.util.Timer;
import java.nio.ByteBuffer;
@ -14,15 +16,28 @@ import static io.anuke.mindustry.Vars.unitGroups;
public class BaseUnit extends Unit {
public UnitType type;
public Timer timer = new Timer(5);
public Entity target;
public float walkTime = 0f;
public Unit target;
public BaseUnit(UnitType type){
public BaseUnit(UnitType type, Team team){
this.type = type;
this.team = team;
}
/**internal constructor used for deserialization, DO NOT USE*/
public BaseUnit(){}
public void rotate(float angle){
rotation = Mathf.slerpDelta(rotation, angle, type.rotatespeed);
}
@Override
public void move(float x, float y){
walkTime += Timers.delta();
baseRotation = Mathf.slerpDelta(baseRotation, Mathf.atan2(x, y), type.baseRotateSpeed);
super.move(x, y);
}
@Override
public float getMass() {
return type.mass;
@ -88,6 +103,7 @@ public class BaseUnit extends Unit {
public void added(){
maxhealth = type.health;
//hitbox.solid = true;
hitbox.setSize(type.hitsize);
hitboxTile.setSize(type.hitsizeTile);
@ -102,6 +118,7 @@ public class BaseUnit extends Unit {
@Override
public void writeSpawn(ByteBuffer buffer) {
buffer.put(type.id);
buffer.put((byte)team.ordinal());
buffer.putFloat(x);
buffer.putFloat(y);
buffer.putShort((short)health);
@ -110,6 +127,7 @@ public class BaseUnit extends Unit {
@Override
public void readSpawn(ByteBuffer buffer) {
type = UnitType.getByID(buffer.get());
team = Team.values()[buffer.get()];
x = buffer.getFloat();
y = buffer.getFloat();
health = buffer.getShort();

View File

@ -5,7 +5,10 @@ import io.anuke.ucore.util.Mathf;
import io.anuke.ucore.util.Translator;
public abstract class GroundUnitType extends UnitType{
protected Translator tr = new Translator();
//only use for drawing!
protected Translator tr1 = new Translator();
//only use for updating!
protected Translator tr2 = new Translator();
public GroundUnitType(String name) {
super(name);
@ -13,13 +16,13 @@ public abstract class GroundUnitType extends UnitType{
@Override
public void draw(BaseUnit unit) {
float walktime = 0; //TODO!
float walktime = unit.walkTime; //TODO!
float ft = Mathf.sin(walktime, 6f, 2f);
for (int i : Mathf.signs) {
tr.trns(unit.baseRotation, ft * i);
Draw.rect(name + "-leg", unit.x + tr.x, unit.y + tr.y, 12f * i, 12f - Mathf.clamp(ft * i, 0, 2), unit.baseRotation - 90);
tr1.trns(unit.baseRotation, ft * i);
Draw.rect(name + "-leg", unit.x + tr1.x, unit.y + tr1.y, 12f * i, 12f - Mathf.clamp(ft * i, 0, 2), unit.baseRotation - 90);
}
Draw.rect(name + "-base", unit.x, unit.y, unit.baseRotation- 90);
@ -29,6 +32,9 @@ public abstract class GroundUnitType extends UnitType{
@Override
public void behavior(BaseUnit unit) {
tr2.set(unit.target.x, unit.target.y).sub(unit.x, unit.y).limit(speed);
unit.move(tr2.x, tr2.y);
unit.rotate(tr2.angle());
}
}

View File

@ -2,6 +2,7 @@ package io.anuke.mindustry.entities.units;
import com.badlogic.gdx.utils.Array;
import io.anuke.mindustry.entities.BulletType;
import io.anuke.mindustry.entities.Units;
import io.anuke.mindustry.graphics.Fx;
import io.anuke.mindustry.net.Net;
import io.anuke.mindustry.net.NetEvents;
@ -16,6 +17,10 @@ public abstract class UnitType {
private static byte lastid = 0;
private static Array<UnitType> types = new Array<>();
private static int timerIndex = 0;
protected static final int timerTarget = timerIndex++;
public final String name;
public final byte id;
@ -23,8 +28,9 @@ public abstract class UnitType {
protected float hitsize = 5f;
protected float hitsizeTile = 4f;
protected float speed = 0.4f;
protected float range = 60;
protected float range = 160;
protected float rotatespeed = 0.1f;
protected float baseRotateSpeed = 0.1f;
protected float mass = 1f;
protected boolean isFlying;
protected float drag = 0.1f;
@ -47,6 +53,8 @@ public abstract class UnitType {
return;
}
updateTargeting(unit);
//TODO logic
unit.x += unit.velocity.x / mass;
@ -54,20 +62,27 @@ public abstract class UnitType {
unit.velocity.scl(Mathf.clamp(1f-drag* Timers.delta()));
behavior(unit);
if(unit.target != null) behavior(unit);
unit.x = Mathf.clamp(unit.x, 0, world.width() * tilesize);
unit.y = Mathf.clamp(unit.y, 0, world.height() * tilesize);
}
/**Only runs when the unit has a target.*/
public abstract void behavior(BaseUnit unit);
public void updateTargeting(BaseUnit unit){
//TODO
if(unit.target == null || unit.target.isDead()){
unit.target = null;
}
if(unit.timer.get(timerTarget, 30)){
unit.target = Units.getClosestEnemy(unit.team, unit.x, unit.y, range, e -> true);
}
}
public void onShoot(BaseUnit unit, BulletType type, float rotation){
//TODO
//TODO remove?
}
public void onDeath(BaseUnit unit){

View File

@ -120,6 +120,7 @@ public class Save16 extends SaveFileVersion {
byte teams = stream.readByte();
for(int i = 0; i < teams; i ++){
Team team = Team.values()[i];
EntityGroup<BaseUnit> group = unitGroups[i];
int amount = stream.readInt();
@ -130,7 +131,7 @@ public class Save16 extends SaveFileVersion {
float y = stream.readFloat();
int health = stream.readShort();
BaseUnit enemy = new BaseUnit(UnitType.getByID(type));
BaseUnit enemy = new BaseUnit(UnitType.getByID(type), team);
enemy.health = health;
enemy.x = x;
enemy.y = y;

View File

@ -5,6 +5,7 @@ import com.badlogic.gdx.files.FileHandle;
import io.anuke.mindustry.entities.Player;
import io.anuke.mindustry.entities.units.BaseUnit;
import io.anuke.mindustry.entities.units.UnitTypes;
import io.anuke.mindustry.game.Team;
import io.anuke.mindustry.net.Net;
import io.anuke.ucore.core.Timers;
import io.anuke.ucore.scene.builders.button;
@ -54,7 +55,7 @@ public class DebugFragment implements Fragment {
row();
new button("wave", () -> state.wavetime = 0f);
row();
new button("spawn", () -> new BaseUnit(UnitTypes.scout).set(player.x, player.y).add());
new button("spawn", () -> new BaseUnit(UnitTypes.scout, Team.red).set(player.x, player.y).add());
row();
}}.end();

View File

@ -127,7 +127,7 @@ public class Turret extends Block{
if(hasAmmo(tile) || (debug && infiniteAmmo)){
if(entity.timer.get(timerTarget, targetInterval)){
entity.target = Units.getClosestEnemies(tile.getTeam(),
entity.target = Units.getClosestEnemy(tile.getTeam(),
tile.worldx(), tile.worldy(), range, e -> !e.isDead());
}