v0.3.0: reworked the ballistica system
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
/*
|
||||
* Pixel Dungeon
|
||||
* Copyright (C) 2012-2014 Oleg Dolya
|
||||
* Copyright (C) 2012-2015 Oleg Dolya
|
||||
*
|
||||
* Shattered Pixel Dungeon
|
||||
* Copyright (C) 2014-2015 Evan Debenham
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
@@ -20,43 +23,73 @@ package com.shatteredpixel.shatteredpixeldungeon.mechanics;
|
||||
import com.shatteredpixel.shatteredpixeldungeon.actors.Actor;
|
||||
import com.shatteredpixel.shatteredpixeldungeon.levels.Level;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class Ballistica {
|
||||
|
||||
public static int[] trace = new int[Math.max( Level.WIDTH, Level.HEIGHT )];
|
||||
public static int distance;
|
||||
//note that the path is the FULL path of the projectile, including tiles after collision.
|
||||
//make sure to generate a subPath for the common case of going source to collision.
|
||||
public ArrayList<Integer> path = new ArrayList<>();
|
||||
public Integer sourcePos = null;
|
||||
public Integer collisionPos = null;
|
||||
public Integer dist = 0;
|
||||
|
||||
public static int cast( int from, int to, boolean magic, boolean hitChars ) {
|
||||
|
||||
//parameters to specify the colliding cell
|
||||
public static final int STOP_TARGET = 1; //ballistica will stop at the target cell
|
||||
public static final int STOP_CHARS = 2; //ballistica will stop on first char hit
|
||||
public static final int STOP_TERRAIN = 4; //ballistica will stop on terrain(LOS blocking, impassable, etc.)
|
||||
|
||||
public static final int PROJECTILE = STOP_TARGET | STOP_CHARS | STOP_TERRAIN;
|
||||
|
||||
//TODO: consider if we want thrown items to use this, or just have them all be projectileWeapons
|
||||
public static final int THROWN_ITEM = STOP_TARGET | STOP_TERRAIN;
|
||||
|
||||
public static final int MAGIC_BOLT = STOP_CHARS | STOP_TERRAIN;
|
||||
|
||||
public static final int WONT_STOP = 0;
|
||||
|
||||
|
||||
public Ballistica( int from, int to, int params ){
|
||||
sourcePos = from;
|
||||
build(from, to, (params & STOP_TARGET) > 0, (params & STOP_CHARS) > 0, (params & STOP_TERRAIN) > 0);
|
||||
if (collisionPos != null)
|
||||
dist = path.indexOf( collisionPos );
|
||||
else
|
||||
collisionPos = path.get( dist=path.size()-1 );
|
||||
}
|
||||
|
||||
private void build( int from, int to, boolean stopTarget, boolean stopChars, boolean stopTerrain ) {
|
||||
int w = Level.WIDTH;
|
||||
|
||||
|
||||
int x0 = from % w;
|
||||
int x1 = to % w;
|
||||
int y0 = from / w;
|
||||
int y1 = to / w;
|
||||
|
||||
|
||||
int dx = x1 - x0;
|
||||
int dy = y1 - y0;
|
||||
|
||||
|
||||
int stepX = dx > 0 ? +1 : -1;
|
||||
int stepY = dy > 0 ? +1 : -1;
|
||||
|
||||
|
||||
dx = Math.abs( dx );
|
||||
dy = Math.abs( dy );
|
||||
|
||||
|
||||
int stepA;
|
||||
int stepB;
|
||||
int dA;
|
||||
int dB;
|
||||
|
||||
|
||||
if (dx > dy) {
|
||||
|
||||
|
||||
stepA = stepX;
|
||||
stepB = stepY * w;
|
||||
dA = dx;
|
||||
dB = dy;
|
||||
|
||||
} else {
|
||||
|
||||
|
||||
stepA = stepY * w;
|
||||
stepB = stepX;
|
||||
dA = dy;
|
||||
@@ -64,35 +97,48 @@ public class Ballistica {
|
||||
|
||||
}
|
||||
|
||||
distance = 1;
|
||||
trace[0] = from;
|
||||
|
||||
int cell = from;
|
||||
|
||||
|
||||
int err = dA / 2;
|
||||
while (cell != to || magic) {
|
||||
|
||||
while (Level.insideMap(cell)) {
|
||||
|
||||
//if we're in a wall, collide with the previous cell along the path.
|
||||
if (stopTerrain && !Level.passable[cell] && !Level.avoid[cell]) {
|
||||
collide(path.get(path.size()-1));
|
||||
}
|
||||
|
||||
path.add(cell);
|
||||
|
||||
if ((stopTerrain && Level.losBlocking[cell])
|
||||
|| (cell != sourcePos && stopChars && Actor.findChar( cell ) != null)
|
||||
|| (cell == to && stopTarget)){
|
||||
collide(cell);
|
||||
}
|
||||
|
||||
cell += stepA;
|
||||
|
||||
|
||||
err += dB;
|
||||
if (err >= dA) {
|
||||
err = err - dA;
|
||||
cell = cell + stepB;
|
||||
}
|
||||
|
||||
trace[distance++] = cell;
|
||||
|
||||
if (!Level.passable[cell] && !Level.avoid[cell]) {
|
||||
return trace[--distance - 1];
|
||||
}
|
||||
|
||||
if (Level.losBlocking[cell] || (hitChars && Actor.findChar( cell ) != null)) {
|
||||
return cell;
|
||||
}
|
||||
}
|
||||
|
||||
trace[distance++] = cell;
|
||||
|
||||
return to;
|
||||
}
|
||||
|
||||
//we only want to record the first position collision occurs at.
|
||||
private void collide(int cell){
|
||||
if (collisionPos == null)
|
||||
collisionPos = cell;
|
||||
}
|
||||
|
||||
//returns a segment of the path from start to end, inclusive.
|
||||
//if there is an error, returns an empty arraylist instead.
|
||||
public List<Integer> subPath(int start, int end){
|
||||
try {
|
||||
end = Math.max( end, path.size()-1);
|
||||
return path.subList(start, end+1);
|
||||
} catch (Exception e){
|
||||
return new ArrayList<>();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user