feature(prometheus) Новый персонаж
This commit is contained in:
BIN
resource/java/ru/dmitriymx/game/mw-prometheus-01.png
Normal file
BIN
resource/java/ru/dmitriymx/game/mw-prometheus-01.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 2.8 KiB |
BIN
resource/java/ru/dmitriymx/game/mw-prometheus-gun.png
Normal file
BIN
resource/java/ru/dmitriymx/game/mw-prometheus-gun.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 1.2 KiB |
@@ -5,9 +5,11 @@ import org.lwjgl.input.Keyboard;
|
||||
import org.lwjgl.opengl.Display;
|
||||
import org.lwjgl.opengl.DisplayMode;
|
||||
import org.lwjgl.opengl.GL11;
|
||||
import ru.dmitriymx.game.mw.Prometheus;
|
||||
|
||||
public class Main {
|
||||
private Foxy foxy;
|
||||
//private Foxy foxy;
|
||||
private Prometheus mech;
|
||||
|
||||
private void init_display(int width, int height){
|
||||
try {
|
||||
@@ -22,7 +24,7 @@ public class Main {
|
||||
|
||||
private void init_opengl(int width, int height) {
|
||||
GL11.glEnable(GL11.GL_TEXTURE_2D);
|
||||
GL11.glClearColor(0, 0, 0.3f, 0);
|
||||
GL11.glClearColor(0.0f, 0.0f, 0.0f, 0);
|
||||
|
||||
GL11.glEnable(GL11.GL_BLEND);
|
||||
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
|
||||
@@ -36,39 +38,38 @@ public class Main {
|
||||
private void render(){
|
||||
while(Keyboard.next()){
|
||||
if(Keyboard.getEventKey() == Keyboard.KEY_LEFT){
|
||||
foxy.setInverse(true);
|
||||
if(Keyboard.getEventKeyState()){
|
||||
foxy.setState(1);
|
||||
mech.setState(1);
|
||||
mech.setRigthRun(true);
|
||||
} else {
|
||||
foxy.setState(0);
|
||||
mech.setState(0);
|
||||
}
|
||||
} else if (Keyboard.getEventKey() == Keyboard.KEY_RIGHT){
|
||||
foxy.setInverse(false);
|
||||
if(Keyboard.getEventKeyState()){
|
||||
foxy.setState(1);
|
||||
mech.setState(1);
|
||||
mech.setRigthRun(false);
|
||||
} else {
|
||||
foxy.setState(0);
|
||||
mech.setState(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
final int speed = 5 * 4;
|
||||
if(foxy.getState() == 1){
|
||||
if(foxy.getInverse()){
|
||||
foxy.setX(foxy.getX() - speed);
|
||||
if(mech.getState() == 1){
|
||||
if(mech.getRigthRun()){
|
||||
mech.setX(mech.getX() - mech.getSpeed());
|
||||
} else {
|
||||
foxy.setX(foxy.getX() + speed);
|
||||
mech.setX(mech.getX() + mech.getSpeed());
|
||||
}
|
||||
}
|
||||
foxy.render();
|
||||
mech.render();
|
||||
}
|
||||
|
||||
public void start(int width, int height){
|
||||
init_display(width, height);
|
||||
init_opengl(width, height);
|
||||
|
||||
foxy = new Foxy();
|
||||
foxy.setY(Display.getHeight() - (34*4));
|
||||
mech = new Prometheus();
|
||||
mech.setY(Display.getHeight() - (mech.getHeight()*4));
|
||||
|
||||
while(!Display.isCloseRequested()){
|
||||
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
|
||||
|
||||
@@ -5,7 +5,7 @@ import java.io.InputStream;
|
||||
|
||||
public class Sprite extends Texture {
|
||||
public class Coords{
|
||||
int x1,y1,x2,y2;
|
||||
public int x1,y1,x2,y2;
|
||||
Coords(int x1, int y1, int x2, int y2){
|
||||
this.x1 = x1;
|
||||
this.y1 = y1;
|
||||
@@ -59,6 +59,14 @@ public class Sprite extends Texture {
|
||||
}
|
||||
}
|
||||
|
||||
public void prevFrame(){
|
||||
if(current_frame == 0){
|
||||
current_frame = max_frames-1;
|
||||
} else {
|
||||
current_frame--;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Ширина одного спрайта
|
||||
* @return
|
||||
|
||||
317
src/ru/dmitriymx/game/mw/Prometheus.java
Normal file
317
src/ru/dmitriymx/game/mw/Prometheus.java
Normal file
@@ -0,0 +1,317 @@
|
||||
package ru.dmitriymx.game.mw;
|
||||
|
||||
import org.lwjgl.input.Mouse;
|
||||
import org.lwjgl.opengl.Display;
|
||||
import org.lwjgl.opengl.GL11;
|
||||
import ru.dmitriymx.game.Sprite;
|
||||
|
||||
public class Prometheus {
|
||||
private Sprite body, gun;
|
||||
private int x, y;
|
||||
private int width, height;
|
||||
private int state;
|
||||
private boolean inverse = false;
|
||||
private final int K = 4;
|
||||
private final int speed = 2 * K;
|
||||
private boolean rigth = true;
|
||||
|
||||
public Prometheus(){
|
||||
x = y = 0;
|
||||
width = 48;
|
||||
height = 32;
|
||||
body = new Sprite(Prometheus.class.getResourceAsStream("/ru/dmitriymx/game/mw-prometheus-01.png"), width, height);
|
||||
gun = new Sprite(Prometheus.class.getResourceAsStream("/ru/dmitriymx/game/mw-prometheus-gun.png"), 48, 16);
|
||||
setState(0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Разворачивание текстуры в другую сторону
|
||||
* @param value false=вправо, true=влево
|
||||
*/
|
||||
public void setInverse(boolean value){
|
||||
inverse = value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Куда развернута текстура
|
||||
* @return false=вправо, true=влево
|
||||
*/
|
||||
public boolean getInverse(){
|
||||
return inverse;
|
||||
}
|
||||
|
||||
/**
|
||||
* Установка состояние персонажа.<br/>
|
||||
* 0 - idle - стоит на месте<br/>
|
||||
* 1 - run - бежит
|
||||
*/
|
||||
public void setState(int value){
|
||||
state = value;
|
||||
if(state == 0){
|
||||
body.setFrame(0);
|
||||
gun.setFrame(0);
|
||||
} else {
|
||||
body.setFrame(1);
|
||||
gun.setFrame(1);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Ссостояние персонажа.<br/>
|
||||
* 0 - idle - стоит на месте<br/>
|
||||
* 1 - run - бежит
|
||||
*/
|
||||
public int getState(){
|
||||
return state;
|
||||
}
|
||||
|
||||
public void setX(int value){
|
||||
x = value;
|
||||
}
|
||||
|
||||
public int getX(){
|
||||
return x;
|
||||
}
|
||||
|
||||
public void setY(int value){
|
||||
y = value;
|
||||
}
|
||||
|
||||
public int getY(){
|
||||
return y;
|
||||
}
|
||||
|
||||
public void render(){
|
||||
Sprite.Coords bodyFrame = body.getFrame();
|
||||
Sprite.Coords gunFrame = gun.getFrame();
|
||||
|
||||
GL11.glColor3f(1f, 1f, 1f);
|
||||
|
||||
if(!inverse && (get_center_x() > Mouse.getX())){
|
||||
inverse = true;
|
||||
} else if(inverse && (get_center_x() < Mouse.getX())) {
|
||||
inverse = false;
|
||||
}
|
||||
|
||||
render_body(bodyFrame);
|
||||
render_gun(gunFrame);
|
||||
|
||||
/*int _x;
|
||||
int _y;
|
||||
if(!inverse) {
|
||||
_x = (x + (19 * K));
|
||||
_y = (y + (5 * K));
|
||||
} else {
|
||||
_x = (x + (29 * K));
|
||||
_y = (y + (5 * K));
|
||||
}
|
||||
int mX = Mouse.getX();
|
||||
int mY = Display.getHeight() - Mouse.getY();
|
||||
float _a = (float)(Math.atan2(mY - _y, mX - _x) * 180/Math.PI);
|
||||
draw_target(_x, _y, 0);
|
||||
draw_quad(_x,_y,_a);*/
|
||||
|
||||
if(state == 1){
|
||||
if(!inverse){
|
||||
if(!rigth){
|
||||
body.nextFrame();
|
||||
gun.nextFrame();
|
||||
}else{
|
||||
body.prevFrame();
|
||||
gun.prevFrame();
|
||||
}
|
||||
} else {
|
||||
if(rigth){
|
||||
body.nextFrame();
|
||||
gun.nextFrame();
|
||||
}else{
|
||||
body.prevFrame();
|
||||
gun.prevFrame();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void render_body(Sprite.Coords frame){
|
||||
body.bind();
|
||||
|
||||
GL11.glPushMatrix();
|
||||
GL11.glTranslatef(x,y,0);
|
||||
|
||||
GL11.glBegin(GL11.GL_QUADS);
|
||||
if(inverse){
|
||||
GL11.glTexCoord2f(body.floatX(frame.x2), body.floatY(frame.y1));
|
||||
GL11.glVertex2f(0, 0);
|
||||
|
||||
GL11.glTexCoord2f(body.floatX(frame.x1), body.floatY(frame.y1));
|
||||
GL11.glVertex2f(body.getWidthSprite()*K, 0);
|
||||
|
||||
GL11.glTexCoord2f(body.floatX(frame.x1), body.floatY(frame.y2));
|
||||
GL11.glVertex2f(body.getWidthSprite()*K, body.getHeightSprite()*K);
|
||||
|
||||
GL11.glTexCoord2f(body.floatX(frame.x2), body.floatY(frame.y2));
|
||||
GL11.glVertex2f(0, body.getHeightSprite()*K);
|
||||
} else {
|
||||
GL11.glTexCoord2f(body.floatX(frame.x1), body.floatY(frame.y1));
|
||||
GL11.glVertex2f(0, 0);
|
||||
|
||||
GL11.glTexCoord2f(body.floatX(frame.x2), body.floatY(frame.y1));
|
||||
GL11.glVertex2f(body.getWidthSprite()*K, 0);
|
||||
|
||||
GL11.glTexCoord2f(body.floatX(frame.x2), body.floatY(frame.y2));
|
||||
GL11.glVertex2f(body.getWidthSprite()*K, body.getHeightSprite()*K);
|
||||
|
||||
GL11.glTexCoord2f(body.floatX(frame.x1), body.floatY(frame.y2));
|
||||
GL11.glVertex2f(0, body.getHeightSprite()*K);
|
||||
}
|
||||
GL11.glEnd();
|
||||
|
||||
GL11.glPopMatrix();
|
||||
}
|
||||
|
||||
private void render_gun(Sprite.Coords frame){
|
||||
gun.bind();
|
||||
|
||||
GL11.glPushMatrix();
|
||||
GL11.glColor3f(1f, 1f, 1f);
|
||||
|
||||
int _x;
|
||||
int _y;
|
||||
if(!inverse){
|
||||
_x = (x + (19 * K));
|
||||
_y = (y + (5 * K));
|
||||
} else {
|
||||
_x = (x + (29 * K));
|
||||
_y = (y + (5 * K));
|
||||
}
|
||||
int mX = Mouse.getX();
|
||||
int mY = Display.getHeight() - Mouse.getY();
|
||||
int offcetY = (-16 * K);
|
||||
int offcetX = (-10 * K);
|
||||
float angle = (float)(Math.atan2(mY - _y, mX - _x) * 180/Math.PI);
|
||||
|
||||
GL11.glTranslatef(_x, _y, 0);
|
||||
GL11.glRotatef(angle, 0, 0, 1);
|
||||
|
||||
GL11.glBegin(GL11.GL_QUADS);
|
||||
|
||||
if(!inverse){
|
||||
GL11.glTexCoord2f(gun.floatX(frame.x1), gun.floatY(frame.y1));
|
||||
GL11.glVertex2f(offcetX, offcetY);
|
||||
|
||||
GL11.glTexCoord2f(gun.floatX(frame.x2), gun.floatY(frame.y1));
|
||||
GL11.glVertex2f(offcetX + gun.getWidthSprite()*K, offcetY);
|
||||
|
||||
GL11.glTexCoord2f(gun.floatX(frame.x2), gun.floatY(frame.y2));
|
||||
GL11.glVertex2f(offcetX + gun.getWidthSprite()*K, offcetY + gun.getHeightSprite()*K);
|
||||
|
||||
GL11.glTexCoord2f(gun.floatX(frame.x1), gun.floatY(frame.y2));
|
||||
GL11.glVertex2f(offcetX, offcetY + gun.getHeightSprite()*K);
|
||||
} else {
|
||||
GL11.glTexCoord2f(gun.floatX(frame.x1), gun.floatY(frame.y2));
|
||||
GL11.glVertex2f(offcetX, -(offcetY + gun.getHeightSprite()*K));
|
||||
|
||||
GL11.glTexCoord2f(gun.floatX(frame.x2), gun.floatY(frame.y2));
|
||||
GL11.glVertex2f(offcetX + gun.getWidthSprite()*K, -(offcetY + gun.getHeightSprite()*K));
|
||||
|
||||
GL11.glTexCoord2f(gun.floatX(frame.x2), gun.floatY(frame.y1));
|
||||
GL11.glVertex2f(offcetX + gun.getWidthSprite()*K, -offcetY);
|
||||
|
||||
GL11.glTexCoord2f(gun.floatX(frame.x1), gun.floatY(frame.y1));
|
||||
GL11.glVertex2f(offcetX, -offcetY);
|
||||
}
|
||||
GL11.glEnd();
|
||||
|
||||
GL11.glPopMatrix();
|
||||
}
|
||||
|
||||
private void draw_target(int x, int y, float angle){
|
||||
final int size = 100;
|
||||
GL11.glDisable(GL11.GL_TEXTURE_2D);
|
||||
GL11.glPushMatrix();
|
||||
GL11.glTranslatef(x,y,0);
|
||||
GL11.glRotatef(angle, 0, 0, 1);
|
||||
|
||||
GL11.glBegin(GL11.GL_LINES);
|
||||
GL11.glColor3f(1,0,0);
|
||||
GL11.glVertex2f(0,0);
|
||||
GL11.glVertex2f(size,0);
|
||||
GL11.glColor3f(0,1,0);
|
||||
GL11.glVertex2f(0,0);
|
||||
GL11.glVertex2f(0,size);
|
||||
GL11.glColor3f(0,0,1);
|
||||
GL11.glVertex2f(0,0);
|
||||
GL11.glVertex2f(-size,0);
|
||||
GL11.glColor3f(1,1,1);
|
||||
GL11.glVertex2f(0,0);
|
||||
GL11.glVertex2f(0,-size);
|
||||
GL11.glEnd();
|
||||
|
||||
GL11.glPopMatrix();
|
||||
GL11.glEnable(GL11.GL_TEXTURE_2D);
|
||||
}
|
||||
|
||||
private void draw_quad(int x, int y, float angle){
|
||||
GL11.glDisable(GL11.GL_TEXTURE_2D);
|
||||
GL11.glPushMatrix();
|
||||
|
||||
GL11.glTranslatef(x,y,0);
|
||||
GL11.glRotatef(angle, 0, 0, 1);
|
||||
|
||||
GL11.glColor4f(1, 1, 1, 0.5f);
|
||||
GL11.glBegin(GL11.GL_QUADS);
|
||||
if(!inverse){
|
||||
GL11.glVertex2f(0,0);
|
||||
GL11.glVertex2f(100,0);
|
||||
GL11.glVertex2f(100,50);
|
||||
GL11.glVertex2f(0,50);
|
||||
} else {
|
||||
GL11.glVertex2f(0,-50);
|
||||
GL11.glVertex2f(100,-50);
|
||||
GL11.glVertex2f(100,0);
|
||||
GL11.glVertex2f(0,0);
|
||||
}
|
||||
GL11.glEnd();
|
||||
|
||||
GL11.glPopMatrix();
|
||||
GL11.glEnable(GL11.GL_TEXTURE_2D);
|
||||
}
|
||||
|
||||
private void draw_line(int x1, int y1, int x2, int y2){
|
||||
GL11.glDisable(GL11.GL_TEXTURE_2D);
|
||||
GL11.glPushMatrix();
|
||||
|
||||
GL11.glColor3f(1,1,0);
|
||||
GL11.glBegin(GL11.GL_LINES);
|
||||
GL11.glVertex2f(x1,y1);
|
||||
GL11.glVertex2f(x2,y2);
|
||||
GL11.glEnd();
|
||||
|
||||
GL11.glPopMatrix();
|
||||
GL11.glEnable(GL11.GL_TEXTURE_2D);
|
||||
}
|
||||
|
||||
public int getSpeed(){
|
||||
return speed;
|
||||
}
|
||||
|
||||
public int getWidth(){
|
||||
return width;
|
||||
}
|
||||
|
||||
public int getHeight(){
|
||||
return height;
|
||||
}
|
||||
|
||||
private int get_center_x(){
|
||||
return x + ((width * K) / 2);
|
||||
}
|
||||
|
||||
public boolean getRigthRun(){
|
||||
return rigth;
|
||||
}
|
||||
|
||||
public void setRigthRun(boolean value){
|
||||
rigth = value;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user