基于微云游戏引擎的游戏开发——《猴子跳》

本人android开发新手,借博客大赛希望和各位开发者共同交流下android游戏的开发心得。本人前几个练手的游戏都没有使用游戏引擎,开发出来效果一般般,之后便尝试了使用游戏引擎,第一款游戏引擎就是用的微云,之所以用微云,因为微云提供了一个不错的demo可以作为很好的功能演示,非常的直观,但是其实例确实不足,应该说在我学习微云引擎的时候,只有demo源代码可以供你参考的。但是这一切不能否认微云是一个很好的游戏引擎的事实,尤其是它整合的box2d物理引擎用起来也非常的顺手。


那么下面我就简单来介绍下我做的这个小游戏《猴子跳》,整个游戏风格简单,有点类似《doodle jump》

效果图如下:

static {
		System.loadLibrary("xml2");
		System.loadLibrary("wiengine");
		System.loadLibrary("lua");
		System.loadLibrary("chipmunk");
	}


基于微云游戏引擎的游戏开发——《猴子跳》_第1张图片

基于微云游戏引擎的游戏开发——《猴子跳》_第2张图片

基于微云游戏引擎的游戏开发——《猴子跳》_第3张图片

基于微云游戏引擎的游戏开发——《猴子跳》_第4张图片

gameview.java

import com.wiyun.engine.nodes.Director;
import com.wiyun.engine.nodes.Scene;
import com.wiyun.engine.nodes.Director.IDirectorLifecycleListener;
import com.wiyun.engine.opengl.WYGLSurfaceView;
import com.wiyun.engine.transitions.JumpZoomTransition;

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager.LayoutParams;

public class GameView extends Activity implements IDirectorLifecycleListener {
	/** Called when the activity is first created. */
	protected WYGLSurfaceView mGLSurfaceView;
	static GameView gameview;
	static int fenshu, score;
	protected Scene mScene;
	static {
		System.loadLibrary("xml2");
		System.loadLibrary("wiengine");
		System.loadLibrary("lua");
		System.loadLibrary("chipmunk");
	}
	MediaPlayer mMediaPlayer, mMediaPlayer2;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getWindow().addFlags(LayoutParams.FLAG_FULLSCREEN);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		mGLSurfaceView = new WYGLSurfaceView(this);
		gameview = this;
		setContentView(mGLSurfaceView);
		// 设置显示帧率,程序发布时应该去掉
		Director.getInstance().setDisplayFPS(true);

		// 创建第一个场景
		menu a = new menu();
		
		mMediaPlayer = new MediaPlayer();
		// 添加一个生命周期监听器,如果不需要可以不添加
		Director.getInstance().addLifecycleListener(this);

		// 开始运行第一个场景
		Director.getInstance().runWithScene(a.mScene);

	}

	public void play1() {
		
		mMediaPlayer = MediaPlayer.create(GameView.this, R.raw.jump);
		mMediaPlayer.setLooping(false);
		mMediaPlayer.start();
	}
	public void play2() {
		mMediaPlayer = MediaPlayer.create(GameView.this, R.raw.boost);
		mMediaPlayer.setLooping(false);
		mMediaPlayer.start();
	}
	public void play3() {
		mMediaPlayer = MediaPlayer.create(GameView.this, R.raw.jump2);
		mMediaPlayer.setLooping(false);
		mMediaPlayer.start();
	}

	public void change() {
		main a = new main();
		Director.getInstance().replaceScene(
				JumpZoomTransition.make(1, a.mScene));
		number n = new number();
		a.mScene.addChild(n);
	}

	@Override
	protected void onPause() {
		super.onPause();

		Director.getInstance().pause();
	}

	@Override
	protected void onResume() {
		super.onResume();

		Director.getInstance().resume();
	}

	@Override
	protected void onDestroy() {
        super.onDestroy();
		Director.getInstance().end();

	}

	@Override
	public void onDirectorEnded() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onDirectorPaused() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onDirectorResumed() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onDirectorScreenCaptured(String arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onSurfaceChanged(int arg0, int arg1) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onSurfaceCreated() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onSurfaceDestroyed() {
		// TODO Auto-generated method stub

	}
}


菜单栏 menu.java

import com.wiyun.engine.motionwelder.MWSprite;
import com.wiyun.engine.nodes.Director;
import com.wiyun.engine.nodes.Layer;
import com.wiyun.engine.nodes.Menu;
import com.wiyun.engine.nodes.MenuItemSprite;
import com.wiyun.engine.nodes.Scene;
import com.wiyun.engine.nodes.Sprite;
import com.wiyun.engine.opengl.Texture2D;
import com.wiyun.engine.particle.ParticleSystem;
import com.wiyun.engine.types.WYRect;
import com.wiyun.engine.types.WYSize;
import com.wiyun.engine.utils.ResolutionIndependent;

public class menu extends Layer {
	public Scene mScene;
	WYSize s;
	MWSprite m_sprite1;
	ParticleSystem emitter;

	menu() {
		s = Director.getInstance().getWindowSize();
		mScene = Scene.make();

		bc();
		s();
		m();

	}

	public void bc() {
		Texture2D bc = Texture2D.makePNG(R.drawable.bc);

		Sprite bc2 = Sprite.make(bc);

		bc.autoRelease();
		bc2.autoRelease();
		bc2.setPosition(bc2.getWidth() / 2, bc2.getHeight() / 2);
		mScene.addChild(bc2);
	}

	public void s() {
		emitter = ParticleSnow.make();
		emitter.setPosition(s.width / 2, s.height);
		emitter.autoRelease();
		mScene.addChild(emitter);
	}

	public void m() {

		Texture2D tex = Texture2D.makePNG(R.drawable.start_normal);
		Texture2D tex2 = Texture2D.makePNG(R.drawable.start_down);
		Sprite sprite1Normal = Sprite.make(tex, ResolutionIndependent
				.resolve(WYRect.make(0, 0, 96, 29)));
		Sprite sprite1Disabled = Sprite.make(tex2, ResolutionIndependent
				.resolve(WYRect.make(0, 0, 96, 29)));
		MenuItemSprite start = MenuItemSprite.make(sprite1Normal,
				sprite1Disabled, sprite1Disabled, this, "start");

		tex = Texture2D.makePNG(R.drawable.about_normal);
		tex2 = Texture2D.makePNG(R.drawable.about_down);
		sprite1Normal = Sprite.make(tex, ResolutionIndependent.resolve(WYRect
				.make(0, 0, 96, 29)));
		sprite1Disabled = Sprite.make(tex2, ResolutionIndependent
				.resolve(WYRect.make(0, 0, 96, 29)));
		MenuItemSprite about = MenuItemSprite.make(sprite1Normal,
				sprite1Disabled, sprite1Disabled);

		tex = Texture2D.makePNG(R.drawable.quit_normal);
		tex2 = Texture2D.makePNG(R.drawable.quit_down);
		sprite1Normal = Sprite.make(tex, ResolutionIndependent.resolve(WYRect
				.make(0, 0, 96, 29)));
		sprite1Disabled = Sprite.make(tex2, ResolutionIndependent
				.resolve(WYRect.make(0, 0, 96, 29)));
		MenuItemSprite quit = MenuItemSprite.make(sprite1Normal,
				sprite1Disabled, sprite1Disabled, this, "quit");

		Menu menu = Menu.make(start, about, quit);
		menu.alignItemsVertically(40);
		menu.setPosition(s.width / 2, s.height / 2);
		mScene.addChild(menu);
	}

	public void quit() {
		GameView.gameview.onDestroy();
	}

	public void start() {
		GameView.gameview.change();
	}

}

游戏大背景的设置,游戏的大背景由36张图片拼接而成的

BC.java

import com.wiyun.engine.box2d.Box2D;
import com.wiyun.engine.box2d.Box2DRender;
import com.wiyun.engine.box2d.collision.PolygonShape;
import com.wiyun.engine.box2d.dynamics.Body;
import com.wiyun.engine.box2d.dynamics.BodyDef;
import com.wiyun.engine.box2d.dynamics.Fixture;
import com.wiyun.engine.box2d.dynamics.World;
import com.wiyun.engine.box2d.dynamics.World.IContactListener;
import com.wiyun.engine.box2d.dynamics.contacts.Contact;
import com.wiyun.engine.nodes.Director;
import com.wiyun.engine.nodes.Layer;
import com.wiyun.engine.opengl.Texture2D;
import com.wiyun.engine.types.WYPoint;
import com.wiyun.engine.types.WYSize;
import com.wiyun.engine.utils.TargetSelector;

public class BC extends Layer implements IContactListener {
	static World mWorld;

	Fixture bc1, bc2;
	WYSize s;
	Box2DRender render;
	float y1 = +17.8f, y2 = 0;
	static int chushi = 30;
	static boolean grass1 = false, grass2 = false, grass3 = false;
	protected Box2D mBox2D;

	BC() {
		mBox2D = Box2D.make();

		mBox2D.setDebugDraw(false);
		render = Box2DRender.make();
		mBox2D.setBox2DRender(render);

		mWorld = mBox2D.getWorld();

		s = Director.getInstance().getWindowSize();
		addChild(mBox2D);
		y1 = s.height / s.width * 10f;
		// set gravity
		mWorld.setGravity(0, 4);// 47

		{
			BodyDef bd = BodyDef.make();
			bd.setType(Body.TYPE_DYNAMIC);
			bd.setPosition(0, y1);
			Body body = mWorld.createBody(bd);
			bd.destroy();
			PolygonShape shape = PolygonShape.make();
			shape.setAsBox(10f, s.height / s.width * 10f);
			int id = id(chushi);
			bc1 = body.createFixture(shape, 0.0f);
			Texture2D tex = Texture2D.makePNG(id);
			render.bindTexture(bc1, tex);
			tex.autoRelease();
		}
		{
			BodyDef bd = BodyDef.make();
			bd.setType(Body.TYPE_DYNAMIC);
			bd.setPosition(0, y1 + s.height / s.width * 20 - 0.26f);
			Body body = mWorld.createBody(bd);
			bd.destroy();
			PolygonShape shape = PolygonShape.make();
			shape.setAsBox(10f, s.height / s.width * 10f);

			bc2 = body.createFixture(shape, 0.0f);
			chushi--;
			int id = id(chushi);
			Texture2D tex = Texture2D.makePNG(id);
			render.bindTexture(bc2, tex);
			tex.autoRelease();
		}

		// place box2d to center of bottom edge
		mBox2D.setPosition(s.width / 2, 0);
		mWorld.setContactListener(this);

		// add contact listener
		schedule(new TargetSelector(this, "update(float)", new Object[] { 0f }));
	}

	public void update(float delta) {
		mWorld.step(1.f / 60.f, 10, 10);
		mWorld.clearForces();
		if (Box2.collision == true) {
			bc1.getBody().setLinearVelocity(
					WYPoint.make(0f, -2.56f * Box2.jump1));
			bc2.getBody().setLinearVelocity(
					WYPoint.make(0f, -2.56f * Box2.jump1));
			Box2.collision = false;
		}
		if (bc1.getBody().getPosition().y < -y1 + 0.26f) {
			int id = id(chushi);
			Texture2D tex = Texture2D.makePNG(id);
			render.bindTexture(bc1, tex);
			tex.autoRelease();

			chushi--;
			id = id(chushi);
			tex = Texture2D.makePNG(id);
			render.bindTexture(bc2, tex);
			tex.autoRelease();
			bc1.getBody().setTransform(0, y1, 0);
			bc2.getBody().setTransform(0,
					y1 + s.height / s.width * 20f - 0.26f, 0);// 35
			grass1 = true;
			grass2 = true;
			grass3 = true;
		}
	}

	public int id(int a) {
		int id = 0;
		switch (a) {
		case 1:
			id = R.drawable.background_1_1;
			break;
		case 2:
			id = R.drawable.background_2_2;
			break;
		case 3:
			id = R.drawable.background_3_3;
			break;
		case 4:
			id = R.drawable.background_4_4;
			break;
		case 5:
			id = R.drawable.background_5_5;
			break;
		case 6:
			id = R.drawable.background_6_6;
			break;
		case 7:
			id = R.drawable.background_7_7;
			break;
		case 8:
			id = R.drawable.background_8_8;
			break;
		case 9:
			id = R.drawable.background_9_9;
			break;
		case 10:
			id = R.drawable.background_10_10;
			break;
		case 11:
			id = R.drawable.background_11_11;
			break;
		case 12:
			id = R.drawable.background_12_12;
			break;
		case 13:
			id = R.drawable.background_13_13;
			break;
		case 14:
			id = R.drawable.background_14_14;
			break;
		case 15:
			id = R.drawable.background_15_15;
			break;
		case 16:
			id = R.drawable.background_16_16;
			break;
		case 17:
			id = R.drawable.background_17_17;
			break;
		case 18:
			id = R.drawable.background_18_18;
			break;
		case 19:
			id = R.drawable.background_19_19;
			break;
		case 20:
			id = R.drawable.background_20_20;
			break;
		case 21:
			id = R.drawable.background_21_21;
			break;
		case 22:
			id = R.drawable.background_22_22;
			break;
		case 23:
			id = R.drawable.background_23_23;
			break;
		case 24:
			id = R.drawable.background_24_24;
			break;
		case 25:
			id = R.drawable.background_25_25;
			break;
		case 26:
			id = R.drawable.background_26_26;
			break;
		case 27:
			id = R.drawable.background_27_27;
			break;
		case 28:
			id = R.drawable.background_28_28;
			break;
		case 29:
			id = R.drawable.background_29_29;
			break;
		case 30:
			id = R.drawable.background_30_30;
			break;
		}

		return id;

	}

	@Override
	public void beginContact(int contactPointer) {
		// TODO Auto-generated method stub
		Contact contact = Contact.from(contactPointer);
		contact.setEnabled(false);
	}

	@Override
	public void endContact(int contactPointer) {
		// TODO Auto-generated method stub
		Contact contact = Contact.from(contactPointer);
		contact.setEnabled(false);
	}

	@Override
	public void postSolve(int contactPointer, int arg1) {
		// TODO Auto-generated method stub
		Contact contact = Contact.from(contactPointer);
		contact.setEnabled(false);
	}

	@Override
	public void preSolve(int contactPointer, int arg1) {
		// TODO Auto-generated method stub
		Contact contact = Contact.from(contactPointer);
		contact.setEnabled(false);
	}
}

第二层的设计,grass.java

import com.wiyun.engine.box2d.Box2D;
import com.wiyun.engine.box2d.Box2DRender;
import com.wiyun.engine.box2d.FixtureAnimation;
import com.wiyun.engine.box2d.collision.PolygonShape;
import com.wiyun.engine.box2d.dynamics.Body;
import com.wiyun.engine.box2d.dynamics.BodyDef;
import com.wiyun.engine.box2d.dynamics.Fixture;
import com.wiyun.engine.box2d.dynamics.World;
import com.wiyun.engine.box2d.dynamics.World.IContactListener;
import com.wiyun.engine.box2d.dynamics.contacts.Contact;
import com.wiyun.engine.nodes.Director;
import com.wiyun.engine.nodes.Layer;
import com.wiyun.engine.opengl.Texture2D;
import com.wiyun.engine.types.WYPoint;
import com.wiyun.engine.types.WYSize;
import com.wiyun.engine.utils.TargetSelector;

public class grass extends Layer implements IContactListener {
	static World mWorld;

	Fixture grass1, grass2, grass3; // 漂浮物1,2,3(因为一开始只设计了了草,现在更改了)
	float grassx1, grassx2, grassx3;// 漂浮物1,2,3的横向移动速度,因为有云和草什么的,要具有不同的速度撒(因为一开始只设计了了草,现在更改了)
	float grassy1, grassy2, grassy3;// 漂浮物1,2,3的纵向移动速度

	Fixture bc1, bc2;
	WYSize s;
	Box2DRender render;
	float y1 = +17.8f, y2 = 0;
	protected Box2D mBox2D;

	grass() {
		mBox2D = Box2D.make();

		mBox2D.setDebugDraw(false);
		render = Box2DRender.make();
		mBox2D.setBox2DRender(render);

		mWorld = mBox2D.getWorld();

		s = Director.getInstance().getWindowSize();
		addChild(mBox2D);
		// set gravity
		mWorld.setGravity(0, 30);// 47

		first();

		// place box2d to center of bottom edge
		mBox2D.setPosition(s.width / 2, 0);
		mWorld.setContactListener(this);

		// add contact listener
		schedule(new TargetSelector(this, "update(float)", new Object[] { 0f }));
	}

	public void first() {
		{
			BodyDef bd = BodyDef.make();
			bd.setType(Body.TYPE_DYNAMIC);
			bd.setPosition(-5, 5);
			Body body = mWorld.createBody(bd);
			bd.destroy();
			PolygonShape shape = PolygonShape.make();
			shape.setAsBox(5f, 10f);

			grass1 = body.createFixture(shape, 0.0f);
			Texture2D tex = Texture2D.makePNG(R.drawable.branch_upper_flip);
			tex.autoRelease();
			render.bindTexture(grass1, tex);
		}
		{
			BodyDef bd = BodyDef.make();
			bd.setType(Body.TYPE_DYNAMIC);
			bd.setPosition(0, 65);
			Body body = mWorld.createBody(bd);
			bd.destroy();
			PolygonShape shape = PolygonShape.make();
			shape.setAsBox(12f, 12f);

			grass2 = body.createFixture(shape, 0.0f);
			Texture2D tex = Texture2D.makePNG(R.drawable.branch_lower);
			tex.autoRelease();
			render.bindTexture(grass2, tex);
		}
		{
			BodyDef bd = BodyDef.make();
			bd.setType(Body.TYPE_DYNAMIC);
			bd.setPosition(0, 120);
			Body body = mWorld.createBody(bd);
			bd.destroy();
			PolygonShape shape = PolygonShape.make();
			shape.setAsBox(13f, 13f);

			grass3 = body.createFixture(shape, 0.0f);
			Texture2D tex = Texture2D.makePNG(R.drawable.vines);
			render.bindTexture(grass3, tex);
			tex.autoRelease();
		}
	}

	public void update(float delta) {
		mWorld.step(1.f / 60.f, 10, 10);
		mWorld.clearForces();
		if (Box2.collision2 == true) {
			grass1.getBody().setLinearVelocity(
					WYPoint.make(grassx1, Box2.jump1 * (-19.14f + grassy1)));
			grass2.getBody().setLinearVelocity(
					WYPoint.make(grassx2, Box2.jump1 * (-19.14f + grassy2)));
			grass3.getBody().setLinearVelocity(
					WYPoint.make(grassx3, Box2.jump1 * (-19.14f + grassy3)));
			Box2.collision2 = false;
		}
		if (grass1.getBody().getPosition().y < -80) {
			float a = grass1.getBody().getPosition().x;
			grass1.getBody().setTransform(a, 80, 0);

			if (BC.grass1 == true) {
				BC.grass1 = false;
				if (BC.chushi < 29 && BC.chushi > 27) {
					Texture2D tex = Texture2D.makePNG(R.drawable.noimage);
					render.bindTexture(grass1, tex);
					tex.autoRelease();
				} else if (BC.chushi == 25) {
					Texture2D tex = Texture2D.makePNG(R.drawable.building);
					render.bindTexture(grass1, tex);
					tex.autoRelease();
				} else if (BC.chushi == 23) {
					Texture2D tex = Texture2D.makePNG(R.drawable.rock1);
					render.bindTexture(grass1, tex);
					tex.autoRelease();
				} else {
					Texture2D tex = Texture2D.makePNG(R.drawable.noimage);
					render.bindTexture(grass1, tex);
					tex.autoRelease();
				}
			}
		}
		if (grass2.getBody().getPosition().y < -80) {
			float a = grass2.getBody().getPosition().x;
			grass2.getBody().setTransform(a, 80, 0);
			if (BC.grass2 == true) {
				BC.grass2 = false;
				if (BC.chushi == 28) {

					Texture2D tex = Texture2D.makePNG(R.drawable.branch_blue);
					render.bindTexture(grass2, tex);
					tex.autoRelease();
				} else if (BC.chushi == 26 && BC.chushi == 27) {
					Texture2D tex = Texture2D.makePNG(R.drawable.cloud_white_2);
					render.bindTexture(grass2, tex);
					grassx2 = -3f;
					tex.autoRelease();
				} else {
					Texture2D tex = Texture2D.makePNG(R.drawable.noimage);
					render.bindTexture(grass2, tex);
					tex.autoRelease();
				}
			}

		}
		if (grass3.getBody().getPosition().y < -80) {
			float a = grass3.getBody().getPosition().x;
			if (BC.chushi != 23) {
				grass3.getBody().setTransform(a, 80, 0);
			} else {
				grass3.getBody().setTransform(a, 280, 0);
			}

			if (BC.grass3 == true) {
				BC.grass3 = false;
				if (BC.chushi == 28) {

					Texture2D tex = Texture2D.makePNG(R.drawable.bridge);
					render.bindTexture(grass3, tex);
					tex.autoRelease();
				} else if (BC.chushi == 25 || BC.chushi == 24
						|| BC.chushi == 26 || BC.chushi == 27) {
					Texture2D tex = Texture2D.makePNG(R.drawable.cloud_city);
					render.bindTexture(grass3, tex);
					tex.autoRelease();
				} else if (BC.chushi == 23) {
					Texture2D tex = Texture2D
							.makePNG(R.drawable.asteroids_deep);
					render.bindTexture(grass3, tex);
					grassy3 = -40f;
					tex.autoRelease();
				} else if (BC.chushi == 22) {
					Texture2D tex = Texture2D.makePNG(R.drawable.yellow_clouds);
					render.bindTexture(grass3, tex);
					grassy3 = -5f;
					tex.autoRelease();
				} else if (BC.chushi == 19) {
					Texture2D tex = Texture2D.makePNG(R.drawable.cloud_white_1);
					render.bindTexture(grass3, tex);
					grassx3 = 4f;
					grassy3 = 0f;
					tex.autoRelease();
				}
			}
		}
		if (grass3.getBody().getPosition().x < -24) {
			float a = grass3.getBody().getPosition().y;
			grass3.getBody().setTransform(24, a, 0);
		}
		if (grass3.getBody().getPosition().x > 24) {
			float a = grass3.getBody().getPosition().y;
			grass3.getBody().setTransform(-24, a, 0);
		}
		if (grass2.getBody().getPosition().x < -24) {
			float a = grass2.getBody().getPosition().y;
			grass2.getBody().setTransform(24, a, 0);
		}
		if (grass2.getBody().getPosition().x > 24) {
			float a = grass2.getBody().getPosition().y;
			grass2.getBody().setTransform(-24, a, 0);
		}
		if (grass1.getBody().getPosition().x < -24) {
			float a = grass1.getBody().getPosition().y;
			grass1.getBody().setTransform(24, a, 0);
		}
		if (grass1.getBody().getPosition().x > 24) {
			float a = grass1.getBody().getPosition().y;
			grass1.getBody().setTransform(-24, a, 0);
		}
	}

	@Override
	public void beginContact(int contactPointer) {
		// TODO Auto-generated method stub
		Contact contact = Contact.from(contactPointer);
		contact.setEnabled(false);
	}

	@Override
	public void endContact(int contactPointer) {
		// TODO Auto-generated method stub
		Contact contact = Contact.from(contactPointer);
		contact.setEnabled(false);
	}

	@Override
	public void postSolve(int contactPointer, int arg1) {
		// TODO Auto-generated method stub
		Contact contact = Contact.from(contactPointer);
		contact.setEnabled(false);
	}

	@Override
	public void preSolve(int contactPointer, int arg1) {
		// TODO Auto-generated method stub
		Contact contact = Contact.from(contactPointer);
		contact.setEnabled(false);
	}
}
最上面一层的设计

Box2.java

import java.nio.channels.Selector;
import java.util.Random;

import com.wiyun.engine.box2d.Box2D;
import com.wiyun.engine.box2d.Box2DRender;
import com.wiyun.engine.box2d.FixtureAnimation;
import com.wiyun.engine.box2d.collision.PolygonShape;
import com.wiyun.engine.box2d.dynamics.Body;
import com.wiyun.engine.box2d.dynamics.BodyDef;
import com.wiyun.engine.box2d.dynamics.Fixture;
import com.wiyun.engine.box2d.dynamics.World;
import com.wiyun.engine.box2d.dynamics.World.IContactListener;
import com.wiyun.engine.box2d.dynamics.contacts.Contact;
import com.wiyun.engine.nodes.Director;
import com.wiyun.engine.nodes.Layer;
import com.wiyun.engine.nodes.Scheduler;
import com.wiyun.engine.nodes.Timer;
import com.wiyun.engine.opengl.Texture2D;
import com.wiyun.engine.particle.ParticleSystem;
import com.wiyun.engine.particle.QuadParticleSystem;
import com.wiyun.engine.types.WYPoint;
import com.wiyun.engine.types.WYSize;
import com.wiyun.engine.utils.TargetSelector;

public class Box2 extends Layer implements IContactListener {
	World mWorld;
	Body m_character;

	float x;
	float y;
	float taijiex=1;
	static float change1 = 1, jump1 = 1,boost1=0;
	boolean hide=false;
	int donghua = 0; // 娃娃上升的判断条件(在碰撞时被赋值为true)
	int donghua2 = 1; // 娃娃下落的判断条件(在娃娃速度为0时被赋值为true)
	WYSize s;// 屏幕大小
	Character character;// 角色类的声明
	Taijie[] taijie;// 台阶类的声明
	WYPoint sp;// 整个场景的移动速度(碰撞后台阶都赋予这个速度) ps:grass1,2,3要自己重新赋速度了
	int mum = 0;
	int move=-1;
	Timer t ;
	static Box2DRender render;// 向一个关联里面加入图片或者动画信息
	static boolean collision = false, collision2 = false;
	boolean taijiemove=false;
	protected Box2D mBox2D;
	ParticleSystem emitter;
	Fixture jump, boost, change;

	Box2() {
		mBox2D = Box2D.make();

		mBox2D.setDebugDraw(false);
		render = Box2DRender.make();
		mBox2D.setBox2DRender(render);

		mWorld = mBox2D.getWorld();
		sp = WYPoint.make(0f, 0f);
		character = new Character(mWorld, 15f);
		taijie = new Taijie[10];
		{
			for (int i = 0; i < taijie.length; i++) {
				taijie[i] = new Taijie(mWorld);
				taijie[i].m_platform.getBody().setTransform(0, taijie[i].m_platform.getBody().getPosition().y + 4.6f, 0);
			}
		}
		Random a = new Random();
		{
			BodyDef bd = BodyDef.make();
			bd.setType(Body.TYPE_DYNAMIC);
			bd.setPosition((float) a.nextInt(13) - 5.5f, 2.1f * 80f);
			Body body = mWorld.createBody(bd);
			bd.destroy();
			PolygonShape shape = PolygonShape.make();
			shape.setAsBox(1.3f, 1.3f);
			jump = body.createFixture(shape, 0.0f);
			Texture2D tex = Texture2D.makePNG(R.drawable.jump);
			render.bindTexture(jump, tex);
			tex.autoRelease();
		}
		{
			BodyDef bd = BodyDef.make();
			bd.setType(Body.TYPE_DYNAMIC);
			bd.setPosition((float) a.nextInt(13) - 5.5f, 2.1f * 180f);
			Body body = mWorld.createBody(bd);
			bd.destroy();
			PolygonShape shape = PolygonShape.make();
			shape.setAsBox(1.3f, 1.3f);
			boost = body.createFixture(shape, 0.0f);
			Texture2D tex = Texture2D.makePNG(R.drawable.boost);
			render.bindTexture(boost, tex);
			tex.autoRelease();
		}
		{
			BodyDef bd = BodyDef.make();
			bd.setType(Body.TYPE_DYNAMIC);
			bd.setPosition((float) a.nextInt(13) - 5.5f, 2.1f * 150f);
			Body body = mWorld.createBody(bd);
			bd.destroy();
			PolygonShape shape = PolygonShape.make();
			shape.setAsBox(1.3f, 1.3f);
			change = body.createFixture(shape, 0.0f);
			Texture2D tex = Texture2D.makePNG(R.drawable.changge);
			render.bindTexture(change, tex);
			tex.autoRelease();
		}
		s = Director.getInstance().getWindowSize();
		
		emitter = new ParticleSnow();
		emitter.setPosition(-1000, -1000);
        main.mScene.addChild(emitter);
		addChild(mBox2D);
		// set gravity
		mWorld.setGravity(0, 47);// 47
		
		// place box2d to center of bottom edge
		mBox2D.setPosition(s.width / 2, 0);

		// add contact listener
		mWorld.setContactListener(this);
		setAccelerometerEnabled(true);
		schedule(new TargetSelector(this, "update(float)", new Object[] { 0f }));
		
	}

	public void update(float delta) {
		mWorld.step(1.f / 60.f, 10, 10);
		mWorld.clearForces();
		float top = 0;
		for (int i2 = 0; i2 < taijie.length; i2++) {

			if (taijie[i2].m_platform.getBody().getPosition().y > top) {
				top = taijie[i2].m_platform.getBody().getPosition().y;
			}
		}
		for (int i2 = 0; i2 < taijie.length; i2++) {
			if (taijie[i2].m_platform.getBody().getPosition().y < -2) {
				Random a= new Random();

				Random b = new Random();
				Random c = new Random();
				b.nextInt(30);
				if(i2==move)
				{
					Scheduler.getInstance().unschedule(t);
					move=-1;
					taijie[i2].m_platform.getBody().setLinearVelocity(WYPoint.make(0, taijie[i2].m_platform.getBody().getLinearVelocity().y));
					Texture2D tex = Texture2D.makePNG(R.drawable.bar1);
	    			render.bindTexture(taijie[i2].m_platform, tex);
	    			tex.autoRelease();
				}
				if(hide==true&&b.nextInt(30)>BC.chushi)
				{
				taijie[i2].m_platform.getBody().setTransform(-100f, top + 4.6f, 0);
	            hide=false;
				}else
				{
				taijie[i2].m_platform.getBody().setTransform((float) a.nextInt(16) - 7.5f, top + 4.6f, 0);
                hide=true;
                if(c.nextInt(30)+1>BC.chushi&&move==-1)
				{
                Random e = new Random();
                
                	taijie[i2].m_platform.getBody().setTransform(-3f, taijie[i2].m_platform.getBody().getPosition().y, 0);
    	            
                	taijie[i2].m_platform.getBody().setLinearVelocity(WYPoint.make(+e.nextInt(2)+4, taijie[i2].m_platform.getBody().getLinearVelocity().y));
                
                Texture2D tex = Texture2D.makePNG(R.drawable.bar3);
    			render.bindTexture(taijie[i2].m_platform, tex);
    			tex.autoRelease();
                TargetSelector mSelector1 = new TargetSelector(this, "updatechange(float,int,int)", new Object[] { 0f, 1 ,i2});
            	t = new Timer(mSelector1, 2f);
            	Scheduler.getInstance().schedule(t);
            	move=i2;
            	System.out.println("时间已经设置了");
				}
				}
				GameView.fenshu++;
			}
		}
		/*
		for (int i2 = 0; i2 < taijie.length; i2++) {
			if (taijie[i2].m_platform.getBody().getPosition().x < -2) {
				taijie[i2].m_platform.getBody().setLinearVelocity(WYPoint.make(taijie[i2].m_platform.getBody().getLinearVelocity().x*-1f, 0));
			}else if(taijie[i2].m_platform.getBody().getPosition().x >20)
			{
				taijie[i2].m_platform.getBody().setLinearVelocity(WYPoint.make(taijie[i2].m_platform.getBody().getLinearVelocity().x*-1f, 0));
			}
		}*/
		if (GameView.score < GameView.fenshu * 100) {
			GameView.score += 9;
		}
		if (jump.getBody().getPosition().y < -11.1) {
			Random a = new Random();
			jump.getBody().setTransform((float) a.nextInt(13) - 5.5f, 650, 0);
		}
		if (change.getBody().getPosition().y < -11.1) {
			Random a = new Random();
			change.getBody().setTransform((float) a.nextInt(13) - 5.5f, 650, 0);
		}
		if (boost.getBody().getPosition().y < -11.1) {
			Random a = new Random();
			boost.getBody().setTransform((float) a.nextInt(13) - 5.5f, 650, 0);
		}
		if (character.m_character.getBody().getPosition().x < -11.1) {
			float a = character.m_character.getBody().getPosition().y;
			character.m_character.getBody().setTransform(11, a, 0);
		} else if (character.m_character.getBody().getPosition().x > 11.1) {
			float a = character.m_character.getBody().getPosition().y;
			character.m_character.getBody().setTransform(-11, a, 0);
		}

		if(boost1==0)
		{
		if (taijie[0].m_platform.getBody().getLinearVelocity().y >= 0
				&& donghua2 == 1) {
			FixtureAnimation anim = FixtureAnimation.make(0.2f,
					R.drawable.down, R.drawable.down2, R.drawable.down3);
			anim.setLoop(true);
			anim.start(character.m_character);
			donghua2 = 0;
		} else if (donghua == 1) {
			FixtureAnimation anim = FixtureAnimation.make(0.15f,
					R.drawable.up1, R.drawable.up2, R.drawable.up3,
					R.drawable.up4, R.drawable.up5, R.drawable.up6);
			anim.setLoop(false);
			anim.start(character.m_character);
			donghua = 0;
		}
		}else
		{
			FixtureAnimation anim = FixtureAnimation.make(0.15f,
					R.drawable.up1, R.drawable.up2, R.drawable.up3,
					R.drawable.up4, R.drawable.up5, R.drawable.up6);
			anim.setLoop(false);
			anim.start(character.m_character);
		}

	}

	public void updatechange(float a,int c,int n)
	{
		taijie[n].m_platform.getBody().setLinearVelocity(WYPoint.make(taijie[move].m_platform.getBody().getLinearVelocity().x*-1f, taijie[move].m_platform.getBody().getLinearVelocity().y));
	}
	
	public void updategravityOnce(float a) {
		BC.mWorld.setGravity(0, 4);
		grass.mWorld.setGravity(0, 30);
		mWorld.setGravity(0, 47);
		Texture2D tex = Texture2D.makePNG(R.drawable.boost);
		render.bindTexture(boost, tex);
		tex.autoRelease();
		boost1=0;
		emitter.setPosition(-1000, -1000);
		
	}

	public void updatejumpOnce(float a) {
		jump1 = 1;
		Texture2D tex = Texture2D.makePNG(R.drawable.jump);
		render.bindTexture(jump, tex);
		tex.autoRelease();
	}

	public void updatechangeOnce(float a) {
		change1 = 1;
		Texture2D tex = Texture2D.makePNG(R.drawable.changge);
		render.bindTexture(change, tex);
		tex.autoRelease();
	}

	@Override
	public void wyAccelerometerChanged(float accelX, float accelY, float accelZ) {

		x = accelX;
		character.m_character.getBody().setLinearVelocity(
				WYPoint.make(change1
						* (5f * x + character.m_character.getBody()
								.getLinearVelocity().x * 0.8f), -y));
		if(boost1==1)
		{
		emitter.setPosition(character.m_character.getBody().getPosition().x*24+s.width/2, character.m_character.getBody().getPosition().y*24-50);
		}
		// update(1f);
		/*
		 * if(character.m_character.getBody().getPosition().x<-15f) {
		 * character=null; character=new Character(mWorld,30f); }
		 * if(character.m_character.getBody().getPosition().x>30f) {
		 * character=null; character=new Character(mWorld,-14.5f); }
		 */
	}

	@Override
	public void beginContact(int contactPointer) {
		// TODO Auto-generated method stub
		/**/

	}

	@Override
	public void endContact(int arg0) {
		// TODO Auto-generated method stub
		y = 0;
	}

	@Override
	public void postSolve(int arg0, int arg1) {
		// TODO Auto-generated method stub

	}

	@Override
	public void preSolve(int contactPointer, int oldManifoldPointer) {
		// TODO Auto-generated method stub
		Contact contact = Contact.from(contactPointer);
		Fixture fixtureA = contact.getFixtureA();
		Fixture fixtureB = contact.getFixtureB();
		boolean pengzhuang = false;
		int i = 0;

		if (fixtureA.equals(character.m_character)) {
			pengzhuang = true;
			if (fixtureB.equals(boost)) {
				BC.mWorld.setGravity(0, -3);
				grass.mWorld.setGravity(0, -22);
				mWorld.setGravity(0, -35);
				Texture2D tex = Texture2D.makePNG(R.drawable.noimage);
				render.bindTexture(boost, tex);
				tex.autoRelease();
				boost1=1;
				emitter.setPosition(character.m_character.getBody().getPosition().x*24+s.width/2, character.m_character.getBody().getPosition().y*24-50);
				
				scheduleOnce(new TargetSelector(this,
						"updategravityOnce(float)", new Object[] { 0f }), 4f);
				pengzhuang = false;
				GameView.gameview.play2();
				
				
			} else if (fixtureB.equals(jump)) {
				jump1 = 2f;
				Texture2D tex = Texture2D.makePNG(R.drawable.noimage);
				render.bindTexture(jump, tex);
				tex.autoRelease();
				scheduleOnce(new TargetSelector(this, "updatejumpOnce(float)",new Object[] { 0f }), 5f);
				pengzhuang = false;
			} else if (fixtureB.equals(change)) {
				change1 = -1;
				Texture2D tex = Texture2D.makePNG(R.drawable.noimage);
				render.bindTexture(change, tex);
				tex.autoRelease();
				scheduleOnce(new TargetSelector(this,
						"updatechangeOnce(float)", new Object[] { 0f }), 3f);
				pengzhuang = false;
			}

			while (!fixtureB.equals(taijie[i].m_platform)
					&& i < taijie.length - 1) {
				i++;
			}

		} else if (fixtureB.equals(character.m_character)) {
			pengzhuang = true;

			while (!fixtureA.equals(taijie[i].m_platform)
					&& i < taijie.length - 1) {
				i++;
			}

		}

		if (pengzhuang == true) {

			if (taijie[i].m_platform.getBody().getLinearVelocity().y > 0
					&& taijie[i].m_platform.getBody().getPosition().y + 1.2f < character.m_character
							.getBody().getPosition().y) {
				contact.setEnabled(true);
				sp = WYPoint.make(0f, -30f * jump1);
				for (int i2 = 0; i2 < taijie.length; i2++) {
					if(i2==move)
					{
					taijie[i2].m_platform.getBody().setLinearVelocity(WYPoint.make(taijie[i2].m_platform.getBody().getLinearVelocity().x, -30f * jump1));
					}
					else
					{
						taijie[i2].m_platform.getBody().setLinearVelocity(WYPoint.make(0, -30f * jump1));
							
					}
				}
				donghua = 1;
				donghua2 = 1;
				change.getBody().setLinearVelocity(sp);
				jump.getBody().setLinearVelocity(sp);
				boost.getBody().setLinearVelocity(sp);
				if(jump1==1)
				{
				GameView.gameview.play1();
				}else if(jump1==2)
				{
				GameView.gameview.play3();	
				}
				System.out.println("碰撞了");
				collision = true;
				collision2 = true;

				/*
				 * for(int i2=mum;i2<taijie.length;i2++) {
				 * if(taijie[i2].m_platform.getBody().getPosition().y<-5) {
				 * mWorld.destroyBody(taijie[i2].m_platform.getBody());
				 * main.jishu++; float y=main.jishu*5f; Random aa=new Random();
				 * BodyDef bd = BodyDef.make(); bd.setType(Body.TYPE_DYNAMIC);
				 * bd.setPosition((float)aa.nextInt(20)-10, y); Body body =
				 * mWorld.createBody(bd); bd.destroy();
				 * 
				 * 
				 * PolygonShape shape = PolygonShape.make();
				 * shape.setAsBox(2.0f, 0.4f); body.createFixture(shape, 0.0f);
				 * } }
				 */
			} else {
				contact.setEnabled(false);
			}
		} else {
			contact.setEnabled(false);
		}

	}
	public class ParticleSnow extends QuadParticleSystem {
		

		protected ParticleSnow() {
			this(700);
		}

		protected ParticleSnow(int p) {
			super(p);

			// duration
	        setDuration(PARTICLE_DURATION_INFINITY);
	    	
	    	// Gravity mode: speed of particles
	        setSpeedVariance(160, 20);
	    	
	    	// Gravity mode: radial
	        setRadialAccelerationVariance(-90, 0);
	    	
	    	// Gravity mode: tagential
	        setTangentialAccelerationVariance(0, 0);
	        // gravity
			setParticleGravity(0, -1200);
	    	// angle
	        setDirectionAngleVariance(90, 360);
	    		
	    	// life of particles
	        setLifeVariance(3, 1);

	    	// spin of particles
	        setEndSpinVariance(0, 2000);
	    	
	        // color of particles
	        setStartColorVariance(0.9f, 0.9f, 0.9f, 1f, 0f, 0f, 0.1f, 0f);
			setEndColorVariance(1f, 1f, 1f, 1f, 0f, 0f, 0f, 0f);

	    	// size, in pixels
	        setStartSizeVariance(20, 0);
	        setEndSizeVariance(PARTICLE_START_SIZE_EQUAL_TO_END_SIZE, 0);
	    	
	    	// emits per second
	        setEmissionRate(50);
	    	
			setTexture(Texture2D.makePNG(R.drawable.xingxing));

			// additive

			setBlendAdditive(false);
		}
	}

}


你可能感兴趣的:(游戏,object,Random,character,float,引擎)