feature(foxy) По нажатию клавиши, изменяется состояние бег-простой
This commit is contained in:
BIN
resource/java/ru/dmitriymx/game/foxy4.png
Normal file
BIN
resource/java/ru/dmitriymx/game/foxy4.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 3.0 KiB |
@@ -3,56 +3,72 @@ package ru.dmitriymx.game;
|
|||||||
import org.lwjgl.opengl.GL11;
|
import org.lwjgl.opengl.GL11;
|
||||||
|
|
||||||
public class Foxy {
|
public class Foxy {
|
||||||
private Sprite texture;
|
private Sprite tex_idle, tex_run;
|
||||||
|
private Sprite current_texture;
|
||||||
private int x, y;
|
private int x, y;
|
||||||
private boolean inverse = false;
|
private boolean inverse = false;
|
||||||
|
|
||||||
public Foxy(){
|
public Foxy(){
|
||||||
texture = new Sprite(Foxy.class.getResourceAsStream("/ru/dmitriymx/game/foxy2.png"), 38, 33);
|
tex_idle = new Sprite(Foxy.class.getResourceAsStream("/ru/dmitriymx/game/foxy2.png"), 38, 33);
|
||||||
|
tex_run = new Sprite(Foxy.class.getResourceAsStream("/ru/dmitriymx/game/foxy4.png"), 44, 32);
|
||||||
x = y = 0;
|
x = y = 0;
|
||||||
|
setState(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setInverse(boolean value){
|
public void setInverse(boolean value){
|
||||||
inverse = value;
|
inverse = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Состояние персонажа.<br/>
|
||||||
|
* 0 - idle - стоит на месте
|
||||||
|
* 1 - run - бежит
|
||||||
|
*/
|
||||||
|
public void setState(int value){
|
||||||
|
if(value == 0){
|
||||||
|
current_texture = tex_idle;
|
||||||
|
} else if(value == 1){
|
||||||
|
current_texture = tex_run;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void render(){
|
public void render(){
|
||||||
Sprite.Coords frame = texture.getFrame();
|
Sprite.Coords frame = current_texture.getFrame();
|
||||||
final int K = 2;
|
final int K = 2;
|
||||||
|
|
||||||
GL11.glColor3f(1f, 1f, 1f);
|
GL11.glColor3f(1f, 1f, 1f);
|
||||||
texture.bind();
|
current_texture.bind();
|
||||||
|
|
||||||
GL11.glBegin(GL11.GL_QUADS);
|
GL11.glBegin(GL11.GL_QUADS);
|
||||||
|
|
||||||
if(inverse){
|
if(inverse){
|
||||||
GL11.glTexCoord2f(texture.floatX(frame.x2),texture.floatY(frame.y1));
|
GL11.glTexCoord2f(current_texture.floatX(frame.x2), current_texture.floatY(frame.y1));
|
||||||
GL11.glVertex2f(0, 0);
|
GL11.glVertex2f(0, 0);
|
||||||
|
|
||||||
GL11.glTexCoord2f(texture.floatX(frame.x1),texture.floatY(frame.y1));
|
GL11.glTexCoord2f(current_texture.floatX(frame.x1), current_texture.floatY(frame.y1));
|
||||||
GL11.glVertex2f(texture.getWidthSprite()*K, 0);
|
GL11.glVertex2f(current_texture.getWidthSprite()*K, 0);
|
||||||
|
|
||||||
GL11.glTexCoord2f(texture.floatX(frame.x1),texture.floatY(frame.y2));
|
GL11.glTexCoord2f(current_texture.floatX(frame.x1), current_texture.floatY(frame.y2));
|
||||||
GL11.glVertex2f(texture.getWidthSprite()*K, texture.getHeightSprite()*K);
|
GL11.glVertex2f(current_texture.getWidthSprite()*K, current_texture.getHeightSprite()*K);
|
||||||
|
|
||||||
GL11.glTexCoord2f(texture.floatX(frame.x2),texture.floatY(frame.y2));
|
GL11.glTexCoord2f(current_texture.floatX(frame.x2), current_texture.floatY(frame.y2));
|
||||||
GL11.glVertex2f(0, texture.getHeightSprite()*K);
|
GL11.glVertex2f(0, current_texture.getHeightSprite()*K);
|
||||||
} else {
|
} else {
|
||||||
GL11.glTexCoord2f(texture.floatX(frame.x1),texture.floatY(frame.y1));
|
GL11.glTexCoord2f(current_texture.floatX(frame.x1), current_texture.floatY(frame.y1));
|
||||||
GL11.glVertex2f(0, 0);
|
GL11.glVertex2f(0, 0);
|
||||||
|
|
||||||
GL11.glTexCoord2f(texture.floatX(frame.x2),texture.floatY(frame.y1));
|
GL11.glTexCoord2f(current_texture.floatX(frame.x2), current_texture.floatY(frame.y1));
|
||||||
GL11.glVertex2f(texture.getWidthSprite()*K, 0);
|
GL11.glVertex2f(current_texture.getWidthSprite()*K, 0);
|
||||||
|
|
||||||
GL11.glTexCoord2f(texture.floatX(frame.x2),texture.floatY(frame.y2));
|
GL11.glTexCoord2f(current_texture.floatX(frame.x2), current_texture.floatY(frame.y2));
|
||||||
GL11.glVertex2f(texture.getWidthSprite()*K, texture.getHeightSprite()*K);
|
GL11.glVertex2f(current_texture.getWidthSprite()*K, current_texture.getHeightSprite()*K);
|
||||||
|
|
||||||
GL11.glTexCoord2f(texture.floatX(frame.x1),texture.floatY(frame.y2));
|
GL11.glTexCoord2f(current_texture.floatX(frame.x1), current_texture.floatY(frame.y2));
|
||||||
GL11.glVertex2f(0, texture.getHeightSprite()*K);
|
GL11.glVertex2f(0, current_texture.getHeightSprite()*K);
|
||||||
}
|
}
|
||||||
|
|
||||||
GL11.glEnd();
|
GL11.glEnd();
|
||||||
|
|
||||||
texture.nextFrame();
|
current_texture.nextFrame();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -37,8 +37,18 @@ public class Main {
|
|||||||
while(Keyboard.next()){
|
while(Keyboard.next()){
|
||||||
if(Keyboard.getEventKey() == Keyboard.KEY_LEFT){
|
if(Keyboard.getEventKey() == Keyboard.KEY_LEFT){
|
||||||
foxy.setInverse(true);
|
foxy.setInverse(true);
|
||||||
|
if(Keyboard.getEventKeyState()){
|
||||||
|
foxy.setState(1);
|
||||||
|
} else {
|
||||||
|
foxy.setState(0);
|
||||||
|
}
|
||||||
} else if (Keyboard.getEventKey() == Keyboard.KEY_RIGHT){
|
} else if (Keyboard.getEventKey() == Keyboard.KEY_RIGHT){
|
||||||
foxy.setInverse(false);
|
foxy.setInverse(false);
|
||||||
|
if(Keyboard.getEventKeyState()){
|
||||||
|
foxy.setState(1);
|
||||||
|
} else {
|
||||||
|
foxy.setState(0);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user