/*
* Foogl (Friendly Object Oriented Game Library)
* Copyright (c) 2008 Wachirawut Thamviset.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package kku.cs.fgl;
import org.newdawn.slick.geom.Rectangle;
import org.newdawn.slick.geom.Shape;
/**
* �����š�ê��ѹ�ͧ Actor a ��� b
*
* @author Wachirawut Thamvsiet
*
*/
public class CollisionData {
protected Actor a; // actor a
protected Actor b; // actor b
protected int x; // ���˹� x � �ش��誹 ����ѷ�� �Ѻ ���˹觢ͧ actor a
protected int y; // ���˹� y � �ش��誹 ����ѷ�� �Ѻ ���˹觢ͧ actor a
protected int type=0; // 0=���Ѻ�ѹ , 1=��Ҫ� , 2=�Ѻ�ѹ , 3=�͡�ҡ��ê�
/**
* ��Ǩ�ͺ��ê������ҧ a ��� b ��Ҫ��ѹ�������
* ��Ҫ��ѹ����������˹觷�誹�ѹ
* �µ�Ǩ�ͺẺ����� ��� ������ͧ b �������� a ���ش�
* ��ҡóշ�� a ����� b ��� check
* a ��è��� monster b ����� ����ع�������ظ
* (�բ�������� a ��� b ��ͧ����� viewport ���ǡѹ)
*/
public boolean checkCollide(){
if(a==null||b==null)return false;
if(b.isDead()||!b.isVisible()) return false;
Shape s2=b.getShape();
float p[]=s2.getPoints();
for(int i=0;i<p.length/2;i++){
if(a.contains(p[i*2],p[i*2+1])){
x=(int)(p[i*2]-a.getX()); // �ԡѴ�������ѹ��Ѻ a.x
y=(int)(p[i*2+1]-a.getY()); // �ԡѴ�������ѹ��Ѻ a.y
return true;
}
}
return false;
}
/**
* ��Ѻ��ا������
*
*/
final public void update(){
if(a==null||b==null)return;
int oldtype=type;
//System.out.println("collision update "+a.getClass().getSimpleName()+" "+b.getClass().getSimpleName());
if(checkCollide()){
if(type==1 || type==2)type=2;
else type=1;
}else{
if(type==1 || type==2)type=3;
else type=0;
}
if(type!=oldtype){
a.collide(b,x,y,type);
int x2 = (int)(a.getX()+x);
int y2 = (int)(a.getX()+x);
b.collide(a,x2,y2,type);
}
}
public static Rectangle getIntersectionRect(float x1, float y1,
float width1, float height1, float x2, float y2, float width2,
float height2) {
float x12 = x1 + width1, y12 = y1 + height1, x22 = x2 + width2, y22 = y2
+ height2;
if (x1 < x2)
x1 = x2;
if (y1 < y2)
y1 = y2;
if (x12 > x22)
x12 = x22;
if (y12 > y22)
y12 = y22;
x12 -= x1;
y12 -= y1;
return new Rectangle(x1, y1, (int) x12, (int) y12);
}
}