/
arti4
/
IndividualProject
Обзор
Документация
Войти
/
arti4
/
IndividualProject
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
MyGame/app/src/main/java/com/example/mygame/Crystal.java
157 строк
6 KB
arti4
upload files
18 ноя 2025, 12:41
18 ноя 2025, 12:41
f3a2f8a
Код
Авторство
О чём код?
package com.example.mygame; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Rect; import org.jbox2d.dynamics.Body; public class Crystal { private float x, y; private int width, height; private Rect crystalRect; private boolean isCollected = false; private int crystalId; private Body physicsBody; private PhysicsWorld physicsWorld; private float scaleFactor; private Context context; private Bitmap crystalBitmap; public Crystal(float x, float y, int crystalId, PhysicsWorld physicsWorld, float scaleFactor, Context context) { this.x = x; this.y = y; this.crystalId = crystalId; this.physicsWorld = physicsWorld; this.scaleFactor = scaleFactor; this.context = context; this.width = (int)(35 * scaleFactor); this.height = (int)(35 * scaleFactor); this.crystalRect = new Rect((int)x, (int)y, (int)(x + width), (int)(y + height)); createPhysicsBody(); loadCrystalBitmap(); } private void loadCrystalBitmap() { try { int resourceId = context.getResources().getIdentifier("ic_crystal", "drawable", context.getPackageName()); if (resourceId != 0) { Bitmap originalBitmap = BitmapFactory.decodeResource(context.getResources(), resourceId); crystalBitmap = Bitmap.createScaledBitmap(originalBitmap, width, height, true); } } catch (Exception e) { e.printStackTrace(); crystalBitmap = createFallbackBitmap(); } } private Bitmap createFallbackBitmap() { Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); Paint paint = new Paint(); paint.setAntiAlias(true); paint.setColor(Color.argb(255, 0, 200, 255)); paint.setStyle(Paint.Style.FILL); float centerX = width / 2f; float centerY = height / 2f; float size = Math.min(width, height) * 0.7f; android.graphics.Path diamond = new android.graphics.Path(); diamond.moveTo(centerX, centerY - size/2); diamond.lineTo(centerX + size/2, centerY); diamond.lineTo(centerX, centerY + size/2); diamond.lineTo(centerX - size/2, centerY); diamond.close(); canvas.drawPath(diamond, paint); paint.setColor(Color.argb(150, 255, 255, 255)); android.graphics.Path highlight = new android.graphics.Path(); highlight.moveTo(centerX - size/4, centerY - size/6); highlight.lineTo(centerX, centerY - size/4); highlight.lineTo(centerX + size/6, centerY - size/8); highlight.lineTo(centerX + size/8, centerY + size/8); highlight.close(); canvas.drawPath(highlight, paint); paint.setColor(Color.argb(200, 0, 150, 255)); paint.setStyle(Paint.Style.STROKE); paint.setStrokeWidth(2 * scaleFactor); canvas.drawPath(diamond, paint); return bitmap; } private void createPhysicsBody() { float centerX = x + width / 2.0f; float centerY = y + height / 2.0f; physicsBody = physicsWorld.createCrystalBody(centerX, centerY, width, height, this); } public void draw(Canvas canvas) { if (isCollected) return; if (crystalBitmap != null) { canvas.drawBitmap(crystalBitmap, x, y, null); } else { drawFallbackCrystal(canvas); } } private void drawFallbackCrystal(Canvas canvas) { Paint paint = new Paint(); paint.setAntiAlias(true); float centerX = x + width/2; float centerY = y + height/2; float size = width * 0.6f; android.graphics.Path diamond = new android.graphics.Path(); diamond.moveTo(centerX, centerY - size/2); diamond.lineTo(centerX + size/2, centerY); diamond.lineTo(centerX, centerY + size/2); diamond.lineTo(centerX - size/2, centerY); diamond.close(); paint.setColor(Color.argb(200, 0, 200, 255)); paint.setStyle(Paint.Style.FILL); canvas.drawPath(diamond, paint); paint.setColor(Color.argb(255, 0, 150, 255)); paint.setStyle(Paint.Style.STROKE); paint.setStrokeWidth(3 * scaleFactor); canvas.drawPath(diamond, paint); paint.setColor(Color.argb(150, 255, 255, 255)); paint.setStyle(Paint.Style.FILL); canvas.drawCircle(centerX - size/4, centerY - size/4, size/6, paint); } public boolean checkCollection(Rect motorcycleRect) { if (isCollected) return false; if (Rect.intersects(crystalRect, motorcycleRect)) { isCollected = true; if (physicsBody != null) { physicsWorld.markBodyForRemoval(physicsBody); } return true; } return false; } public void setCollected(boolean collected) { this.isCollected = collected; if (collected && physicsBody != null) { physicsWorld.markBodyForRemoval(physicsBody); } } public float getX() { return x; } public float getY() { return y; } public int getWidth() { return width; } public int getHeight() { return height; } public Rect getRect() { return crystalRect; } public boolean isCollected() { return isCollected; } public int getCrystalId() { return crystalId; } public Body getPhysicsBody() { return physicsBody; } }