首页>代码>JAVA swing高仿愤怒的小鸟AngryBird源代码下载>/AngryBirds/src/AngryBirdsApplication/AngryBirdsArea.java
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package AngryBirdsApplication;

import AngryBirdsCharacters.AngryBirdsBodyInfo;
import java.awt.Color;
import java.awt.Graphics2D;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import org.jbox2d.callbacks.QueryCallback;
import org.jbox2d.collision.AABB;
import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.*;
import org.jbox2d.collision.shapes.*;
import org.jbox2d.dynamics.joints.*;

/**
 *
 * @author Sumomoxiao
 */
enum QueueItemType {

    MouseDown, MouseMove, MouseUp, KeyPressed, KeyReleased
}

class QueueItem {

    public QueueItemType type;
    public Vec2 p;
    public char c;
    public int code;

    public QueueItem(QueueItemType t, Vec2 pt) {
        type = t;
        p = pt;
    }
    
}

class FixtureQueryCallback implements QueryCallback {

    public final Vec2 point;
    public Fixture fixture;

    public FixtureQueryCallback() {
        point = new Vec2();
        fixture = null;
    }

    /**
     * @see
     * org.jbox2d.callbacks.QueryCallback#reportFixture(org.jbox2d.dynamics.Fixture)
     */
    public boolean reportFixture(Fixture argFixture) {
        Body body = argFixture.getBody();
        if (body.getType() == BodyType.DYNAMIC) {
            boolean inside = argFixture.testPoint(point);
            if (inside) {
                fixture = argFixture;

                return false;
            }
        }

        return true;
    }
}

public abstract class AngryBirdsArea {

    private MouseJoint mouseJoint;
    private Vec2 mouseWorld = new Vec2();
    public final World sworld;
    private final Vec2 gravity;
    public Vec2 slingAnchor;
    public ArrayList<Body> birdlist;
    public ArrayList<Body> oblist;
    public ArrayList<Body> piglist, sling;
    public WeldJoint attach;
    public WeldJointDef attachDef;
    public Body ground;
    public float scale = 1 / 64f;
    float timeStep = 1.0f / 60.0f;
    int velocityIterations = 6;
    int positionIterations = 2;
    public int birdbullets;
    private final LinkedList<QueueItem> inputQueue;

    public AngryBirdsArea() {
        gravity = new Vec2(0, -10f);
        inputQueue = new LinkedList<QueueItem>();
        sworld = new World(gravity, true);
        birdlist = new ArrayList<Body>();
        oblist = new ArrayList<Body>();
        piglist = new ArrayList<Body>();
        sling = new ArrayList<Body>();
        slingAnchor = new Vec2();

    }

    abstract public void initStage();
    long endtime = 0;
    long duration = 0;  //duration of release the bird;
    long descountdown = 0;
    public void step() {
        
        sworld.step(timeStep, velocityIterations, positionIterations);
        
        if (mouseJoint == null && attach == null) {
            
            endtime = System.currentTimeMillis();
            duration = (endtime - releasetime) / 1000;
            
        }


        if (duration > 3 && attach == null) {
            
            if(birdbullets<=birdlist.size()) {
            birdlist.get(birdbullets).setTransform(slingAnchor, 0);
   
            attachDef.bodyB = birdlist.get(birdbullets);
            
            attach = (WeldJoint) this.getWorld().createJoint(attachDef);
            duration = 0;
            
            }

        }
        

        
        /*
        for(int i=0;i<birdlist.size();i++) {
            if(!birdlist.get(i).isAwake())
                getWorld().destroyBody(birdlist.get(i));
                birdlist.remove(i);
            
        }
        * 
        */

    }

    public ArrayList<Body> getBirds() {
        return birdlist;
    }

    public ArrayList<Body> getObstacles() {
        return oblist;
    }

    public ArrayList<Body> getPigs() {
        return piglist;
    }

    public World getWorld() {
        return sworld;
    }

    public Body getGround() {
        return ground;
    }

    public void setGravity(Vec2 gra) {
        sworld.setGravity(gra);
    }

    public boolean isMouseJointNull() {
        if(mouseJoint == null) 
            return true;
        return false;
    }
    public void update() {
        if (!inputQueue.isEmpty()) {
            synchronized (inputQueue) {
                while (!inputQueue.isEmpty()) {
                    QueueItem i = inputQueue.pop();
                    switch (i.type) {
                        case MouseDown:
                            mouseDown(i.p);
                            break;
                        case MouseMove:
                            mouseMove(i.p);
                            break;
                        case MouseUp:
                            mouseUp(i.p);
                            break;
                    }
                }
            }
        }
        step();
    }
    /**
     * Called for mouse-up
     *
     * @param p
     */
    long releasetime = 0;

    public void mouseUp(Vec2 p) {
        
        float length = 0;
        Vec2 pos = new Vec2();
        pos = p.sub(slingAnchor);
        length = pos.length();

        if (length > 3) {


            pos.x /= length / 3;
            pos.y /= length / 3;

            p = pos;

        }

        if (mouseJoint != null) {
            mouseJoint.m_bodyB.setFixedRotation(false);
            mouseJoint.m_bodyB.setLinearVelocity(pos.negate().mul(7.5f));  //release and shoot!
            sworld.destroyJoint(mouseJoint);
            mouseJoint = null;
            if(birdbullets<birdlist.size()-1)
            birdbullets++;
            releasetime = System.currentTimeMillis();

        }

        /*
         * if (bombSpawning) { completeBombSpawn(p); }
         *
         */
    }
    private final AABB queryAABB = new AABB();
    private final FixtureQueryCallback callback = new FixtureQueryCallback();

    /**
     * Called for mouse-down
     *
     * @param p
     */
    public void mouseDown(Vec2 p) {
        mouseWorld.set(p);

        if (mouseJoint != null) {
            return;
        }

        queryAABB.lowerBound.set(p.x - .001f, p.y - .001f);
        queryAABB.upperBound.set(p.x + .001f, p.y + .001f);
        callback.point.set(p);
        callback.fixture = null;
        sworld.queryAABB(callback, queryAABB);

        if (callback.fixture != null && callback.fixture.m_filter.groupIndex == -1) {

            if (attach != null) {
                sworld.destroyJoint(attach);
                attach = null;
            }

            Body body = callback.fixture.getBody();
            MouseJointDef def = new MouseJointDef();
            def.bodyA = ground;
            def.bodyB = body;
            def.target.set(p);
            def.maxForce = 1000f * body.getMass();
            body.setFixedRotation(true);//NOTE!!!!!!!!!!!!!!!!!
            mouseJoint = (MouseJoint) sworld.createJoint(def);
            body.setAwake(true);
        }
    }
    /**
     * Process MouseEvent.
     *
     * @param p,length
     */
    Vec2 length;

    public void mouseMove(Vec2 p) {
        mouseWorld.set(p);
        Vec2 force = p.sub(slingAnchor);
        float ol = force.length();

        if (force.length() > 3f) {
            force.x /= ol / 3;
            force.y /= ol / 3;
            force.addLocal(slingAnchor);
            p = force;
        }
        if (mouseJoint != null) {
            mouseJoint.setTarget(p);
        }
    }

    public void queueMouseUp(Vec2 p) {
        synchronized (inputQueue) {
            inputQueue.addLast(new QueueItem(QueueItemType.MouseUp, p));
        }
    }

    public void queueMouseDown(Vec2 p) {
        synchronized (inputQueue) {
            inputQueue.addLast(new QueueItem(QueueItemType.MouseDown, p));
        }
    }

    public void queueMouseMove(Vec2 p) {
        synchronized (inputQueue) {
            inputQueue.addLast(new QueueItem(QueueItemType.MouseMove, p));
        }
    }
}
最近下载更多
wangjialiang1  LV17 2023年8月24日
yerwiu  LV10 2023年7月30日
月影浮动  LV6 2020年5月22日
wei112233  LV15 2020年5月20日
远程调试_成品系统  LV15 2020年3月19日
wangzff  LV3 2020年2月26日
GG思密达da  LV9 2020年1月16日
zipper  LV8 2020年1月8日
yukaifeng  LV4 2019年12月30日
安妮的小熊  LV6 2019年12月23日
最近浏览更多
sunrui5555  LV1 4月19日
unprecedented 2023年11月16日
暂无贡献等级
微信网友_6738500712058880 2023年11月16日
暂无贡献等级
wangjialiang1  LV17 2023年8月24日
yerwiu  LV10 2023年7月30日
fmj0408  LV1 2023年6月19日
libobozi 2023年6月19日
暂无贡献等级
17683946472  LV9 2023年6月8日
泓鼎168  LV19 2023年5月10日
想把它染成绿色 2023年2月28日
暂无贡献等级
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友