package Bai_13_e3droid.gioi;
import java.util.ArrayList;
import android.view.MotionEvent;
import com.e3roid.E3Activity;
import com.e3roid.E3Engine;
import com.e3roid.E3Scene;
import com.e3roid.drawable.sprite.AnimatedSprite;
import com.e3roid.drawable.texture.TiledTexture;
import com.e3roid.event.SceneUpdateListener;
//Để tạo được vòng lặp trong game ta cần implements SceneUpdateListener
public class Bai_13_e3droidActivity extends E3Activity implements SceneUpdateListener {
private final static int WIDTH = 320;
private final static int HEIGHT = 480;
//Khai báo 2 biến đê quản lý nhạc nền và âm thanh nổ
private Sound nhac_nen;
private Sound no;
private TiledTexture sunmonster;//Biến này để load ảnh nhé
private TiledTexture explosion;
private AnimatedSprite[] sprite;// Cái này là cái mới nè.Mình khai báo 1 mảng AnimatedSprite.
//Cái này sẽ dùng để vẽ 1 cái mặt kiểu Yahoo ý, và 1 vụ nổ.
//Cài này dùng để cắt cái ảnh của mình ra để lấy từng Frame 1
private ArrayList<AnimatedSprite.Frame> Frames_sunmonster = new ArrayList<AnimatedSprite.Frame>();
private ArrayList<AnimatedSprite.Frame> Frames_explosion = new ArrayList<AnimatedSprite.Frame>();
//---------------------------------------------------------------------------------------
@Override
public E3Engine onLoadEngine() {
E3Engine engine = new E3Engine(this, WIDTH, HEIGHT);
engine.requestFullScreen();
engine.requestPortrait();
return engine;
}
//---------------------------------------------------------------------------------------
@Override
public E3Scene onLoadScene() {
//Khởi tạo 2 biến quản lý âm thanh
nhac_nen = new Sound(this, R.raw.nhacnen);
no = new Sound(this, R.raw.no);
E3Scene scene = new E3Scene();
scene.addEventListener(this);
//Cứ 30ms là làm tươi màn hình 1 lần
scene.registerUpdateListener(30, this);
//Giờ ta sẽ khởi tạo các AnimatedSprite
sprite = new AnimatedSprite[3];//Khai bảo mảng này có 3 phần tử
//Đặt các ảnh và các sprite và khởi tạo vị trí của sprite này
//Đặt cả 2 đối tượng này ở giữa theo trục x nhé.
int centerX = (getWidth() - sunmonster.getTileWidth()) / 2;
sprite[0] = new AnimatedSprite(sunmonster, centerX, 50);//Cái này đặt vị trí y=50
centerX = (getWidth() - explosion.getTileWidth()) / 2;
sprite[1] = new AnimatedSprite(sunmonster, centerX, getHeight()-50);//Cái này đặt ở cuối màn hình - 50 để bạn thấy nó luôn
//Ở trên ta đã khởi tạo và đặt vị trí cho 2 sprite
//Giờ ta add frame vào cho sprite
sprite[0]. animate(100, Frames_sunmonster);//Cứ 100ms là vẽ 1 cái ảnh
sprite[1].animate(100, Frames_sunmonster);
//Add xong frame rồi. giờ ta cho nó hiện thị thôi.
scene.getTopLayer().add(sprite[0]);
scene.getTopLayer().add(sprite[1]);
//Phần trên ta mới tạo ra 2 cái mặt cưới thồi. Giờ ta thêm 1 cái để làm vụ nổ nhé.
sprite[2] = new AnimatedSprite(explosion, (getWidth() - explosion.getTileWidth()) / 2, (getHeight() - explosion.getTileWidth()) / 2);//Đặt nó vào vị trí trung tâm
//Add frame
sprite[2].animate(100, Frames_explosion);
//Hiện thị
scene.getTopLayer().add(sprite[2]);
//Tạm thời ta cho ẩn sprite[2]ẩn đi.
sprite[2].hide();
//Bắt đầu game thì ta bật nhạc nền.
nhac_nen.play();
return scene;
}
//---------------------------------------------------------------------------------------
@Override
public void onLoadResources() {
/*TiledTexture(java.lang.String name,
*int width,
*int height,
*int xindex,
*int yindex,
*int border,
*Context context)
*/
//Tại sao lại là 192/6: chiều rộng thực tế của ảnh là 192 và trong đó có 6 cái ảnh nhỏ, mỗi ảnh nhỏ là 1 sprite
//nên ta mang chia cho 6 để lấy chiều rộng của 1 sprite
sunmonster = new TiledTexture("sunmonster.png", 192/6, 32, 0, 0, 0, this);//Load ảnh mặt cười nè.
explosion = new TiledTexture("explosion.png", 162/5, 32, 0, 0, 0, this);//Cái này là ảnh tạo ra vụ nổ.
//Ở đây ta thực hiện cắt lấy từng frame của 2 bức ảnh trên
Frames_sunmonster = new ArrayList<AnimatedSprite.Frame>();
//Ta dùng phương thức add để lấy từng phần của bức ảnh nhé
//.add(new AnimatedSprite.Frame(chỉ số cột, chỉ số hàng)); Chỉ số bắt đầu là 0 nhé.
Frames_sunmonster.add(new AnimatedSprite.Frame(0, 0));//Cột 0, hàng 0
Frames_sunmonster.add(new AnimatedSprite.Frame(1, 0));//Cột 1, hàng 0
Frames_sunmonster.add(new AnimatedSprite.Frame(2, 0));//Cột 2, hàng 0
Frames_sunmonster.add(new AnimatedSprite.Frame(3, 0));//Cột 3, hàng 0
Frames_sunmonster.add(new AnimatedSprite.Frame(4, 0));//Cột 4, hàng 0
Frames_sunmonster.add(new AnimatedSprite.Frame(5, 0));//Cột 5, hàng 0
// cái này tương tự cái trên nhé.
Frames_explosion = new ArrayList< AnimatedSprite.Frame>();
Frames_explosion.add(new AnimatedSprite.Frame(0, 0));
Frames_explosion.add(new AnimatedSprite.Frame(1, 0));
Frames_explosion.add(new AnimatedSprite.Frame(2, 0));
Frames_explosion.add(new AnimatedSprite.Frame(3, 0));
Frames_explosion.add(new AnimatedSprite.Frame(4, 0));
}
//---------------------------------------------------------------------------------------
public boolean onSceneTouchEvent(E3Scene scene, MotionEvent motionEvent) {
//Khi chạm vào màn hình thì tắt nhạc nền
nhac_nen.stop();
return false;
}
//---------------------------------------------------------------------------------------
/*
* Trong vòng lặp này ta sẽ cho 2 mặt cười di chuyển. một mặt di chuyển từ trên xuống, một mặt di chuyển từ dưới
* lên. Khi 2 mặt này chạm vào nhau thì có 1 vụ nổ. Khi sảy ra vụ nổ này thì 2 cái mặt cười sẽ biến mất*/
@Override
public void onUpdateScene(E3Scene arg0, long arg1) {
runOnUiThread(new Runnable() {
public void run() {
sprite[0].moveRelativeY(2);
sprite[1].moveRelativeY(-2);//Mỗi lần ta di chuyển 2px
if(sprite[0].collidesWith(sprite[1])){//Kiểm tra va chạm
//Khi 2 mặt cười này chạm vào nhau ta ẩn 2 mặt cười này đi
sprite[0].hide();
sprite[1].hide();
//Ta cho hiện thị cái vụ nổ lên
sprite[2].show();
//Khi hiện thị bom thì ta cho phát tiếng nổ.(Cho phép lặp lại)
no.loop();
}
}
});
}
}