/
arti4
/
IndividualProject
Обзор
Документация
Войти
/
arti4
/
IndividualProject
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
MyGame/app/src/main/java/com/example/mygame/Motorcycle.java
272 строки
11 KB
arti4
upload files
18 ноя 2025, 12:41
18 ноя 2025, 12:41
f3a2f8a
Код
Авторство
О чём код?
package com.example.mygame; import android.content.Context; import android.content.SharedPreferences; 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; import org.jbox2d.common.Vec2; public class Motorcycle { public Body physicsBody; private float x, y; private float rotation = 0f; private Rect motorcycleRect; private int screenWidth, screenHeight; private float scaleFactor; private PhysicsWorld physicsWorld; private int motorcycleType; private float[] enginePowers = {70.0f, 85.0f, 60.0f, 90.0f, 75.0f, 65.0f, 80.0f, 95.0f, 55.0f, 100.0f}; private float[] brakingPowers = {50.0f, 55.0f, 45.0f, 70.0f, 55.0f, 48.0f, 62.0f, 75.0f, 42.0f, 80.0f}; private float[] maxSpeeds = {22.0f, 25.0f, 20.0f, 27.0f, 23.0f, 21.0f, 24.0f, 28.0f, 19.0f, 30.0f}; private float[] jumpPowers = {1.2f, 1.3f, 1.0f, 1.5f, 1.3f, 1.1f, 1.35f, 1.6f, 0.9f, 1.7f}; private float enginePower; private float brakingPower; private float maxSpeed; private float jumpPower; private boolean isOnGround = true; private boolean wasOnGround = true; private Bitmap motorcycleBitmap; private Context context; private String[] motorcycleNames = { "Спортивный", "Гоночный", "Эндуро", "Супербайк", "Неоновый", "Фиолетовый", "Стальной", "Бело-синий", "Черно-зеленый", "Оранжевый чоппер" }; public Motorcycle(PhysicsWorld physicsWorld, int startX, int startY, int screenWidth, int screenHeight, float scaleFactor, int motorcycleType, Context context) { this.physicsWorld = physicsWorld; this.screenWidth = screenWidth; this.screenHeight = screenHeight; this.scaleFactor = scaleFactor; this.motorcycleType = motorcycleType; this.context = context; this.enginePower = enginePowers[motorcycleType]; this.brakingPower = brakingPowers[motorcycleType]; this.maxSpeed = maxSpeeds[motorcycleType]; this.jumpPower = jumpPowers[motorcycleType]; loadMotorcycleBitmap(); float width = getMotorcycleWidth(); float height = getMotorcycleHeight(); this.physicsBody = physicsWorld.createMotorcycleBody(startX + width/2, startY + height/2, width, height, this, motorcycleType); this.x = startX; this.y = startY; this.motorcycleRect = new Rect(startX, startY, startX + (int)width, startY + (int)height); } private void loadMotorcycleBitmap() { try { int resourceId = context.getResources().getIdentifier( "car_" + (motorcycleType + 1), "drawable", context.getPackageName() ); if (resourceId != 0) { Bitmap originalBitmap = BitmapFactory.decodeResource(context.getResources(), resourceId); int scaledWidth = (int)(getMotorcycleWidth()); int scaledHeight = (int)(getMotorcycleHeight()); motorcycleBitmap = Bitmap.createScaledBitmap(originalBitmap, scaledWidth, scaledHeight, true); } else { resourceId = context.getResources().getIdentifier("car_1", "drawable", context.getPackageName()); if (resourceId != 0) { Bitmap originalBitmap = BitmapFactory.decodeResource(context.getResources(), resourceId); int scaledWidth = (int)(getMotorcycleWidth()); int scaledHeight = (int)(getMotorcycleHeight()); motorcycleBitmap = Bitmap.createScaledBitmap(originalBitmap, scaledWidth, scaledHeight, true); } else { motorcycleBitmap = createFallbackBitmap(); } } } catch (Exception e) { e.printStackTrace(); motorcycleBitmap = createFallbackBitmap(); } } private Bitmap createFallbackBitmap() { int width = (int)getMotorcycleWidth(); int height = (int)getMotorcycleHeight(); Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); Paint paint = new Paint(); paint.setColor(Color.RED); canvas.drawRect(0, 0, width, height, paint); paint.setColor(Color.WHITE); paint.setTextSize(20 * scaleFactor); canvas.drawText("MOTO " + motorcycleType, width * 0.1f, height * 0.5f, paint); return bitmap; } public float getMotorcycleWidth() { switch (motorcycleType) { case 0: return 150 * scaleFactor; case 1: return 144 * scaleFactor; case 2: return 163 * scaleFactor; case 3: return 137 * scaleFactor; case 4: return 157 * scaleFactor; case 5: return 150 * scaleFactor; case 6: return 170 * scaleFactor; case 7: return 144 * scaleFactor; case 8: return 176 * scaleFactor; case 9: return 189 * scaleFactor; default: return 150 * scaleFactor; } } public float getMotorcycleHeight() { switch (motorcycleType) { case 0: return 88 * scaleFactor; case 1: return 82 * scaleFactor; case 2: return 101 * scaleFactor; case 3: return 75 * scaleFactor; case 4: return 95 * scaleFactor; case 5: return 88 * scaleFactor; case 6: return 108 * scaleFactor; case 7: return 85 * scaleFactor; case 8: return 114 * scaleFactor; case 9: return 69 * scaleFactor; default: return 88 * scaleFactor; } } public void update() { Vec2 pos = physicsWorld.getBodyPosition(physicsBody); float width = getMotorcycleWidth(); float height = getMotorcycleHeight(); this.x = pos.x - width/2; this.y = pos.y - height/2; this.rotation = (float)Math.toDegrees(physicsWorld.getBodyAngle(physicsBody)); Vec2 velocity = physicsBody.getLinearVelocity(); if (velocity.x > maxSpeed) { physicsBody.setLinearVelocity(new Vec2(maxSpeed, velocity.y)); } else if (velocity.x < -maxSpeed) { physicsBody.setLinearVelocity(new Vec2(-maxSpeed, velocity.y)); } wasOnGround = isOnGround; isOnGround = checkIfOnGround(); updateMotorcycleRect(); } private boolean checkIfOnGround() { Vec2 pos = physicsBody.getPosition(); float motorcycleBottom = pos.y / physicsWorld.getPhysicsScale() + getMotorcycleHeight()/2; float groundLevel = screenHeight * 0.7f; Vec2 velocity = physicsBody.getLinearVelocity(); boolean hasGroundContact = physicsWorld.hasGroundContact(physicsBody); boolean isNearSurface = Math.abs(motorcycleBottom - groundLevel) < 60f; boolean isStableVertically = Math.abs(velocity.y) < 5f; boolean isUpright = !isUpsideDown() || Math.abs(rotation) < 135f; return hasGroundContact || (isNearSurface && isStableVertically && isUpright); } private void updateMotorcycleRect() { float width = getMotorcycleWidth(); float height = getMotorcycleHeight(); motorcycleRect.set((int)x, (int)y, (int)(x + width), (int)(y + height)); } public void applyEngineForce(boolean isAccelerating) { if (isOnGround && isAccelerating) { Vec2 currentVelocity = physicsBody.getLinearVelocity(); if (currentVelocity.x < maxSpeed) { physicsBody.applyForceToCenter(new Vec2(enginePower, 0)); } } } public void applyBrake() { if (isOnGround) { Vec2 currentVelocity = physicsBody.getLinearVelocity(); if (currentVelocity.x < -0.3f) { physicsBody.applyForceToCenter(new Vec2(brakingPower, 0)); } else { physicsBody.setLinearVelocity(new Vec2(0, currentVelocity.y)); } } } public void rotateInAir() { if (!isOnGround) { physicsBody.applyAngularImpulse(-0.08f); } } public void moveHorizontal(float targetX) { if (isOnGround) { float currentX = getX(); float diffX = targetX - currentX; float lateralForce = diffX * 1.5f; lateralForce = Math.max(-12.0f, Math.min(lateralForce, 12.0f)); physicsBody.applyForceToCenter(new Vec2(0, lateralForce)); } } public void applyJump(float jumpPowerMultiplier) { Vec2 currentVelocity = physicsBody.getLinearVelocity(); float forwardImpulse = Math.max(3.0f, currentVelocity.x * 0.3f); float upwardImpulse = -jumpPower * jumpPowerMultiplier; Vec2 impulse = new Vec2(forwardImpulse, upwardImpulse); physicsBody.applyLinearImpulse(impulse, physicsBody.getWorldCenter()); physicsBody.applyForceToCenter(new Vec2(jumpPower * 0.8f, 0)); } public static boolean isMotorcycleAvailable(Context context, int motorcycleType) { if (motorcycleType == 0) return true; SharedPreferences prefs = context.getSharedPreferences("MotorcyclePrefs", Context.MODE_PRIVATE); String unlockedString = prefs.getString("unlocked_motorcycles", "0"); String[] unlockedArray = unlockedString.split(","); for (String unlocked : unlockedArray) { if (unlocked.equals(String.valueOf(motorcycleType))) { return true; } } return false; } public void draw(Canvas canvas) { canvas.save(); float width = getMotorcycleWidth(); float height = getMotorcycleHeight(); float centerX = x + width/2; float centerY = y + height/2; canvas.rotate(rotation, centerX, centerY); if (motorcycleBitmap != null) { canvas.drawBitmap(motorcycleBitmap, x, y, null); } canvas.restore(); } public Rect getRect() { return motorcycleRect; } public float getX() { return x; } public float getY() { return y; } public boolean isOnGround() { return isOnGround; } public float getSpeed() { return Math.abs(physicsBody.getLinearVelocity().x); } public boolean isOnRoof() { return isUpsideDown() && isOnGround(); } public boolean isUpsideDown() { float normalizedRotation = rotation % 360; if (normalizedRotation > 180) { normalizedRotation -= 360; } else if (normalizedRotation < -180) { normalizedRotation += 360; } return Math.abs(normalizedRotation) > 120f; } public float getRotation() { return rotation; } }