Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Where our rendering and the heart of the game takes place
- import java.awt.AWTException;
- import java.awt.Color;
- import java.awt.Component;
- import java.awt.Dimension;
- import java.awt.Font;
- import java.awt.FontFormatException;
- import java.awt.Graphics;
- import java.awt.GraphicsEnvironment;
- import java.awt.Image;
- import java.awt.Point;
- import java.awt.Rectangle;
- import java.awt.Robot;
- import java.awt.Toolkit;
- import java.awt.image.BufferStrategy;
- import java.awt.image.BufferedImage;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.IOException;
- import java.io.Reader;
- import java.util.HashMap;
- import java.util.Random;
- import javax.imageio.ImageIO;
- import javax.sound.sampled.LineUnavailableException;
- import javax.sound.sampled.UnsupportedAudioFileException;
- import javax.swing.Box;
- import javax.swing.BoxLayout;
- import javax.swing.Icon;
- import javax.swing.ImageIcon;
- import javax.swing.JButton;
- import javax.swing.JComponent;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JPanel;
- import javax.swing.SwingConstants;
- import javax.swing.border.EmptyBorder;
- import kuusisto.tinysound.Music;
- import kuusisto.tinysound.Sound;
- import kuusisto.tinysound.TinySound;
- class Window extends JFrame {
- private static final long serialVersionUID = 1L;
- GameMain game;
- JLabel title;
- JPanel WindowPanel;
- JLabel Title;
- JButton Enter;
- JButton Enter2;
- JButton Settings;
- JLabel torch;
- BackgroundPane MenuPane;
- Component TitleGap;
- Music TitleMusicSound;
- Music LonelyVoyage;
- int PixelSize = 10;
- static Vector2D TorchLocation;
- int lightx = 0;
- boolean DrawBoundries = true;
- boolean DisplayFPS = true;
- static final int ScreenWidth = Toolkit.getDefaultToolkit().getScreenSize().width;
- static final int ScreenHeight = Toolkit.getDefaultToolkit().getScreenSize().height;
- BufferedImage canvas;
- void SetFont(JComponent obj,String type,int size)
- {
- Font font = new Font(type,Font.PLAIN,size);
- obj.setFont(font);
- }
- LightSource globallight;
- public void SetMenuVisible(boolean b)
- {
- WindowPanel.setVisible(b);
- }
- public void ChangeStatus(String type) throws IOException // Changes the game status and rendering consequentially
- , UnsupportedAudioFileException, LineUnavailableException
- {
- if(type=="Game")
- {
- MenuPane.setVisible(false);
- TitleMusicSound.stop();
- LonelyVoyage = TinySound.loadMusic("SoundAssets\\LonelyVoyage.wav");
- LonelyVoyage.play(true);
- //Torch HouseTorch = new Torch(GraphicsConfiguration,new Point(100+300,460));
- globallight = new LightSource(GraphicsConfiguration,0f,1.0f,new Point(100+300+125,200+300/2),null);
- GraphicsObject House = new GraphicsObject(GraphicsConfiguration,"House",new Point(50,300),3,6);
- //GraphicsObject Box = new GraphicsObject(GraphicsConfiguration,"WoodBlock",new Point(75,835),50,50);
- //GraphicsObject Figure = new GraphicsObject(GraphicsConfiguration,"FigureTests",new Point(100+670,400));
- }
- }
- HashMap<Point,Color> pixelbase = new HashMap<Point,Color>();
- GraphicsObject[] GraphicsObjects = new GraphicsObject[1000];
- LightSource[] LightSources = new LightSource[1000];
- // Graphics Object Configuration
- // Used as the base configuration cache for all GraphicsObjects,
- // and lets added classes show themselves into their respective arrays
- GraphicsObjectConfiguration GraphicsConfiguration = new GraphicsObjectConfiguration(this);
- File img = new File("src\\ImageAssets\\House.png");
- BufferedImage image;
- int[] originalcolor;
- Point TopLeft;
- public float lerp(float a,float b,float t)
- {
- return a*(1-t)+(b*t);
- }
- public float clamp(float v,float min,float max)
- {
- if(v<min)
- {
- return min;
- }
- if(v>max)
- {
- return max;
- }
- return v;
- }
- public Color lerpcolors(Color color1,Color color2,float perc)
- {
- Color newcolor;
- newcolor = new Color(
- (int) lerp(color1.getRed(),color2.getRed(),perc),
- (int) lerp(color1.getGreen(),color2.getGreen(),perc),
- (int) lerp(color1.getBlue(),color2.getBlue(),perc)
- );
- return newcolor;
- }
- public LightSource getNearestLightSource(Point pixelpoint)
- {
- int leastdistance = 10000;
- LightSource NearestLight = null;
- for(int i = 0;i<LightSources.length;i++)
- {
- LightSource light = LightSources[i];
- if (light!=null)
- {
- int distance = (int) light.location.distance(pixelpoint);
- if((distance<leastdistance) && distance <= light.Range*2)
- {
- NearestLight=light;
- leastdistance=distance;
- }
- }
- }
- return NearestLight;
- }
- public LightSource[] getInLightSources(Point pixelpoint)
- {
- LightSource[] inlightsources = null;
- int sourcescount=-1;
- for(int i = 0;i<LightSources.length;i++)
- {
- LightSource light = LightSources[i];
- if (light!=null)
- {
- int distance = (int) light.location.distance(pixelpoint);
- if (distance<light.Range)
- {
- if(inlightsources==null)
- {
- inlightsources = new LightSource[10];
- }
- sourcescount++;
- inlightsources[sourcescount]=light;
- }
- }
- }
- return inlightsources;
- }
- public int getOccupied(Object[] a)
- {
- int count = 0;
- for(int i = 0;i<a.length;i++)
- {
- if(a[i]!=null)
- {
- count++;
- }
- }
- return count;
- }
- public static float randomfloat(int min, int max) {
- Random rand = new Random();
- int randomNum = rand.nextInt((max - min) + 1) + min;
- return (float) randomNum;
- }
- float lasttick = -1000;
- int inc = 0;
- public void render() throws InterruptedException, IOException
- {
- inc+=1;
- if(GameMain.GameStatus!="Game"){return;}
- BufferStrategy bs = this.getBufferStrategy();
- if(bs==null)
- {
- createBufferStrategy(3);
- return;
- }
- Graphics g = bs.getDrawGraphics();
- globallight.Range = 200;//globallight.Range+1;
- //globallight.Range = randomfloat(190,210);
- Point ImageLocation = new Point(250,250);
- g.fillRect(0, 0,ScreenWidth,ScreenHeight); // Fill a black outline
- g.setColor(Color.BLACK);
- for(int i = 0;i<GraphicsObjects.length;i++)
- {
- GraphicsObject object = GraphicsObjects[i];
- if(object!=null)
- {
- GraphicsObjectConfiguration.config config;
- config = object.config;
- BufferedImage image = config.image;
- int width = config.width;
- int height = config.height;
- Point location = object.center;
- int locationx = location.x;
- int locationy = location.y;
- int sizex = object.sizex;
- int sizey = object.sizey;
- HashMap<Point, Color> PointToColor = config.configuration;
- int[] originalcolor = config.originalcolor; // A reference to the original color array, to set back to during the end
- int[] modcolor = config.modcolor;
- boolean lightsourcefound = false;
- boolean Rewriting = false;
- if(Rewriting==false) // Rewrite mod color
- {
- for(int x = 0;x<width;x++)
- {
- for(int y = 0;y<width;y++)
- {
- Point pixelpoint = new Point(x,y);
- Point pixelpointglobal = new Point(locationx+x,locationy+y);
- int pixelcolor;
- pixelcolor = image.getRGB(x, y);
- //PointToColor.get(pixelpoint);
- //if(pixelcolor!=null)
- //{
- //LightSource light = getNearestLightSource(pixelpointglobal);
- //System.out.println(light);
- //int red = pixelcolor.getRed();
- //int green = pixelcolor.getGreen();
- //int blue = pixelcolor.getBlue();
- // if(light!=null)
- // {
- //lightsourcefound=true;
- // Light each pixel based on the Gaussian profile
- // float lightbrightness = light.Brightness;
- // float distance_from_lightsource = (float) pixelpointglobal.distance(light.location);
- // float brightperc = 1;
- // (float) clamp((float)
- // (Math.pow(2.718,-(Math.pow(distance_from_lightsource,2)/Math.pow(light.Range,2)))),0,1)
- // *lightbrightness;
- // red = (int) clamp((red*brightperc*lightbrightness),0.0f,255.0f);
- // green = (int) clamp((green*brightperc*lightbrightness),0.0f,255.0f);
- // blue = (int) clamp((blue*brightperc*lightbrightness),0.0f,255.0f);
- // pixelcolor = new Color(red,green,blue,255); // Draw pixel
- // modcolor[width*y+x]=pixelcolor.getRGB();
- // }
- //}
- }
- }
- image.setRGB(0,0,width,height,modcolor,0,width);
- }
- g.drawImage(image,locationx,locationy,sizex,sizey,null);
- image.setRGB(0,0,width,height,originalcolor,0,width);
- }
- }
- if(DisplayFPS==true)
- {
- g.setColor(Color.RED);
- g.drawString("FPS: "+game.ticks,25,50);
- }
- if(!DrawBoundries)
- {
- for(int i = 0;i<GraphicsObjects.length;i++) // Draw Boundaries
- {
- GraphicsObject o = GraphicsObjects[i];
- if (o!=null)
- {
- Point center = o.center;
- if(center!=null)
- {
- g.drawRect(center.x,center.y,1,1);
- }
- }
- }
- }
- bs.show();
- }
- public class BackgroundPane extends JPanel {
- /**
- *
- */
- private static final long serialVersionUID = 1L;
- private BufferedImage img;
- @Override
- public Dimension getPreferredSize() {
- BufferedImage img = getBackgroundImage();
- Dimension size = super.getPreferredSize();
- if (img != null) {
- size.width = Math.max(size.width, img.getWidth());
- size.height = Math.max(size.height, img.getHeight());
- }
- return size;
- }
- public BufferedImage getBackgroundImage() {
- return img;
- }
- public void setBackgroundImage(BufferedImage value) {
- if (img != value) {
- BufferedImage old = img;
- img = value;
- firePropertyChange("background", old, img);
- revalidate();
- repaint();
- }
- }
- @Override
- protected void paintComponent(Graphics g) {
- super.paintComponent(g);
- BufferedImage bg = getBackgroundImage();
- if (bg != null) {
- int x = (getWidth() - bg.getWidth()) / 2;
- int y = (getHeight() - bg.getHeight()) / 2;
- g.drawImage(bg, x, y, this);
- File torchfile = new File(GameMain.ImageAssets+"Torch.png");
- ImageIcon torch = new ImageIcon(torchfile.getPath());
- Image torchimage = torch.getImage();
- System.out.println(GameMain.GameStatus);
- if(TorchLocation!=null)
- {
- g.drawImage(torchimage,TorchLocation.x,TorchLocation.y,this);
- }
- else
- {
- }
- }
- }
- }
- public void AddGraphicsObject(GraphicsObject object)
- {
- }
- public Window(GameMain gameMain) throws FontFormatException, IOException, UnsupportedAudioFileException, LineUnavailableException, AWTException
- {
- super("Verwarring"); // super calls The JFrame constructor
- TinySound.init();
- game=gameMain;
- canvas = new Robot().createScreenCapture(new Rectangle(0,0,ScreenWidth,ScreenHeight));
- TitleMusicSound = TinySound.loadMusic("SoundAssets\\TitleMusic.wav");
- TitleMusicSound.setVolume(0.5);
- TitleMusicSound.play(false);
- File Cardinal = new File(GameMain.ImageAssets+"Cardinal.ttf");
- Font CardinalFont = Font.createFont(NORMAL, Cardinal);
- GraphicsEnvironment.getLocalGraphicsEnvironment().registerFont(CardinalFont); // Registering Cardinal Font
- WindowPanel = new JPanel(); // Main Menu Panel
- MenuPane = new BackgroundPane();
- MenuPane.setLayout(new BoxLayout(MenuPane,BoxLayout.Y_AXIS));
- WindowPanel.add(MenuPane);
- try {
- BufferedImage bg = ImageIO.read(new File(GameMain.ImageAssets+"Gradient.png"));
- MenuPane.setBackgroundImage(bg);
- } catch (IOException ex) {
- ex.printStackTrace();
- }
- TitleGap = Box.createRigidArea(new Dimension(0,40));
- Title = new JLabel();
- Title.setText("");
- Title.setOpaque(false);
- Title.setBackground(Color.PINK);
- Title.setHorizontalAlignment(SwingConstants.CENTER);
- Title.setAlignmentX(CENTER_ALIGNMENT);
- Title.setMaximumSize(new Dimension(700,180));
- Title.setPreferredSize(Title.getMaximumSize());
- Title.setBorder(new EmptyBorder(20, 20, 20, 20));
- Enter = new JButton();
- Enter.setText("Singleplayer");
- Enter.setHorizontalAlignment(SwingConstants.CENTER);
- Enter.setBackground(Color.RED);
- Enter.setOpaque(false);
- Enter.setMaximumSize(new Dimension(170+250,200));
- Enter.setHorizontalAlignment(SwingConstants.CENTER);
- Enter.setAlignmentX(CENTER_ALIGNMENT);
- Enter.setBorderPainted(false);
- Enter.setBackground(new Color(255,255,255));
- Enter.setFocusable(false);
- Enter.setContentAreaFilled(false);
- Enter2 = new JButton();
- Enter2.setText("Multiplayer");
- Enter2.setHorizontalAlignment(SwingConstants.CENTER);
- Enter2.setBackground(Color.RED);
- Enter2.setOpaque(false);
- Enter2.setMaximumSize(new Dimension(170+250,200));
- Enter2.setHorizontalAlignment(SwingConstants.CENTER);
- Enter2.setAlignmentX(CENTER_ALIGNMENT);
- Enter2.setBorderPainted(false);
- Enter2.setBackground(new Color(255,255,255));
- Enter2.setFocusable(false);
- Enter2.setContentAreaFilled(false);
- Settings = new JButton();
- Settings.setText("Settings");
- Settings.setHorizontalAlignment(SwingConstants.CENTER);
- Settings.setBackground(Color.RED);
- Settings.setOpaque(false);
- Settings.setMaximumSize(new Dimension(170+250,200));
- Settings.setHorizontalAlignment(SwingConstants.CENTER);
- Settings.setAlignmentX(CENTER_ALIGNMENT);
- Settings.setBorderPainted(false);
- Settings.setForeground(Color.BLACK);
- Settings.setBackground(new Color(255,255,255));
- Settings.setFocusable(false);
- Settings.setContentAreaFilled(false);
- SetFont(Enter,"Cardinal",64);
- SetFont(Enter2,"Cardinal",64);
- SetFont(Settings,"Cardinal",64);
- UIMouseListener OnHoverEnter = new UIMouseListener(this,MenuPane,Enter,TitleGap);
- UIMouseListener OnHoverEnter2 = new UIMouseListener(this,MenuPane,Enter2,TitleGap);
- UIMouseListener OnHoverSettings = new UIMouseListener(this,MenuPane,Settings,TitleGap);
- Font Cardinal104 = new Font("Cardinal",Font.PLAIN,104);
- File GameIcon = new File(GameMain.ImageAssets+"GameIcon.png");
- Icon icon = new ImageIcon(GameIcon.getPath());
- Title.setIcon(icon);
- File GameWindowIconFile = new File(GameMain.ImageAssets+"GameWindowIcon.png"); // Window Icon
- Image GameWindowIcon = new ImageIcon(GameWindowIconFile.getPath()).getImage();
- MenuPane.add(Title);
- MenuPane.add(TitleGap);
- MenuPane.add(Enter);
- MenuPane.add(Enter2);
- MenuPane.add(Settings);
- this.setIconImage(GameWindowIcon);
- this.add(WindowPanel);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement