Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (739)
Games in Android Showcase (224)
games submitted by our members
Games in WIP (820)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
  JavaGaming.org - Pastebin



Author: StumpyStrust (posted 2013-08-07 23:01:08, viewed 452 times)

1   2   3   4   5   6   7   8   9   10   11   12   13   14   15   16   17   18   19   20   21   22   23   24   25   26   27   28   29   30   31   32   33   34   35   36   37   38   39   40   41   42   43   44   45   46   47   48   49   50   51   52   53   54   55   56   57   58   59   60   61   62   63   64   65   66   67   68   69   70   71   72   73   74   75   76   77   78   79   80   81   82   83   84   85   86   87   88   89   90   91   92   93   94   95   96   97   98   99   100   101   102   103   104   105   106   107   108   109   110   111   112   113   114   115   116   117   118   119   120   121   122   123   124   125   126   127   128   129   130   131   132   133   134   135   136   137   138   139   140   141   142   143   144   145   146   147   148   149   150   151   152   153   154   155   156   157   158   159   160   161   162   163   164   165   166   167   168   169   170   171   172   173   174   175   176   177   178   179   180   181   182   183   184   185   186   187   188   189   190   191   192   193   194   195   196   197   198   199   200   201   202   203   204   205   206   207   208   209   210   211   212   213   214   215   216   217   218   219   220   221   222   223   224   225   226   227   228   229   230   231   232   233   234   235   236   237   238   239   240   241   242   243   244   245   246   247   248   249   250   251   252   253   254   255   256   257   258   259   260   261   262   263   264   265   266   267   268   269   270   271   272   273   274   275   276   277   278   279   280   281   282   283   284   285   286   287   288   289   290   291   292   293   294   295   296   297   298   299   300   301   302   303   304   305   306   307   308   309   310   311   312   313   314   315   316   317   318   319   320   321   322   323   324   325   326   327   328   329   330   331   332   333   334   335   336   337   338   339   340   341   342   343   344   345   346   347   348   349   350   351   352   353   354   355   356   357   358   359   360   361   362   363   364   365   366   367   368   369   370   371   372   373   374   375   376   377   378   379   380   381   382   383   384   385   386   387   388   389   390   391   392   393   394   395   396   397   398   399   400   401   402   403   404   405   406   407   408   409   410   411   412   413   414   415   416   417   418   419   420   421   422   423   424   425   426   427   428   429   430   431   432   433   434   435   436   437   438   439   440   441   442   443   444   445   446   447   448   449   450   451   452   453   454   455   456   457   458   459   460   461   462   463   464   465   466   467   468   469   470   471   472   473   474   475   476   477   478   479   480   481   482   483   484   485   486   487   488   489   490   491   492   493   494   495   496   497   498   499   500   501   502   503   504   505   506   507   508   509   510   511   512   513   514   515   516   517   518   519   520   521   522  
package com.stumpy.code.light.main;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Polygon;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.util.ArrayList;

import com.stumpy.code.light.test.BufferUtils;

public class Light {
   private static Rectangle RECT1 = new Rectangle(0,0);
   private static final Color BLANK  = new Color(0,0,0,0);
   private static BufferedImage LIGHT_1;
   
   static
   {
      LIGHT_1 = BufferUtils.loadImage("res/light2.png", true);
   }
   
   private ArrayList<Block> blocks;

   public Vector2 maxSize = new Vector2(512,512);
   public Vector2 minSize = new Vector2(4,4);
   public Vector2 sizeFlux = new Vector2(0,0);
   public Vector2 loc = new Vector2(0,0);
   public Vector2 size = new Vector2(32,32);
   private Color color = Color.BLUE;
   private BufferedImage originalLight, light;
   private Graphics2D lightg2d;
   private LightMap father;
   public float intensity = 1;
   public float decay = 0;
   public boolean alive = false;
   public boolean dynamic = true;
   private boolean baked = false;
   
   public Light(double x, double y, double width, double height, Color c)
   {   
      loc.x = x;
      loc.y = y;
      size.x = width;
      size.y = height;
      color = c;
      alive = true;
      light = new BufferedImage((int)width,(int)height,BufferedImage.TRANSLUCENT);
      lightg2d = (Graphics2D) light.getGraphics();
      BufferedImage tex = LIGHT_1;
      originalLight = BufferUtils.copyNewSrc(tex);
      setColor();
   }
   
   public Light(int x, int y, int width, int height, BufferedImage tex, Color c)
   {   
      loc.x = x;
      loc.y = y;
      size.x = width;
      size.y = height;
      color = c;
      alive = true;
      light = new BufferedImage((int)width,(int)height,BufferedImage.TRANSLUCENT);
      lightg2d = (Graphics2D) light.getGraphics();
      originalLight = BufferUtils.copyNewSrc(tex);
      setColor();
   }
   
   public void jump(double x, double y)
   {
      loc.x = x;
      loc.y = y;
   }
   
   public void update()
   {
      size.add(sizeFlux);
      if(size.x >= maxSize.x || size.x <= minSize.x)
         sizeFlux.x *= -1;
      if(size.y >= maxSize.y || size.x <= minSize.y)
         sizeFlux.y *= -1;
      intensity -= decay;
      if(intensity < 0)
         alive = false;
      if(father.dynamicLight())
         return;
   }
   
   public void render(Graphics g)
   {
      if(!father.getScreenBounds().intersects(loc.x-size.x/2, loc.y-size.y/2, size.x,size.y))
         return;
      Graphics2D g2d = (Graphics2D) g.create();
   
      if(father.dynamicLight() && dynamic)
         createShadows();
      g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, intensity));
      g2d.drawImage(light, (int)(loc.x-(size.x/2)),(int)(loc.y-(size.y/2)),(int)size.x, (int) (size.y),null);
      g2d.dispose();
   }
   
   private void createShadows()
   {
      if(baked)
         return;
      clear();
      for(int i = 0;i<= blocks.size()-1;i++)
      {
         Block blocker = blocks.get(i);
         if(blocker.collision(this))
         {
            Point p1 = null,p2 = null,p3,p4,p5 = null,p6 = null;
            Polygon poly = new Polygon();
            if(loc.x < blocker.loc.x + blocker.size.x/2 && loc.x> blocker.loc.x - blocker.size.x/2 )
            {
               if(blocker.loc.y >= loc.y)
               {
                  p1 = new Point((int)(blocker.loc.x - blocker.size.x/2),(int) (blocker.loc.y-blocker.size.y/2));
                  p2 = new Point((int)(blocker.loc.x + blocker.size.x/2),(int) (blocker.loc.y-blocker.size.y/2));
                  p5 = new Point((int)(blocker.loc.x + blocker.size.x/2),(int) (blocker.loc.y+blocker.size.y/2));
                  p6 = new Point((int)(blocker.loc.x - blocker.size.x/2),(int) (blocker.loc.y+blocker.size.y/2));
               }
               else
               {
                  p1 = new Point((int)(blocker.loc.x - blocker.size.x/2),(int) (blocker.loc.y+blocker.size.y/2));
                  p2 = new Point((int)(blocker.loc.x + blocker.size.x/2),(int) (blocker.loc.y+blocker.size.y/2));
                  p5 = new Point((int)(blocker.loc.x + blocker.size.x/2),(int) (blocker.loc.y-blocker.size.y/2));
                  p6 = new Point((int)(blocker.loc.x - blocker.size.x/2),(int) (blocker.loc.y-blocker.size.y/2));
               }
            }
            else if (loc.y < blocker.loc.y + blocker.size.y/2 && loc.y > blocker.loc.y - blocker.size.y/2 )
            {
               if(loc.x <= blocker.loc.x)
               {
                  p1 = new Point((int)(blocker.loc.x - blocker.size.x/2),(int) (blocker.loc.y-blocker.size.y/2));
                  p2 = new Point((int)(blocker.loc.x - blocker.size.x/2),(int) (blocker.loc.y+blocker.size.y/2));
                  p5 = new Point((int)(blocker.loc.x + blocker.size.x/2),(int) (blocker.loc.y+blocker.size.y/2));
                  p6 = new Point((int)(blocker.loc.x + blocker.size.x/2),(int) (blocker.loc.y-blocker.size.y/2));
               }
               else
               {
                  p1 = new Point((int)(blocker.loc.x + blocker.size.x/2),(int) (blocker.loc.y-blocker.size.y/2));
                  p2 = new Point((int)(blocker.loc.x + blocker.size.x/2),(int) (blocker.loc.y+blocker.size.y/2));
                  p5 = new Point((int)(blocker.loc.x - blocker.size.x/2),(int) (blocker.loc.y+blocker.size.y/2));
                  p6 = new Point((int)(blocker.loc.x - blocker.size.x/2),(int) (blocker.loc.y-blocker.size.y/2));
               }
            }
            else
            {
               if(blocker.loc.x < this.loc.x)
               {
                  if(blocker.loc.y-blocker.size.y/2 < loc.y)
                  {
                     p1 = new Point((int)(blocker.loc.x - blocker.size.x/2),(int) (blocker.loc.y+blocker.size.y/2));
                     p2 = new Point((int)(blocker.loc.x + blocker.size.x/2),(int) (blocker.loc.y-blocker.size.y/2));
                     p5 = new Point((int)(blocker.loc.x - blocker.size.x/2),(int) (blocker.loc.y-blocker.size.y/2));
                  }
                  else
                  {
                     p1 = new Point((int)(blocker.loc.x - blocker.size.x/2),(int) (blocker.loc.y-blocker.size.y/2));
                     p2 = new Point((int)(blocker.loc.x + blocker.size.x/2),(int) (blocker.loc.y+blocker.size.y/2));
                     p5 = new Point((int)(blocker.loc.x - blocker.size.x/2),(int) (blocker.loc.y+blocker.size.y/2));
                  }
               }
               else
               {
                  if(blocker.loc.y-blocker.size.y/2 < loc.y)
                  {
                     p1 = new Point((int)(blocker.loc.x - blocker.size.x/2),(int) (blocker.loc.y-blocker.size.y/2));
                     p2 = new Point((int)(blocker.loc.x + blocker.size.x/2),(int) (blocker.loc.y+blocker.size.y/2));
                     p5 = new Point((int)(blocker.loc.x + blocker.size.x/2),(int) (blocker.loc.y-blocker.size.y/2));
                  }
                  else
                  {
                     p1 = new Point((int)(blocker.loc.x + blocker.size.x/2),(int) (blocker.loc.y-blocker.size.y/2));
                     p2 = new Point((int)(blocker.loc.x - blocker.size.x/2),(int) (blocker.loc.y+blocker.size.y/2));
                     p5 = new Point((int)(blocker.loc.x + blocker.size.x/2),(int) (blocker.loc.y+blocker.size.y/2));
                  }
               }
            }
            Point p7 = null;
            if(blocker.size.x != blocker.size.y)
            {   
               double angle1 = Math.atan2(blocker.loc.y-loc.y, blocker.loc.x - loc.x)* 180/Math.PI;
               p7 = new Point((int)(loc.x + size.x *19* Math.cos(angle1 * Math.PI / 180)),
                        (int)(loc.y + size.y*19 * Math.sin(angle1 * Math.PI / 180)));
            }
            double angle1 = Math.atan2(p1.y-loc.y, p1.x - loc.x)* 180/Math.PI;
            double angle2 = Math.atan2(p2.y-loc.y, p2.x - loc.x)* 180/Math.PI;
            p3 = new Point((int)(loc.x + size.x *19* Math.cos(angle1 * Math.PI / 180)),
                     (int)(loc.y + size.y*19 * Math.sin(angle1 * Math.PI / 180)));
            p4 = new Point((int)(loc.x + size.x * 19 * Math.cos(angle2 * Math.PI / 180)),
                     (int)(loc.y + size.y*19 * Math.sin(angle2 * Math.PI / 180)));
            
            poly.addPoint(p1.x,p1.y);
            poly.addPoint(p3.x,p3.y);
            if(p7 != null)
               poly.addPoint(p7.x,p7.y);
            poly.addPoint(p4.x,p4.y);
            poly.addPoint(p2.x,p2.y);
            poly.addPoint(p5.x,p5.y);
            if(p6 != null)
               poly.addPoint(p6.x,p6.y);

            drawClip(poly);
         }
      }
   }
   
   private void drawClip(Polygon p)
   {
      p.translate(-(int)loc.x,-(int)loc.y);
      p.translate(light.getWidth()/2, light.getHeight()/2);
      if(father.getAA())
         lightg2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
      lightg2d.setColor(BLANK);
      lightg2d.fill(p);
   }
   
   private void clear()
   {
      lightg2d.setColor(BLANK);
      lightg2d.fillRect(0, 0, light.getWidth(),light.getHeight());
      lightg2d.drawImage(originalLight,0,0,light.getWidth(),light.getHeight(),null);
   }
   
   public void setColor(Color c)
   {
      color = c;
      setColor();
   }
   
   public void resize()
   {
      light = null;
      light = new BufferedImage((int)size.x,(int)size.y,BufferedImage.TRANSLUCENT);
      lightg2d = (Graphics2D) light.getGraphics();
      BufferedImage tex = LIGHT_1;

      originalLight = null;
      originalLight = BufferUtils.copyNewSrc(tex);
      setColor();
   }
   
   private void setColor()
   {
      Graphics2D g2d = (Graphics2D) originalLight.getGraphics();
      
      lightg2d.setColor(color);
      lightg2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC, 1f));
      lightg2d.fillRect(0,0,light.getWidth(),light.getHeight());
      
      lightg2d.setComposite(AlphaComposite.getInstance(AlphaComposite.DST_ATOP, 1f));
      lightg2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
      lightg2d.drawImage(originalLight,0,0,light.getWidth(),light.getHeight(),null);
      g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC));
      
      g2d.drawImage(light, 0, 0, originalLight.getWidth(), originalLight.getHeight(),null);
   }
   
   public void setFather(LightMap l)
   {
      father = l;
   }
   
   public void setCullers(ArrayList<Block> cullers)
   {
      blocks = cullers;
   }
   
   public void bakeLight()
   {
      createShadows();
      baked = true;
   }
   
   public void debakeLight()
   {
      baked = false;
   }
   
   public boolean containsPoint(double x, double y)
   {
      RECT1.x = (int) (loc.x-size.x/2);
      RECT1.y = (int) (loc.y - size.y/2);
      RECT1.width = (int) size.x;
      RECT1.height = (int) size.y;
      return RECT1.contains(x, y);
   }
}



package com.stumpy.code.light.main;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.image.VolatileImage;
import java.util.ArrayList;

import com.stumpy.code.light.test.BufferUtils;

public class LightMap {
   
   private static final Color BLANK = new Color(0,0,0,0);
   
   public ArrayList<Block> cullers = new ArrayList<Block>(50);
   
   private boolean AA_ON = true;
   private boolean DYNAMIC_LIGHT = true;
   private boolean LIGHT_ON = true;

   private int scrWidth;
   private int scrHeight;
   private Rectangle screenBounds = new Rectangle(0,0,0,0);
   private int mapWidth;
   private int mapHeight;
   private int lightCount = 0 ;
   private int maxLights = 120;
   private int scale;
   private ArrayList<Light> lights = new ArrayList<Light>(25);
   private VolatileImage lightmap;
   private Graphics2D mapg2d;
   
   private Color ambientColor = new Color(0,0,0,255);
   private float ambientLight = 0f;
   private float colorBlend = .4f;
   
   public boolean getAA()
   {
      return AA_ON;
   }
   
   public boolean dynamicLight()
   {
      return DYNAMIC_LIGHT;
   }
   
   public boolean lightOn()
   {
      return LIGHT_ON;
   }
   
   public int getScale()
   {
      return scale;
   }
   
   public void setScale(int i)
   {
      scale = i;
      mapWidth = scrWidth/scale;
      mapHeight = scrHeight/scale;
      lightmap = BufferUtils.getCompatibleVImage(mapWidth, mapHeight);
      mapg2d = (Graphics2D) lightmap.getGraphics();
   }
   
   public void setAA(boolean c)
   {
      AA_ON = c;
   }
   
   public void setDynamic(boolean c)
   {
      DYNAMIC_LIGHT = true;
   }
   
   public void setLightOn(boolean c)
   {
      LIGHT_ON = c;
   }
   
   public void clearLights()
   {
      lights.clear();
   }
   
   public void setColorBlend(float f)
   {
      colorBlend = f;
   }
   
   public void setAmbientColor(Color c)
   {
      ambientColor = c;
   }
   
   public void setAmbientLuminosity(float f)
   {
      ambientColor = new Color(ambientColor.getRed(),ambientColor.getGreen(),ambientColor.getBlue(),(int)(f*255));
   }
   
   public void setAmbientLuminosity(int f)
   {
      ambientColor = new Color(ambientColor.getRed(),ambientColor.getGreen(),ambientColor.getBlue(),f);
   }
   
   public void setAmbientLight(float f)
   {
      ambientLight = f;
   }
   
   public Rectangle getScreenBounds()
   {
      return screenBounds;
   }
   
   public void setScreenBounds(double x, double y, double w, double h)
   {
      screenBounds.x = (int) x;
      screenBounds.y = (int) y;
      screenBounds.width = (int) w;
      screenBounds.height = (int) h;
   }
   
   public void addLight(Light l)
   {
      if(lightCount >= maxLights)
         return;
      lights.add(l);
      lights.get(lights.size()-1).setFather(this);
      lights.get(lights.size()-1).setCullers(cullers);
   }
   
   public void addLight(Light l, int where)
   {
      if(!LIGHT_ON || lightCount >= maxLights)
         return;
      lights.add(where, l);
      lights.get(lights.size()-1).setFather(this);
      lights.get(lights.size()-1).setCullers(cullers);
   }
   
   public LightMap(int w, int h, int scale)
   {
      this.scale = scale;
      scrWidth = w;
      scrHeight = h;
      mapWidth = w/scale;
      mapHeight = h/scale;
      lightmap = BufferUtils.getCompatibleVImage(mapWidth, mapHeight);
      mapg2d = (Graphics2D) lightmap.getGraphics();
   }
   
   public void update()
   {
      for(int i = 0;i<= lights.size()-1;i++)
      {
         lights.get(i).update();
         if(!lights.get(i).alive)
            lights.remove(i);
      }
      lightCount = lights.size();
   }
   
   public void render(Graphics g)
   {
      render(g,0,0);
   }
   
   public void render(Graphics g, int offX, int offY)
   {
      if(!LIGHT_ON)
         return;
      clear();
      mapg2d.translate(-offX,-offY);
      mapg2d.setColor(ambientColor);
      mapg2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, ambientLight));
      mapg2d.fillRect(offX,offY,mapWidth,mapHeight);
      for(int i = 0; i <= lights.size()-1;i++)
      {
         lights.get(i).render(mapg2d);
      }

      Graphics2D g2d = (Graphics2D) g.create();
      g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
      g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, colorBlend));
      g2d.drawImage(lightmap,offX,offY,scrWidth,scrHeight,null);
      g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.DST_ATOP, 1));
      g2d.drawImage(lightmap,offX,offY,scrWidth,scrHeight,null);
      g2d.dispose();
      mapg2d.translate(offX,offY);
   }
   
   private void clear()
   {
      mapg2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC));
      mapg2d.setColor(BLANK);
      mapg2d.fillRect(0, 0, mapWidth, mapHeight);
   }
   
   public ArrayList<Light> getLights()
   {
      return lights;
   }
   
   public void bakeLights()
   {
      for(int i = 0; i <= lights.size()-1;i++)
      {
         lights.get(i).bakeLight();
         lights.get(i).dynamic = false;
      }
   }
   
   public void debakeLights()
   {
      for(int i = 0; i <= lights.size()-1;i++)
      {
         lights.get(i).debakeLight();
         lights.get(i).dynamic = true;
      }
   }
   
}





Dump your java code here :



Special syntax:
  • To highlight a line (yellow background), prefix it with '@@'
  • To indicate that a line should be removed (red background), prefix it with '-'
  • To indicate that a line should be added (green background), prefix it with '+'
  • To post multiple snippets, seperate them by '~~~~'
  EOF
 
Ecumene (57 views)
2017-09-30 02:57:34

theagentd (79 views)
2017-09-26 18:23:31

cybrmynd (187 views)
2017-08-02 12:28:51

cybrmynd (185 views)
2017-08-02 12:19:43

cybrmynd (192 views)
2017-08-02 12:18:09

Sralse (203 views)
2017-07-25 17:13:48

Archive (767 views)
2017-04-27 17:45:51

buddyBro (901 views)
2017-04-05 03:38:00

CopyableCougar4 (1451 views)
2017-03-24 15:39:42

theagentd (1326 views)
2017-03-24 15:32:08
List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05

SF/X Libraries
by SkyAphid
2017-03-02 06:38:56

SF/X Libraries
by SkyAphid
2017-03-02 06:38:32

SF/X Libraries
by SkyAphid
2017-03-02 06:38:05

SF/X Libraries
by SkyAphid
2017-03-02 06:37:51
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!