Advertisement
Guest User

2D Viewport Example

a guest
Jun 15th, 2012
319
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.51 KB | None | 0 0
  1. package de.master.demo;
  2.  
  3. import java.awt.event.KeyEvent;
  4.  
  5. import processing.core.PApplet;
  6. import processing.core.PImage;
  7.  
  8. public class ViewportExample extends PApplet {
  9.    
  10.     int W = KeyEvent.VK_W;
  11.     int A = KeyEvent.VK_A;
  12.     int S = KeyEvent.VK_S;
  13.     int D = KeyEvent.VK_D;
  14.    
  15.     boolean[] keys;
  16.     float speed;
  17.     PImage background;
  18.     PImage player;
  19.     float tx;
  20.     float ty;
  21.     float sceneWidth;
  22.     float sceneHeight;
  23.     float playerX;
  24.     float playerY;
  25.     float playerScreenX;
  26.     float playerScreenY;
  27.    
  28.    
  29.     @Override
  30.     public void setup() {
  31.         background  = loadImage("/background.png");
  32.         player      = loadImage("/player.png");
  33.        
  34.         sceneWidth = background.width;
  35.         sceneHeight= background.height;
  36.        
  37.         size((int)(sceneWidth / 4.0), (int)(sceneHeight / 4.0));
  38.         playerX = 0;
  39.         playerY = 0;
  40.        
  41.         speed = 3;
  42.         keys = new boolean[Short.MAX_VALUE];
  43.        
  44.         // initial setting
  45.         tx = -(playerX - (width / 2));
  46.         ty = -(playerY - (width / 2));
  47.        
  48.         if(tx > 0) tx = 0;
  49.         if(ty > 0) ty = 0;
  50.        
  51.         playerScreenX = width / 2;
  52.         playerScreenY = height / 2;
  53.        
  54.         // black
  55.         fill(0);
  56.         stroke(0);
  57.     }
  58.    
  59.     @Override
  60.     public void draw()
  61.     {
  62.         update();
  63.         image(background, tx, ty);
  64.         image(player, playerScreenX, playerScreenY);
  65.        
  66.         text((playerX + ":" + playerY), playerScreenX, playerScreenY);
  67.         rect(playerX, playerY, 5, 5);
  68.     }
  69.    
  70.     public void update()
  71.     {
  72.        
  73.         if(keys[A])
  74.         {
  75.             playerX -= speed;
  76.            
  77.             if(playerScreenX <= width / 2 && tx < 0)
  78.             {
  79.                 playerScreenX = width / 2;
  80.                 tx += speed;
  81.             }
  82.             else if(playerScreenX <= width / 2 && (tx) >= 0)
  83.             {
  84.                 playerScreenX -= speed;
  85.                 tx = 0;
  86.                
  87.                 if(playerScreenX < 0) playerScreenX = 0;
  88.             }
  89.             else if(playerScreenX >= width / 2 && (tx) < 0)
  90.             {
  91.                 playerScreenX -= speed;
  92.             }
  93.         }
  94.         if(keys[D])
  95.         {
  96.             playerX += speed;
  97.            
  98.             if(playerScreenX >= width / 2 && (-tx + width) < sceneWidth)
  99.             {
  100.                 playerScreenX = width / 2;
  101.                 tx -= speed;
  102.             }
  103.             if(playerScreenX >= width / 2 && (-tx + width) >= sceneWidth)
  104.             {
  105.                 playerScreenX += speed;
  106.                 tx = -(sceneWidth - width);
  107.                 if(playerScreenX >= width - player.width) playerScreenX = width - player.width;
  108.             }
  109.             if(playerScreenX <= width / 2 && (-tx + width) < sceneWidth)
  110.             {
  111.                 playerScreenX += speed;
  112.             }
  113.         }
  114.         if(keys[W])
  115.         {
  116.             playerY -= speed;
  117.             if(playerScreenY <= height / 2 && ty < 0)
  118.             {
  119.                 playerScreenY = height / 2;
  120.                 ty += speed;
  121.             }
  122.             else if(playerScreenY <= height / 2 && (ty) >= 0)
  123.             {
  124.                 playerScreenY -= speed;
  125.                 ty = 0;
  126.                 if(playerScreenY < 0) playerScreenY = 0;
  127.             }
  128.             else if(playerScreenY >= height / 2 && (ty) < 0)
  129.             {
  130.                 playerScreenY -= speed;
  131.             }
  132.         }
  133.         if(keys[S])
  134.         {
  135.             playerY += speed;
  136.             if(playerScreenY >= height / 2 && (-ty + height) < sceneHeight)
  137.             {
  138.                 playerScreenY = height / 2;
  139.                 ty -= speed;
  140.             }
  141.             if(playerScreenY >= height / 2 && (-ty + height) >= sceneHeight)
  142.             {
  143.                 playerScreenY += speed;
  144.                 ty = -(sceneHeight - height);
  145.                
  146.                 if(playerScreenY >= height - player.height) playerScreenY = height - player.height;
  147.             }
  148.             if(playerScreenY <= height / 2 && (-ty + height) < sceneHeight)
  149.             {
  150.                 playerScreenY += speed;
  151.             }
  152.         }
  153.         if(playerX < 0) playerX = 0;
  154.         if(playerX > sceneWidth) playerX = sceneWidth;
  155.         if(playerY < 0) playerY = 0;
  156.         if(playerY > sceneHeight) playerY = sceneHeight;
  157.        
  158.     }
  159.    
  160.     @Override
  161.     public void keyPressed(KeyEvent e) {
  162.         keys[e.getKeyCode()] = true;
  163.     }
  164.    
  165.     @Override
  166.     public void keyReleased(KeyEvent e) {
  167.         keys[e.getKeyCode()] = false;
  168.     }
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement