Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import java.awt.event.*;
- Robot robot;
- Player p;
- void setup() {
- fullScreen(P3D);
- p=new Player(0, 100, 0);
- try {
- robot=new Robot();
- }
- catch(Exception e) {
- e.printStackTrace();
- }
- noCursor();
- }
- void draw() {
- background(255);
- fill(0);
- line(0, 0, 0, 10000, 0, 0);
- line(0, 0, 0, 0, 10000, 0);
- line(0, 0, 0, 0, 0, 10000);
- fill(#ff0000);
- pushMatrix();
- translate(250, 0, 250);
- box(500, -2, 500);
- popMatrix();
- p.draw();
- }
- class Player {
- PVector pos;
- float moveSpeed;
- PVector viewCenter;
- float angle;
- float viewDistance;
- boolean isW=false, isS=false, isA=false, isD=false;
- Player(float x, float y, float z) {
- pos=new PVector(x, y, z);
- viewCenter=new PVector(0, 0, 0);
- viewDistance=sqrt(sq(40)*20);
- moveSpeed=20;
- }
- void draw() {
- camera(pos.x, pos.y, pos.z, viewCenter.x, viewCenter.y, viewCenter.z, 0, -1, 0);
- movement();
- }
- void movement() {
- PVector direction=new PVector(viewCenter.x-pos.x, viewCenter.z-pos.z);
- direction.setMag(moveSpeed);
- if (isW) {
- pos.add(direction.x, 0, direction.y);
- viewCenter.add(direction.x, 0, direction.y);
- }
- if (isS) {
- direction.rotate(radians(180));
- pos.add(direction.x, 0, direction.y);
- viewCenter.add(direction.x, 0, direction.y);
- }
- if (isA) {
- direction.rotate(radians(90));
- pos.add(direction.x, 0, direction.y);
- viewCenter.add(direction.x, 0, direction.y);
- }
- if (isD) {
- direction.rotate(radians(270));
- pos.add(direction.x, 0, direction.y);
- viewCenter.add(direction.x, 0, direction.y);
- }
- }
- void keyReleased(char k, int code) {
- setMove(k, code, false);
- }
- void keyPressed(char k, int code) {
- setMove(k, code, true);
- }
- void setMove(char k, int code, boolean b) {
- switch(k) {
- case 'w':
- isW=b;
- break;
- case 's':
- isS=b;
- break;
- case 'a':
- isA=b;
- break;
- case 'd':
- isD=b;
- break;
- default:
- switch(code) {
- }
- break;
- }
- }
- void mouseMoved(float dx, float dy) {
- angle-=dx/10;
- angle%=360;
- viewCenter=new PVector(pos.x+cos(radians(angle))*viewDistance, pos.y, pos.z+sin(radians(angle))*viewDistance);
- }
- }
- void keyPressed() {
- p.keyPressed(key, keyCode);
- }
- void keyReleased() {
- p.keyReleased(key, keyCode);
- }
- void mouseMoved() {
- float dx=mouseX-width/2;
- float dy=mouseY-height/2;
- p.mouseMoved(dx, dy);
- robot.mouseMove(width/2, height/2);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement