Back to project page SpaceGame.
The source code is released under:
Copyright (c) 2012 ??ukasz Patalas Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the So...
If you think the Android project SpaceGame listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.lpatalas.spacegame; //w w w . ja v a2 s. c o m import com.badlogic.gdx.Gdx; import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.g2d.Sprite; import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.math.MathUtils; import com.badlogic.gdx.math.Rectangle; /** * User: Lukasz * Date: 09.08.12 */ class Asteroid { private final float movementSpeed; private final float rotationSpeed; private final Sprite sprite; private final float radius; public Asteroid(Texture texture) { this.movementSpeed = MathUtils.random(50.0f, 250.0f); this.radius = calculateRadius(texture); this.rotationSpeed = getRandomRotationSpeed(); this.sprite = createSprite(texture); randomizePosition(); } private float calculateRadius(Texture texture) { return Math.max(texture.getWidth(), texture.getHeight()); } private float getRandomRotationSpeed() { boolean clockwiseDirection = MathUtils.randomBoolean(); float speed = MathUtils.random(50.0f, 200.0f); if (clockwiseDirection) speed = -speed; return speed; } private Sprite createSprite(Texture texture) { Sprite sprite = new Sprite(texture); sprite.setOrigin(sprite.getWidth() / 2, sprite.getHeight() / 2); return sprite; } public boolean collidesWith(Rectangle bounds) { float x = sprite.getX() + sprite.getOriginX(); float y = sprite.getY() + sprite.getOriginY(); return bounds.contains(x, y); } public void update(float dt) { updateRotation(dt); updatePosition(dt); } private void updatePosition(float dt) { if (isOutOfScreen()) randomizePosition(); else { move(dt); } } private boolean isOutOfScreen() { return sprite.getX() < -radius; } private void randomizePosition() { float x = Gdx.graphics.getWidth() + MathUtils.random(Gdx.graphics.getWidth()); float y = MathUtils.random(Gdx.graphics.getHeight()); sprite.setPosition(x, y); } private void move(float dt) { float x = sprite.getX(); x -= movementSpeed * dt; sprite.setX(x); } private void updateRotation(float dt) { float rotation = sprite.getRotation(); rotation += rotationSpeed * dt; sprite.setRotation(rotation); } public void render(SpriteBatch spriteBatch) { sprite.draw(spriteBatch); } }