Improved efficiency of areEnemies() function

This commit is contained in:
Anuken 2018-06-16 11:35:32 -04:00
parent 6281826b92
commit ff542a9946
3 changed files with 36 additions and 6 deletions

View File

@ -81,6 +81,15 @@ public class Bullet extends BulletEntity<BulletType> implements TeamTrait, SyncT
supressCollision = true;
}
public void resetOwner(Entity entity, Team team){
this.owner = entity;
this.team = team;
}
public void scaleTime(float add){
time += add;
}
@Override
public int getTypeID() {
return typeID;

View File

@ -15,6 +15,8 @@ public class TeamInfo {
enemyData = new ThreadSet<>();
private ThreadSet<TeamData> allTeamData = new ThreadSet<>();
private ThreadSet<Team> allTeams = new ThreadSet<>();
private int allyBits = 0;
private int enemyBits = 0;
/**Returns all teams on a side.*/
public ObjectSet<TeamData> getTeams(boolean ally) {
@ -38,9 +40,11 @@ public class TeamInfo {
if(ally) {
allies.add(team);
allyData.add(data);
allyBits |= (1 << team.ordinal());
}else {
enemies.add(team);
enemyData.add(data);
enemyBits |= (1 << team.ordinal());
}
allTeamData.add(data);
@ -88,9 +92,9 @@ public class TeamInfo {
/**Returns whether or not these two teams are enemies.*/
public boolean areEnemies(Team team, Team other){
if(team == other) return false; //fast fail to be more efficient
boolean ally = allies.contains(team);
boolean ally2 = enemies.contains(other);
return (ally == ally2) || !allTeams.contains(team); //if it's not in the game, target everything.
boolean ally = (allyBits & (1 << team.ordinal())) != 0;
boolean ally2 = (enemyBits & (1 << other.ordinal())) != 0;
return (ally == ally2) || !ally; //if it's not in the game, target everything.
}
public class TeamData {

View File

@ -1,21 +1,28 @@
package io.anuke.mindustry.world.blocks.defense;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
import io.anuke.mindustry.entities.TileEntity;
import io.anuke.mindustry.entities.bullet.Bullet;
import io.anuke.mindustry.entities.bullet.BulletType;
import io.anuke.mindustry.game.Team;
import io.anuke.mindustry.world.Tile;
import io.anuke.mindustry.world.blocks.Wall;
import io.anuke.ucore.core.Graphics;
import io.anuke.ucore.core.Timers;
import io.anuke.ucore.graphics.Draw;
import io.anuke.ucore.util.Mathf;
import io.anuke.ucore.util.Physics;
import static io.anuke.mindustry.Vars.tilesize;
public class DeflectorWall extends Wall {
static final float hitTime = 10f;
protected float maxDamageDeflect = 5f;
protected Rectangle rect = new Rectangle();
public DeflectorWall(String name) {
super(name);
update = false;
@ -45,17 +52,27 @@ public class DeflectorWall extends Wall {
public void handleBulletHit(TileEntity entity, Bullet bullet){
super.handleBulletHit(entity, bullet);
//doesn't reflect powerful bullets
if(bullet.getDamage() > maxDamageDeflect) return;
float penX = Math.abs(entity.x - bullet.x), penY = Math.abs(entity.y - bullet.y);
if(penX < tilesize/2f * size) {
bullet.getVelocity().x *= -1;
Vector2 position = Physics.raycastRect(bullet.lastPosition().x, bullet.lastPosition().y, bullet.x, bullet.y,
rect.setCenter(entity.x, entity.y).setSize(size * tilesize + bullet.hitbox.width + bullet.hitbox.height));
if(position != null){
bullet.set(position.x, position.y);
}
if(penY < tilesize/2f * size){
if(penX > penY) {
bullet.getVelocity().x *= -1;
}else{
bullet.getVelocity().y *= -1;
}
bullet.updateVelocity(BulletType.getByID(bullet.getTypeID()).drag);
bullet.resetOwner(entity, Team.none);
bullet.scaleTime(1f);
bullet.supressCollision();
((DeflectorEntity)entity).hit = 1f;