1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
| package Moteurs;
import java.util.List;
import sdz.chapitreCinq.Bloc;
import sdz.chapitreCinq.Personnage;
import LabyWay.Activity.R;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class LabyrintheView extends SurfaceView implements SurfaceHolder.Callback {
Personnage mBoule;
private Bitmap mur, personnage;
public Personnage getBoule() {
return mBoule;
}
public void setPersonnge(Personnage pBoule) {
this.mBoule = pBoule;
}
SurfaceHolder mSurfaceHolder;
DrawingThread mThread;
private List<Bloc> mBlocks = null;
public List<Bloc> getBlocks() {
return mBlocks;
}
public void setBlocks(List<Bloc> pBlocks) {
this.mBlocks = pBlocks;
}
public LabyrintheView(Context pContext) {
super(pContext);
mSurfaceHolder = getHolder();
mSurfaceHolder.addCallback(this);
mThread = new DrawingThread();
mur = BitmapFactory.decodeResource(getResources(), R.drawable.caissevide);
personnage = BitmapFactory.decodeResource(getResources(), R.drawable.fantface);
int taillePerso = personnage.getWidth();
// Taille du perso, nb vie
mBoule = new Personnage(taillePerso, 3);
System.out.println("ccccc111111");
}
public LabyrintheView(Context context, AttributeSet attrs) {
super(context, attrs);
mSurfaceHolder = getHolder();
mSurfaceHolder.addCallback(this);
mThread = new DrawingThread();
mur = BitmapFactory.decodeResource(getResources(), R.drawable.caissevide);
personnage = BitmapFactory.decodeResource(getResources(), R.drawable.fantface);
int taillePerso = personnage.getWidth();
// Taille du perso, nb vie
mBoule = new Personnage(taillePerso, 3);
System.out.println("ccccc222222");
}
public LabyrintheView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
mSurfaceHolder = getHolder();
mSurfaceHolder.addCallback(this);
mThread = new DrawingThread();
mur = BitmapFactory.decodeResource(getResources(), R.drawable.caissevide);
personnage = BitmapFactory.decodeResource(getResources(), R.drawable.fantface);
int taillePerso = personnage.getWidth();
// Taille du perso, nb vie
mBoule = new Personnage(taillePerso, 3);
System.out.println("ccccc33333");
}
@Override
protected void onDraw(Canvas pCanvas) {
int x, y;
Bitmap depart = BitmapFactory.decodeResource(getResources(), R.drawable.depart);
Bitmap arrivee = BitmapFactory.decodeResource(getResources(), R.drawable.arrivee);
Bitmap enigme = BitmapFactory.decodeResource(getResources(), R.drawable.enigmehdpi);
// Dessiner le fond de l'écran en premier
pCanvas.drawColor(R.color.Orange);
if(mBlocks != null) {
// Dessiner tous les blocs du labyrinthe
for(Bloc b : mBlocks) {
switch(b.getType()) {
case DEPART:
x = (int) b.getRectangle().left;
y = (int) b.getRectangle().top;
pCanvas.drawBitmap(depart,x,y,null);
break;
case ARRIVEE:
x = (int) b.getRectangle().left;
y = (int) b.getRectangle().top;
pCanvas.drawBitmap(arrivee, x,y,null);
break;
case MUR:
x = (int) b.getRectangle().left;
y = (int) b.getRectangle().top;
//mPaint.setColor(Color.BLACK);
pCanvas.drawBitmap(mur, x,y,null);
break;
case ENIGME:
x = (int) b.getRectangle().left;
y = (int) b.getRectangle().top;
//mPaint.setColor(Color.BLACK);
pCanvas.drawBitmap(enigme, x,y,null);
break;
default:
break;
}
}
}
// Dessiner la boule
if(mBoule != null) {
pCanvas.drawBitmap(personnage, mBoule.getCentreX(), mBoule.getCentreY(),null);
}
}
@Override
public void surfaceChanged(SurfaceHolder pHolder, int pFormat, int pWidth, int pHeight) {
//
}
@Override
public void surfaceCreated(SurfaceHolder pHolder) {
mThread.keepDrawing = true;
mThread.start();
// Quand on crée la boule, on lui indique les coordonnées de l'écran
if(mBoule != null ) {
this.mBoule.setHeight(getHeight());
this.mBoule.setWidth(getWidth());
}
}
@Override
public void surfaceDestroyed(SurfaceHolder pHolder) {
mThread.keepDrawing = false;
boolean retry = true;
while (retry) {
try {
mThread.join();
retry = false;
} catch (InterruptedException e) {}
}
}
@SuppressLint("WrongCall")
private class DrawingThread extends Thread {
boolean keepDrawing = true;
@Override
public void run() {
Canvas canvas;
while (keepDrawing) {
canvas = null;
try {
canvas = mSurfaceHolder.lockCanvas();
synchronized (mSurfaceHolder) {
onDraw(canvas);
}
} finally {
if (canvas != null) {
mSurfaceHolder.unlockCanvasAndPost(canvas);
}
}
// Pour dessiner à 100 fps
try {
Thread.sleep(10);
} catch (InterruptedException e) {}
}
}
}
} |
Partager