Initial commit
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'
|
||||
eclipse.project.name = appName + '-core'
|
||||
|
||||
dependencies {
|
||||
api "com.badlogicgames.gdx:gdx-box2d:$gdxVersion"
|
||||
api "com.badlogicgames.gdx:gdx-freetype:$gdxVersion"
|
||||
api "com.badlogicgames.gdx:gdx:$gdxVersion"
|
||||
|
||||
if(enableGraalNative == 'true') {
|
||||
implementation "io.github.berstanio:gdx-svmhelper-annotations:$graalHelperVersion"
|
||||
}
|
||||
}
|
||||
|
||||
jar {
|
||||
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package ru.samsung.snowman;
|
||||
|
||||
import static ru.samsung.snowman.Main.SCR_WIDTH;
|
||||
|
||||
public class Background extends Unit {
|
||||
public Background(float x, float y, float vx, float height) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
width = SCR_WIDTH+5;
|
||||
this.height = height;
|
||||
this.vx = vx;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void move() {
|
||||
super.move();
|
||||
outOfScreen();
|
||||
}
|
||||
|
||||
void outOfScreen(){
|
||||
if(x<-SCR_WIDTH){
|
||||
x = SCR_WIDTH;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package ru.samsung.snowman;
|
||||
|
||||
import com.badlogic.gdx.math.MathUtils;
|
||||
|
||||
public class Barrier extends Unit {
|
||||
Barrier[] barriers;
|
||||
|
||||
public Barrier(float x, Barrier[] barriers){
|
||||
this.x = x;
|
||||
this.barriers = barriers;
|
||||
y = 290;
|
||||
width = 150;
|
||||
height = 150;
|
||||
radius = 65;
|
||||
padX = -75;
|
||||
padY = -75;
|
||||
vx = -5;
|
||||
shape = Shape.CIRCLE;
|
||||
setShape();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void move() {
|
||||
super.move();
|
||||
outOfScreen();
|
||||
}
|
||||
|
||||
float getLastX(){
|
||||
float lastX = 0;
|
||||
for (Barrier b: barriers) {
|
||||
if(lastX < b.x){
|
||||
lastX = b.x;
|
||||
}
|
||||
}
|
||||
return lastX;
|
||||
}
|
||||
|
||||
void outOfScreen(){
|
||||
if(x<-width){
|
||||
x = getLastX() + MathUtils.random(400, 1500);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package ru.samsung.snowman;
|
||||
|
||||
public enum Do {
|
||||
GO, JUMP_UP, JUMP_DOWN, DEATH;
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package ru.samsung.snowman;
|
||||
|
||||
import com.badlogic.gdx.graphics.g2d.TextureRegion;
|
||||
import com.badlogic.gdx.math.MathUtils;
|
||||
import com.badlogic.gdx.physics.box2d.Body;
|
||||
import com.badlogic.gdx.physics.box2d.BodyDef;
|
||||
import com.badlogic.gdx.physics.box2d.CircleShape;
|
||||
import com.badlogic.gdx.physics.box2d.Fixture;
|
||||
import com.badlogic.gdx.physics.box2d.FixtureDef;
|
||||
import com.badlogic.gdx.physics.box2d.World;
|
||||
|
||||
public class Dynamic {
|
||||
Body body;
|
||||
TextureRegion img;
|
||||
float radius;
|
||||
|
||||
public Dynamic(World world, float x, float y, float radius, TextureRegion img) {
|
||||
this.img = img;
|
||||
this.radius = radius;
|
||||
BodyDef bodyDef = new BodyDef();
|
||||
bodyDef.type = BodyDef.BodyType.DynamicBody;
|
||||
bodyDef.position.set(x, y);
|
||||
|
||||
body = world.createBody(bodyDef);
|
||||
|
||||
CircleShape shape = new CircleShape();
|
||||
shape.setRadius(radius);
|
||||
FixtureDef fixtureDef = new FixtureDef();
|
||||
fixtureDef.shape = shape;
|
||||
fixtureDef.density = 0.8f;
|
||||
fixtureDef.friction = 0.4f;
|
||||
fixtureDef.restitution = 0.5f;
|
||||
//fixtureDef.filter.groupIndex = -1;
|
||||
Fixture fixture = body.createFixture(fixtureDef);
|
||||
shape.dispose();
|
||||
|
||||
body.applyAngularImpulse(MathUtils.random(-0.01f*radius, 0.01f*radius), true);
|
||||
body.applyLinearImpulse(MathUtils.random(-5f*radius, 5f*radius), MathUtils.random(-5f*radius, 5f*radius), body.getPosition().x, body.getPosition().y, true);
|
||||
}
|
||||
float getX(){
|
||||
return (body.getPosition().x-radius)*100;
|
||||
}
|
||||
float getY(){
|
||||
return (body.getPosition().y-radius)*100;
|
||||
}
|
||||
public float getRadius() {
|
||||
return radius*100;
|
||||
}
|
||||
float getRotation(){
|
||||
return body.getAngle()*MathUtils.radiansToDegrees;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package ru.samsung.snowman;
|
||||
|
||||
public enum GameCondition {
|
||||
GO, END_ROUND, GAME_OVER;
|
||||
}
|
||||
@@ -0,0 +1,271 @@
|
||||
package ru.samsung.snowman;
|
||||
|
||||
import com.badlogic.gdx.graphics.Texture;
|
||||
import com.badlogic.gdx.graphics.g2d.BitmapFont;
|
||||
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
|
||||
import com.badlogic.gdx.graphics.g2d.TextureRegion;
|
||||
import com.badlogic.gdx.math.Vector3;
|
||||
import com.badlogic.gdx.utils.Align;
|
||||
import com.badlogic.gdx.utils.Array;
|
||||
import com.badlogic.gdx.utils.TimeUtils;
|
||||
|
||||
/**
|
||||
* 1. копируем класс InputKeyboard.java в пакет приложения,
|
||||
* копируем в assets атлас изображений кнопок keys.png
|
||||
* 2. в поле вызывающего класса создаём ссылку
|
||||
* InputKeyboard keyboard;
|
||||
* 3. в методе create или в конструкторе создаём объект
|
||||
* keyboard = new InputKeyboard(font, SCR_WIDTH, SCR_HEIGHT, 12);
|
||||
* 4. когда требуется включить клавиатуру, вызываем
|
||||
* keyboard.start();
|
||||
* клавиатура работает до нажатия Enter, после нажатия выключится сама
|
||||
* 5. в методе render передаём в клавиатуру координаты касания и если touch вернул true,
|
||||
* то завершаем ввод и передаём введённый текст в переменную name, а клавиатура исчезает
|
||||
* if (keyboard.touch(touch.x, touch.y)) name = keyboard.getText();
|
||||
* или
|
||||
* if (keyboard.touch(touch)) name = keyboard.getText();
|
||||
* Чтобы кроме клавиатуры не обрабатывались прочие касания, используем флаг isKeyboardShow:
|
||||
* if(keyboard.isKeyboardShow){
|
||||
* if (keyboard.touch(touch.x, touch.y)) name = keyboard.getText();
|
||||
* } else {
|
||||
* // все прочие касания
|
||||
* }
|
||||
* 6. в batch рисуем клавиатуру, она будет рисоваться только после вызова keyboard.start()
|
||||
* keyboard.draw(batch);
|
||||
* 7. в методе dispose удаляем объект
|
||||
* keyboard.dispose();
|
||||
*/
|
||||
public class InputKeyboard {
|
||||
String keysFileName = "keys.png";
|
||||
private final BitmapFont font;
|
||||
|
||||
private final float x, y; // координаты
|
||||
private final float keyboardWidth, keyboardHeight; // ширина и высота всей клавиатуры
|
||||
private final float keyWidth, keyHeight; // ширина и высота каждой кнопки
|
||||
private final float padding = 0; // расстояние между кнопками
|
||||
private final int enterTextLength; // длина вводимого текста
|
||||
|
||||
boolean isKeyboardShow;
|
||||
private boolean endOfEdit;
|
||||
|
||||
private String text = ""; // вводимый текст
|
||||
// текст на кнопках
|
||||
private static final String LETTERS_EN_CAPS = "1234567890-~QWERTYUIOP+?^ASDFGHJKL;'`ZXCVBNM<> |"; // английский без shift
|
||||
private static final String LETTERS_EN_LOW = "!@#$%:&*()_~qwertyuiop[]^asdfghjkl:'`zxcvbnm,. |"; // английский c shift
|
||||
private static final String LETTERS_RU_CAPS = "1234567890-~ЙЦУКЕНГШЩЗХЪ^ФЫВАПРОЛДЖЭ`ЯЧСМИТЬБЮЁ|"; // русский без shift
|
||||
private static final String LETTERS_RU_LOW = "!@#$%:&*()_~йцукенгшщзхъ^фывапролджэ`ячсмитьбюё|"; // русский с shift
|
||||
private String letters = LETTERS_EN_CAPS;
|
||||
|
||||
private final Texture imgAtlasKeys; // все изображения кнопок
|
||||
private final TextureRegion imgEditText; // поле ввода
|
||||
private final TextureRegion imgKeyUP, imgKeyDown; // кнопка выпуклая/вдавленная
|
||||
private final TextureRegion imgKeyBS, imgKeyEnter, imgKeyCL, imgKeySW; // картинки управляющих кноп
|
||||
|
||||
private long timeStartPressKey, timeDurationPressKey = 150; // длительность надавливания кнопки
|
||||
private int keyPressed = -1; // код нажатой кнопки
|
||||
private final Array<Key> keys = new Array<>(); // список всех кноп
|
||||
|
||||
public InputKeyboard(BitmapFont font, float scrWidth, float scrHeight, int enterTextLength){
|
||||
this.font = font;
|
||||
this.enterTextLength = enterTextLength; // количество вводимых символов
|
||||
|
||||
imgAtlasKeys = new Texture(keysFileName);
|
||||
imgKeyUP = new TextureRegion(imgAtlasKeys, 0, 0, 256, 256);
|
||||
imgKeyDown = new TextureRegion(imgAtlasKeys, 256, 0, 256, 256);
|
||||
imgEditText = new TextureRegion(imgAtlasKeys, 256*2, 0, 256, 256);
|
||||
imgKeyBS = new TextureRegion(imgAtlasKeys, 256*3, 0, 256, 256);
|
||||
imgKeyEnter = new TextureRegion(imgAtlasKeys, 256*4, 0, 256, 256);
|
||||
imgKeyCL = new TextureRegion(imgAtlasKeys, 256*5, 0, 256, 256);
|
||||
imgKeySW = new TextureRegion(imgAtlasKeys, 256*6, 0, 256, 256);
|
||||
|
||||
// задаём параметры клавиатуры
|
||||
keyboardWidth = scrWidth/21f*20;
|
||||
keyboardHeight = scrHeight/5f*3;
|
||||
x = (scrWidth- keyboardWidth)/2;
|
||||
y = keyboardHeight +scrHeight/30f;
|
||||
keyWidth = keyboardWidth/13;
|
||||
keyHeight = keyboardHeight/5;
|
||||
createKBD();
|
||||
}
|
||||
|
||||
// создание кнопок клавиатуры по рядам
|
||||
private void createKBD(){
|
||||
int j = 0;
|
||||
for (int i = 0; i < 12; i++, j++)
|
||||
keys.add(new Key(i*keyWidth+x+keyWidth/2, y-keyHeight*2, keyWidth-padding, keyHeight-padding, letters.charAt(j)));
|
||||
|
||||
for (int i = 0; i < 13; i++, j++)
|
||||
keys.add(new Key(i*keyWidth+x, y-keyHeight*3, keyWidth-padding, keyHeight-padding, letters.charAt(j)));
|
||||
|
||||
for (int i = 0; i < 12; i++, j++)
|
||||
keys.add(new Key(i*keyWidth+x+keyWidth/2, y-keyHeight*4, keyWidth-padding, keyHeight-padding, letters.charAt(j)));
|
||||
|
||||
for (int i = 0; i < 11; i++, j++)
|
||||
keys.add(new Key(i*keyWidth+x+keyWidth, y-keyHeight*5, keyWidth-padding, keyHeight-padding, letters.charAt(j)));
|
||||
}
|
||||
|
||||
// задаём/меняем раскладку символов на всех кнопках
|
||||
private void setCharsKBD() {
|
||||
int j = 0;
|
||||
for (int i = 0; i < 12; i++, j++)
|
||||
keys.get(j).letter = letters.charAt(j);
|
||||
|
||||
for (int i = 0; i < 13; i++, j++)
|
||||
keys.get(j).letter = letters.charAt(j);
|
||||
|
||||
for (int i = 0; i < 12; i++, j++)
|
||||
keys.get(j).letter = letters.charAt(j);
|
||||
|
||||
for (int i = 0; i < 11; i++, j++)
|
||||
keys.get(j).letter = letters.charAt(j);
|
||||
}
|
||||
|
||||
// рисуем клавиатуру и вводимый текст
|
||||
public void draw(SpriteBatch batch){
|
||||
if(isKeyboardShow) {
|
||||
// рисуем кнопки
|
||||
for (int i = 0; i < keys.size; i++) {
|
||||
drawImgKey(batch, i, keys.get(i).x, keys.get(i).y, keys.get(i).width, keys.get(i).height);
|
||||
}
|
||||
// рисуем вводимый текст
|
||||
batch.draw(imgEditText, 2 * keyWidth + x + keyWidth / 2, y - keyHeight, keyboardWidth - 5 * keyWidth - padding, keyHeight);
|
||||
font.draw(batch, text, 2 * keyWidth + x + keyWidth / 2, keys.get(0).letterY + keyHeight, keyboardWidth - 5 * keyWidth - padding, Align.center, false);
|
||||
}
|
||||
}
|
||||
|
||||
// рисуем каждую кнопку
|
||||
private void drawImgKey(SpriteBatch batch, int i, float x, float y, float width, float height){
|
||||
float dx, dy;
|
||||
if(keyPressed == i){ // если нажата, то рисуем нажатую кнопку
|
||||
batch.draw(imgKeyDown, x, y, width, height);
|
||||
dx = 2;
|
||||
dy = -2;
|
||||
if(TimeUtils.millis() - timeStartPressKey > timeDurationPressKey){
|
||||
keyPressed = -1;
|
||||
}
|
||||
} else { // рисуем отжатую кнопку
|
||||
dx = 0;
|
||||
dy = 0;
|
||||
batch.draw(imgKeyUP, x, y, width, height);
|
||||
}
|
||||
|
||||
// выводим символы на кнопки
|
||||
switch (letters.charAt(i)) {
|
||||
case '~': batch.draw(imgKeyBS, x+dx, y+dy, width, height); break; // backspace
|
||||
case '^': batch.draw(imgKeyEnter, x+dx, y+dy, width, height); break; // enter
|
||||
case '`': batch.draw(imgKeyCL, x+dx, y+dy, width, height); break; // caps lock
|
||||
case '|': batch.draw(imgKeySW, x+dx, y+dy, width, height); break; // ru/en switcher
|
||||
default: // все прочие символы
|
||||
font.draw(batch, ""+keys.get(i).letter, keys.get(i).letterX+dx, keys.get(i).letterY+dy);
|
||||
}
|
||||
}
|
||||
|
||||
// проверяем, куда нажали
|
||||
public boolean touch(float tx, float ty){
|
||||
if(isKeyboardShow) {
|
||||
for (int i = 0; i < keys.size; i++) {
|
||||
if (!keys.get(i).hit(tx, ty).isEmpty()) {
|
||||
keyPressed = i;
|
||||
setText(i);
|
||||
timeStartPressKey = TimeUtils.millis();
|
||||
}
|
||||
}
|
||||
// окончание редактирования ввода (нажата кнопка enter)
|
||||
if (endOfEdit) {
|
||||
endOfEdit = false;
|
||||
isKeyboardShow = false;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
// проверяем, куда нажали - перегрузка
|
||||
public boolean touch(Vector3 t){
|
||||
if(isKeyboardShow) {
|
||||
for (int i = 0; i < keys.size; i++) {
|
||||
if (!keys.get(i).hit(t.x, t.y).isEmpty()) {
|
||||
keyPressed = i;
|
||||
setText(i);
|
||||
timeStartPressKey = TimeUtils.millis();
|
||||
}
|
||||
}
|
||||
// окончание редактирования ввода (нажата кнопка enter)
|
||||
if (endOfEdit) {
|
||||
endOfEdit = false;
|
||||
isKeyboardShow = false;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// обработка нажатия кнопок
|
||||
private void setText(int i){
|
||||
switch (letters.charAt(i)) {
|
||||
case '~': // backspace
|
||||
if(!text.isEmpty()) text = text.substring(0, text.length() - 1);
|
||||
break;
|
||||
case '^': // enter
|
||||
if(text.isEmpty()) break;
|
||||
endOfEdit = true;
|
||||
break;
|
||||
case '`': // caps lock
|
||||
if(letters.charAt(12) == 'Q') letters = LETTERS_EN_LOW;
|
||||
else if(letters.charAt(12) == 'q') letters = LETTERS_EN_CAPS;
|
||||
else if(letters.charAt(12) == 'Й') letters = LETTERS_RU_LOW;
|
||||
else if(letters.charAt(12) == 'й') letters = LETTERS_RU_CAPS;
|
||||
setCharsKBD();
|
||||
break;
|
||||
case '|': // ru/en switcher
|
||||
if(letters.charAt(12) == 'й') letters = LETTERS_EN_LOW;
|
||||
else if(letters.charAt(12) == 'Й') letters = LETTERS_EN_CAPS;
|
||||
else if(letters.charAt(12) == 'q') letters = LETTERS_RU_LOW;
|
||||
else if(letters.charAt(12) == 'Q') letters = LETTERS_RU_CAPS;
|
||||
setCharsKBD();
|
||||
break;
|
||||
default: // ввод символов
|
||||
if(text.length() < enterTextLength) text += letters.charAt(i);
|
||||
//if(text.length() == 1 && letters.equals(LETTERS_EN_CAPS)) letters = LETTERS_EN_LOW;
|
||||
//if(text.length() == 1 && letters.equals(LETTERS_RU_CAPS)) letters = LETTERS_RU_LOW;
|
||||
setCharsKBD();
|
||||
}
|
||||
}
|
||||
|
||||
// выдача отредактированного текста
|
||||
public String getText() {
|
||||
return text;
|
||||
}
|
||||
|
||||
// класс отдельной кнопки виртуальной клавиатуры
|
||||
private class Key {
|
||||
float x, y;
|
||||
float width, height;
|
||||
char letter; // символ на кнопке
|
||||
float letterX, letterY; // координаты вывода символа
|
||||
|
||||
private Key (float x, float y, float width, float height, char letter) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
this.letter = letter;
|
||||
letterX = x + width/3;
|
||||
letterY = y + height - (height - font.getCapHeight())/2;
|
||||
}
|
||||
|
||||
private String hit(float tx, float ty){
|
||||
if (x<tx && tx<x+width && y<ty && ty<y+height) {
|
||||
return "" + letter;
|
||||
}
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
public void start(){
|
||||
isKeyboardShow = true;
|
||||
}
|
||||
|
||||
public void dispose(){
|
||||
imgAtlasKeys.dispose();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package ru.samsung.snowman;
|
||||
|
||||
import com.badlogic.gdx.math.Vector2;
|
||||
import com.badlogic.gdx.physics.box2d.Body;
|
||||
import com.badlogic.gdx.physics.box2d.BodyDef;
|
||||
import com.badlogic.gdx.physics.box2d.PolygonShape;
|
||||
import com.badlogic.gdx.physics.box2d.World;
|
||||
|
||||
public class Kinematic {
|
||||
Body body;
|
||||
float vx, vy;
|
||||
float rotation;
|
||||
|
||||
public Kinematic(World world, float x, float y, float width, float height, float vx, float vy, float rotation) {
|
||||
this.vx = vx;
|
||||
this.vy = vy;
|
||||
this.rotation = rotation;
|
||||
BodyDef bodyDef = new BodyDef();
|
||||
bodyDef.type = BodyDef.BodyType.KinematicBody;
|
||||
bodyDef.position.set(new Vector2(x, y));
|
||||
|
||||
body = world.createBody(bodyDef);
|
||||
|
||||
PolygonShape shape = new PolygonShape();
|
||||
shape.setAsBox(width/2f, height/2f);
|
||||
body.createFixture(shape, 0.0f);
|
||||
shape.dispose();
|
||||
body.setLinearVelocity(vx, vy);
|
||||
body.setAngularVelocity(rotation);
|
||||
}
|
||||
|
||||
void move() {
|
||||
if(body.getPosition().x < 0 || body.getPosition().x > 16){
|
||||
vx = -vx;
|
||||
body.setLinearVelocity(vx, vy);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package ru.samsung.snowman;
|
||||
|
||||
import com.badlogic.gdx.Game;
|
||||
import com.badlogic.gdx.graphics.OrthographicCamera;
|
||||
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
|
||||
import com.badlogic.gdx.math.Vector2;
|
||||
import com.badlogic.gdx.math.Vector3;
|
||||
import com.badlogic.gdx.physics.box2d.World;
|
||||
|
||||
/** {@link com.badlogic.gdx.ApplicationListener} implementation shared by all platforms. */
|
||||
public class Main extends Game {
|
||||
public static final float SCR_WIDTH = 1600, SCR_HEIGHT = 900;
|
||||
|
||||
public SpriteBatch batch;
|
||||
public OrthographicCamera camera;
|
||||
public Vector3 touch;
|
||||
public Resources r;
|
||||
public World world;
|
||||
|
||||
ScreenGame screenGame;
|
||||
|
||||
@Override
|
||||
public void create() {
|
||||
batch = new SpriteBatch();
|
||||
camera = new OrthographicCamera();
|
||||
camera.setToOrtho(false, SCR_WIDTH, SCR_HEIGHT);
|
||||
world = new World(new Vector2(0, -10), true);
|
||||
touch = new Vector3();
|
||||
r = new Resources();
|
||||
|
||||
screenGame = new ScreenGame(this);
|
||||
setScreen(screenGame);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void dispose() {
|
||||
batch.dispose();
|
||||
r.dispose();
|
||||
world.dispose();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package ru.samsung.snowman;
|
||||
|
||||
import com.badlogic.gdx.Gdx;
|
||||
import com.badlogic.gdx.graphics.Texture;
|
||||
import com.badlogic.gdx.graphics.g2d.BitmapFont;
|
||||
import com.badlogic.gdx.graphics.g2d.TextureRegion;
|
||||
|
||||
public class Resources {
|
||||
public Texture imgSnowManAtlas;
|
||||
public TextureRegion[] imgSnowMan = new TextureRegion[15];
|
||||
public TextureRegion imgHead;
|
||||
public TextureRegion imgSmallBall;
|
||||
public Texture imgBall;
|
||||
public Texture imgSky;
|
||||
public Texture imgGround;
|
||||
public Texture imgTrees;
|
||||
public Texture imgClouds;
|
||||
|
||||
public BitmapFont font32;
|
||||
public BitmapFont font64;
|
||||
public BitmapFont font50;
|
||||
|
||||
Resources(){
|
||||
loadImages();
|
||||
loafFonts();
|
||||
}
|
||||
|
||||
void loadImages(){
|
||||
imgSnowManAtlas = new Texture("snowmangoatlas6.png");
|
||||
for (int i = 0; i < imgSnowMan.length; i++) {
|
||||
imgSnowMan[i] = new TextureRegion(imgSnowManAtlas, i*245, 0, 245, 445);
|
||||
}
|
||||
imgHead = new TextureRegion(imgSnowManAtlas, 3706, 45, 210, 210);
|
||||
imgSmallBall = new TextureRegion(imgSnowManAtlas, 3743, 273, 135, 135);
|
||||
imgBall = new Texture("ball2.png");
|
||||
imgSky = new Texture("sky2.png");
|
||||
imgGround = new Texture("ground2.png");
|
||||
imgTrees = new Texture("trees3.png");
|
||||
imgClouds = new Texture("clouds4.png");
|
||||
}
|
||||
|
||||
void loafFonts(){
|
||||
font32 = new BitmapFont(Gdx.files.internal("font/centurygothic32.fnt"));
|
||||
font64 = new BitmapFont(Gdx.files.internal("font/centurygothic64.fnt"));
|
||||
font50 = new BitmapFont(Gdx.files.internal("font/comic50.fnt"));
|
||||
}
|
||||
|
||||
void dispose(){
|
||||
imgSnowManAtlas.dispose();
|
||||
imgBall.dispose();
|
||||
imgSky.dispose();
|
||||
imgGround.dispose();
|
||||
imgTrees.dispose();
|
||||
imgClouds.dispose();
|
||||
font32.dispose();
|
||||
font64.dispose();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,228 @@
|
||||
package ru.samsung.snowman;
|
||||
|
||||
import static ru.samsung.snowman.Main.SCR_HEIGHT;
|
||||
import static ru.samsung.snowman.Main.SCR_WIDTH;
|
||||
|
||||
import com.badlogic.gdx.Gdx;
|
||||
import com.badlogic.gdx.Screen;
|
||||
import com.badlogic.gdx.graphics.OrthographicCamera;
|
||||
import com.badlogic.gdx.graphics.Texture;
|
||||
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
|
||||
import com.badlogic.gdx.graphics.g2d.TextureRegion;
|
||||
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
|
||||
import com.badlogic.gdx.math.Intersector;
|
||||
import com.badlogic.gdx.math.MathUtils;
|
||||
import com.badlogic.gdx.math.Vector3;
|
||||
import com.badlogic.gdx.physics.box2d.Body;
|
||||
import com.badlogic.gdx.physics.box2d.Box2DDebugRenderer;
|
||||
import com.badlogic.gdx.physics.box2d.World;
|
||||
import com.badlogic.gdx.utils.Align;
|
||||
import com.badlogic.gdx.utils.Array;
|
||||
import com.badlogic.gdx.utils.TimeUtils;
|
||||
|
||||
public class ScreenGame implements Screen {
|
||||
//Main main;
|
||||
private SpriteBatch batch;
|
||||
private OrthographicCamera camera;
|
||||
private OrthographicCamera cameraBox2D;
|
||||
private Vector3 touch;
|
||||
private ShapeRenderer shapeRenderer;
|
||||
private Box2DDebugRenderer debugRenderer;
|
||||
private World world;
|
||||
InputKeyboard keyboard;
|
||||
|
||||
private Resources r;
|
||||
private SnowMan snowMan;
|
||||
private Barrier[] barriers = new Barrier[10];
|
||||
private Background[] grounds = new Background[2];
|
||||
private Background[] trees = new Background[2];
|
||||
private Background[] clouds = new Background[2];
|
||||
private Kinematic[] groundsKinematic = new Kinematic[2];
|
||||
private Array<Dynamic> partsSnowMan = new Array<>();
|
||||
|
||||
int lives = 5;
|
||||
GameCondition gameCondition;
|
||||
long timeStartGame;
|
||||
String strTime;
|
||||
|
||||
public ScreenGame(Main main) {
|
||||
//this.main = main;
|
||||
batch = main.batch;
|
||||
camera = main.camera;
|
||||
touch = main.touch;
|
||||
world = main.world;
|
||||
r = main.r;
|
||||
cameraBox2D = new OrthographicCamera();
|
||||
cameraBox2D.setToOrtho(false, SCR_WIDTH/100, SCR_HEIGHT/100);
|
||||
|
||||
shapeRenderer = new ShapeRenderer();
|
||||
debugRenderer = new Box2DDebugRenderer();
|
||||
|
||||
clouds[0] = new Background(0, 500, -0.2f, 400);
|
||||
clouds[1] = new Background(SCR_WIDTH, 500, -0.2f, 400);
|
||||
trees[0] = new Background(0, 270, -1, 400);
|
||||
trees[1] = new Background(SCR_WIDTH, 270, -1, 400);
|
||||
grounds[0] = new Background(0, 0, -5, 300);
|
||||
grounds[1] = new Background(SCR_WIDTH, 0, -5, 300);
|
||||
groundsKinematic[0] = new Kinematic(world, SCR_WIDTH/2/100f, 180/100f, SCR_WIDTH, 50/100f, 0, 0, 0);
|
||||
//groundsKinematic[1] = new Kinematic(world, SCR_WIDTH, 500, SCR_WIDTH, 100, -1000, 0, 0);
|
||||
|
||||
|
||||
keyboard = new InputKeyboard(r.font32, SCR_WIDTH, SCR_HEIGHT, 12);
|
||||
startGame();
|
||||
}
|
||||
|
||||
private void startGame() {
|
||||
snowMan = new SnowMan();
|
||||
barriers[0] = new Barrier(SCR_WIDTH + MathUtils.random(200, 500), barriers);
|
||||
for (int i = 1; i < barriers.length; i++) {
|
||||
barriers[i] = new Barrier(barriers[i-1].x + MathUtils.random(400, 1500), barriers);
|
||||
}
|
||||
gameCondition = GameCondition.GO;
|
||||
keyboard.start();
|
||||
timeStartGame = TimeUtils.millis();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void show() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void render(float delta) {
|
||||
// касания
|
||||
if(Gdx.input.justTouched()){
|
||||
if(gameCondition == GameCondition.GO) {
|
||||
if (snowMan.state == Do.GO) {
|
||||
snowMan.state = Do.JUMP_UP;
|
||||
}
|
||||
} else if(gameCondition == GameCondition.END_ROUND){
|
||||
startGame();
|
||||
} else if(gameCondition == GameCondition.GAME_OVER){
|
||||
lives = 5;
|
||||
startGame();
|
||||
}
|
||||
}
|
||||
|
||||
// события
|
||||
if(gameCondition == GameCondition.GO) {
|
||||
strTime = getTime();
|
||||
for (Background g : clouds) {
|
||||
g.move();
|
||||
}
|
||||
for (Background g : trees) {
|
||||
g.move();
|
||||
}
|
||||
for (Background g : grounds) {
|
||||
g.move();
|
||||
}
|
||||
snowMan.move();
|
||||
for (Barrier b : barriers) {
|
||||
b.move();
|
||||
if (Intersector.overlaps(snowMan.circle, b.circle)) {
|
||||
gameCondition = GameCondition.END_ROUND;
|
||||
deathSnowMan();
|
||||
if (lives == 0) {
|
||||
gameCondition = GameCondition.GAME_OVER;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// отрисовка
|
||||
batch.setProjectionMatrix(camera.combined);
|
||||
batch.begin();
|
||||
batch.draw(r.imgSky, 0, 0, SCR_WIDTH, SCR_HEIGHT);
|
||||
for(Background b: clouds) {
|
||||
batch.draw(r.imgClouds, b.imgX(), b.imgY(), b.imgWidth(), b.imgHeight());
|
||||
}
|
||||
for(Background b: trees) {
|
||||
batch.draw(r.imgTrees, b.imgX(), b.imgY(), b.imgWidth(), b.imgHeight());
|
||||
}
|
||||
for(Background b: grounds) {
|
||||
batch.draw(r.imgGround, b.imgX(), b.imgY(), b.imgWidth(), b.imgHeight());
|
||||
}
|
||||
for (Barrier b: barriers) {
|
||||
batch.draw(r.imgBall, b.imgX(), b.imgY(), b.imgWidth(), b.imgHeight());
|
||||
}
|
||||
for (int i = 0; i < lives; i++) {
|
||||
batch.draw(r.imgSnowMan[14], SCR_WIDTH - 40 * i - 40, SCR_HEIGHT - 70, 30, 60);
|
||||
}
|
||||
if(gameCondition == GameCondition.GO) {
|
||||
batch.draw(r.imgSnowMan[snowMan.phase], snowMan.imgX(), snowMan.imgY(), snowMan.imgWidth(), snowMan.imgHeight());
|
||||
}
|
||||
for (Dynamic d: partsSnowMan) {
|
||||
batch.draw(d.img, d.getX(), d.getY(), d.getRadius(), d.getRadius(), d.getRadius()*2, d.getRadius()*2, 1, 1, d.getRotation());
|
||||
}
|
||||
r.font50.draw(batch, strTime, 10, 890);
|
||||
if(gameCondition == GameCondition.GAME_OVER) {
|
||||
r.font64.draw(batch, "GAME OVER", 0, SCR_HEIGHT/2+100, SCR_WIDTH, Align.center, true);
|
||||
}
|
||||
batch.end();
|
||||
/*
|
||||
shapeRenderer.setProjectionMatrix(camera.combined);
|
||||
shapeRenderer.begin(ShapeRenderer.ShapeType.Line);
|
||||
shapeRenderer.setColor(Color.RED);
|
||||
shapeRenderer.circle(snowMan.x, snowMan.y, snowMan.radius);
|
||||
for (Barrier b: barriers) {
|
||||
shapeRenderer.circle(b.x, b.y, b.radius);
|
||||
}
|
||||
shapeRenderer.end();*/
|
||||
//debugRenderer.render(world, cameraBox2D.combined);
|
||||
world.step(1/60f, 6, 2);
|
||||
|
||||
if(gameCondition == GameCondition.GO && partsSnowMan.notEmpty()) {
|
||||
for(Dynamic d: partsSnowMan){
|
||||
if (d.body != null) {
|
||||
world.destroyBody(d.body);
|
||||
}
|
||||
}
|
||||
partsSnowMan.clear();
|
||||
}
|
||||
}
|
||||
|
||||
void deathSnowMan(){
|
||||
lives--;
|
||||
partsSnowMan.add(new Dynamic(world, snowMan.x/100f, snowMan.y/100f, 70/100f, r.imgSmallBall));
|
||||
partsSnowMan.add(new Dynamic(world, snowMan.x/100f, (snowMan.y+60)/100f, 40/100f, r.imgSmallBall));
|
||||
partsSnowMan.add(new Dynamic(world, snowMan.x/100f, (snowMan.y+100)/100f, 70/100f, r.imgHead));
|
||||
partsSnowMan.add(new Dynamic(world, (snowMan.x-60)/100f, (snowMan.y-60)/100f, 27/100f, r.imgSmallBall));
|
||||
partsSnowMan.add(new Dynamic(world, (snowMan.x+60)/100f, (snowMan.y-60)/100f, 27/100f, r.imgSmallBall));
|
||||
partsSnowMan.add(new Dynamic(world, (snowMan.x-50)/100f, (snowMan.y+70)/100f, 20/100f, r.imgSmallBall));
|
||||
partsSnowMan.add(new Dynamic(world, (snowMan.x+50)/100f, (snowMan.y+70)/100f, 20/100f, r.imgSmallBall));
|
||||
}
|
||||
|
||||
String getTime(){
|
||||
long time = TimeUtils.millis() - timeStartGame;
|
||||
long milisec = time%1000/100;
|
||||
long sec = time/1000%60;
|
||||
long min = time/1000/60%60;
|
||||
long hours = time/1000/60/60;
|
||||
return hours+":"+min/10+min%10+":"+sec/10+sec%10+":"+milisec;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void resize(int width, int height) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void pause() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void resume() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void hide() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void dispose() {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package ru.samsung.snowman;
|
||||
|
||||
public enum Shape {
|
||||
CIRCLE, RECTANGLE, POLYGON;
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package ru.samsung.snowman;
|
||||
|
||||
public class SnowMan extends Unit {
|
||||
Do state;
|
||||
|
||||
public SnowMan() {
|
||||
x = 400;
|
||||
y = 300;
|
||||
width = 150;
|
||||
height = 300;
|
||||
radius = 64;
|
||||
padX = -72;
|
||||
padY = -100;
|
||||
state = Do.GO;
|
||||
nPhases = 12;
|
||||
timePhaseInterval = 50;
|
||||
shape = Shape.CIRCLE;
|
||||
setShape();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void move() {
|
||||
super.move();
|
||||
changePhase();
|
||||
jump();
|
||||
}
|
||||
|
||||
private void jump() {
|
||||
switch (state) {
|
||||
case GO:
|
||||
vy = 0;
|
||||
break;
|
||||
case JUMP_UP:
|
||||
phase = 12;
|
||||
vy = 6;
|
||||
if(y > 550){
|
||||
state = Do.JUMP_DOWN;
|
||||
}
|
||||
break;
|
||||
case JUMP_DOWN:
|
||||
phase = 13;
|
||||
vy = -6;
|
||||
if(y < 300){
|
||||
state = Do.GO;
|
||||
y = 300;
|
||||
phase = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package ru.samsung.snowman;
|
||||
|
||||
import com.badlogic.gdx.math.Vector2;
|
||||
import com.badlogic.gdx.physics.box2d.Body;
|
||||
import com.badlogic.gdx.physics.box2d.BodyDef;
|
||||
import com.badlogic.gdx.physics.box2d.PolygonShape;
|
||||
import com.badlogic.gdx.physics.box2d.World;
|
||||
|
||||
public class Static {
|
||||
public Static(World world, float x, float y, float width, float height) {
|
||||
BodyDef bodyDef = new BodyDef();
|
||||
bodyDef.type = BodyDef.BodyType.StaticBody;
|
||||
bodyDef.position.set(new Vector2(x, y));
|
||||
Body body = world.createBody(bodyDef);
|
||||
PolygonShape shape = new PolygonShape();
|
||||
shape.setAsBox(width/2f, height/2f);
|
||||
body.createFixture(shape, 0.0f);
|
||||
shape.dispose();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
package ru.samsung.snowman;
|
||||
|
||||
import com.badlogic.gdx.math.Circle;
|
||||
import com.badlogic.gdx.math.Polygon;
|
||||
import com.badlogic.gdx.math.Rectangle;
|
||||
import com.badlogic.gdx.utils.TimeUtils;
|
||||
|
||||
public class Unit {
|
||||
float x, y;
|
||||
float vx, vy;
|
||||
float width, height;
|
||||
float radius;
|
||||
float padX, padY;
|
||||
Shape shape;
|
||||
int phase, nPhases;
|
||||
|
||||
public Circle circle;
|
||||
public Rectangle rectangle;
|
||||
public Polygon polygon;
|
||||
|
||||
long timeLastPhase, timePhaseInterval;
|
||||
|
||||
public Unit() {
|
||||
}
|
||||
|
||||
public void move() {
|
||||
x += vx;
|
||||
y += vy;
|
||||
if(shape != null) {
|
||||
setPosition(x, y);
|
||||
}
|
||||
}
|
||||
|
||||
public void changePhase(){
|
||||
if(TimeUtils.millis()-timeLastPhase>timePhaseInterval) {
|
||||
timeLastPhase = TimeUtils.millis();
|
||||
phase++;
|
||||
if(phase==nPhases) {
|
||||
phase = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void setShape() {
|
||||
switch (shape) {
|
||||
case CIRCLE:
|
||||
circle = new Circle(x, y, radius);
|
||||
break;
|
||||
case RECTANGLE:
|
||||
rectangle = new Rectangle(x, y, width, height);
|
||||
break;
|
||||
case POLYGON:
|
||||
float[] vertices = new float[]{};
|
||||
polygon = new Polygon(vertices);
|
||||
}
|
||||
}
|
||||
|
||||
public void setPosition(float x, float y) {
|
||||
switch (shape) {
|
||||
case CIRCLE: circle.setPosition(x, y); break;
|
||||
case RECTANGLE: rectangle.setPosition(x, y); break;
|
||||
case POLYGON: polygon.setPosition(x, y); break;
|
||||
}
|
||||
}
|
||||
|
||||
public float imgX() {
|
||||
return x+padX;
|
||||
}
|
||||
|
||||
public float imgY() {
|
||||
return y+padY;
|
||||
}
|
||||
|
||||
public float imgWidth() {
|
||||
return width;
|
||||
}
|
||||
|
||||
public float imgHeight() {
|
||||
return height;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user