?? abstractsprite.java~12~
字號:
package tankgame2007;
import java.awt.Graphics;
public abstract class AbstractSprite { //這是Sprite父類
int X, Y, width, height;
boolean visible, active;
int collisionX,collisionY;
abstract public void paintSprite(Graphics g, int SpiritDirection);
abstract public void paintSprite(Graphics g);
abstract public void updateState(int SpiritDirection);
public int getX(){ //獲得精靈X坐標值
return X;
}
public int getY(){ //獲得精靈Y坐標值
return Y;
}
public void setLocation(int X, int Y) { //設置精靈X,Y坐標值
this.X = X;
this.Y = Y;
}
public int getWidth(){ //獲得精靈的寬
return width;
}
public int getHeight() { //獲得精靈的高
return height;
}
public void setSize(int width, int height) { //設置精靈寬高值
this.width = width;
this.height = height;
}
public boolean canVisible() { //獲得精靈是否可見的屬性
return visible;
}
public void setVisible(boolean v) {//設置精靈是否可見
visible = v;
}
public boolean canMove() {//獲得精靈是否可移動的屬性
return active;
}
public void setMove(boolean m) {//設置精靈是否可移動的屬性
active = m;
}
//增加的方法:檢測碰撞算法1====================================================
/*
public boolean isCollided(AbstractSprite target) {
boolean collided = false;
if(this.visible == true && target.visible == true){
//頭部碰撞
if(this.getX() <= (target.getX()+ target.getWidth()) &&
this.getX() >= target.getX()) {
if((this.getY()+this.getHeight())>=target.getY()&&
((this.getY()+this.getHeight())<=
(target.getY()+target.getHeight()))){ //左下角碰撞
collided = true;
}
if((this.getY()<=(target.getY() + target.getHeight()))&&
(this.getY()>=target.getY())){ //左上角碰撞
collided = true;
}
}
//尾部碰撞
if(target.getX() <= (this.getX()+ this.getWidth()) &&
target.getX() >= this.getX()) {
if((this.getY()+this.getHeight())>=target.getY()&&
((this.getY()+this.getHeight())<=
(target.getY()+target.getHeight()))){ //左下角碰撞
collided = true;
}
if((this.getY()<=(target.getY() + target.getHeight()))&&
(this.getY()>=target.getY())){ //左上角碰撞
collided = true;
}
}
}
return collided;
}
*/
//檢測碰撞算法2-------------------------------------------------------------
public boolean isCollided(AbstractSprite target) {
boolean collided = false;
if(this.visible == true && target.visible == true){
int cX=(this.getX()+this.getWidth()/2)-
(target.getX()+target.getWidth()/2);
int cW=(this.getWidth()+target.getWidth())/2;
int cY=(this.getY()+this.getHeight()/2)-
(target.getY()+target.getHeight()/2);
int cH=(this.getHeight()+target.getHeight())/2;
if((java.lang.Math.abs(cX) < java.lang.Math.abs(cW)) &&
(java.lang.Math.abs(cY) < java.lang.Math.abs(cH))){
collided = true;
}
}
return collided;
}
// 檢測與地圖的碰撞
public boolean isCollide(int TileWidth,
int TileHeight,
int MapCols,
int moveDirection) {
if(this.X<704-this.getWidth() && this.Y<576-this.getWidth()){
int mapIndexOfLeftTop , mapIndexOfRightTop, mapIndexOfLeftBottom,mapIndexOfRightBottom;
System.out.println("grass length"+MapArray.grass.length);
System.out.println("wall length"+MapArray.wall.length);
int mapRow = (this.Y) / TileHeight;
int mapCol = (this.X) / TileWidth;
System.out.println("mapRow"+mapRow);
System.out.println("mapCols"+mapCol);
if((this.X)%TileWidth!=0)
{
mapIndexOfLeftTop = mapRow * MapCols + mapCol+1;
}
else{
mapIndexOfLeftTop= mapRow * MapCols + mapCol;
}
mapRow = (this.Y) / TileHeight;
mapCol = (this.X + this.width) / TileWidth;
if((this.X)%TileWidth!=0)
{
mapIndexOfRightTop = mapRow * MapCols + mapCol+1;
if(mapIndexOfRightTop>=MapArray.grass.length-1){
mapIndexOfRightTop=MapArray.grass.length-1;
}
}
else{
mapIndexOfRightTop= mapRow * MapCols + mapCol;
}
mapRow = (this.Y + this.height) / TileHeight;
mapCol = (this.X) / TileWidth;
if((this.X)%TileWidth!=0)
{
mapIndexOfLeftBottom = mapRow * MapCols + mapCol+1;
if(mapIndexOfLeftBottom>=MapArray.grass.length-1){
mapIndexOfLeftBottom=MapArray.grass.length-1;
}
}
else{
mapIndexOfLeftBottom= mapRow * MapCols + mapCol;
}
mapRow = (this.Y + this.height) / TileHeight;
mapCol = (this.X + this.width) / TileWidth;
if((this.X)%TileWidth!=0)
{
mapIndexOfRightBottom = mapRow * MapCols + mapCol+1;
if(mapIndexOfRightBottom>=MapArray.grass.length-1){
mapIndexOfRightBottom=MapArray.grass.length-1;
}
}
else{
mapIndexOfRightBottom= mapRow * MapCols + mapCol;
}
//======================================================================
boolean collided = false;
switch (moveDirection) {
case 0:
if ( (MapArray.wall[mapIndexOfLeftTop] > 0) ||
( (MapArray.wall[mapIndexOfLeftBottom] > 0))) {
if (MapArray.wall[mapIndexOfLeftTop] > 0) {
collisionX = (mapIndexOfLeftTop % MapCols) * TileWidth + TileWidth +
1;
}
else {
collisionX = (mapIndexOfLeftBottom % MapCols) * TileWidth +
TileWidth + 1;
}
collided = true;
}
break;
case 1:
if ( (MapArray.wall[mapIndexOfRightTop] > 0) ||
( (MapArray.wall[mapIndexOfRightBottom] > 0))) {
if (MapArray.wall[mapIndexOfRightTop] > 0) {
collisionX = (mapIndexOfRightTop % MapCols) * TileWidth -
this.getWidth() - 1;
}
else {
collisionX = (mapIndexOfRightBottom % MapCols) * TileWidth -
this.getWidth() - 1;
}
collided = true;
}
break;
case 2:
if ( (MapArray.wall[mapIndexOfLeftTop] > 0) ||
( (MapArray.wall[mapIndexOfRightTop] > 0))) {
if (MapArray.wall[mapIndexOfLeftTop] > 0) {
collisionY = (mapIndexOfLeftTop / MapCols + 1) * TileHeight + 1;
}
else {
collisionY = (mapIndexOfRightTop / MapCols + 1) * TileHeight + 1;
}
collided = true;
}
break;
case 3:
if ((MapArray.wall[mapIndexOfLeftBottom] > 0) ||
((MapArray.wall[mapIndexOfRightBottom] > 0))) {
if (MapArray.wall[mapIndexOfLeftBottom] > 0) {
collisionY = (mapIndexOfLeftBottom / MapCols) * TileHeight -
this.getHeight() - 1;
}
else {
collisionY = (mapIndexOfRightBottom / MapCols) * TileHeight -
this.getHeight() - 1;
}
collided = true;
}
}
return collided;
}
return false;
}
//====================================================================
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -