id
stringlengths
36
36
text
stringlengths
1
1.25M
2a2203ac-adbd-40ce-9679-6f9bd581da61
private static void drawTiled1dAnimated() { final int size = 1000; final int scale = 400; final int frames = 75; double p = 1.0 / 3; int seed = (int) (Integer.MAX_VALUE * Math.random()); int n = 6; Perlin2d pn = new Perlin2d(p, n, seed); final double[][] y...
d298f7c7-09a6-45fe-84a0-c7a664967585
@Override public void paintComponent(Graphics g) { g.drawImage(images[count / 10], 0, 0, null); count = (count + 1) % (frames * 10); }
e50e598f-9a86-4883-b50c-f59463f6b351
private static void drawTiled2d() { int width = 800; int height = 600; double p = 1.0 / 2; int seed = (int) (Integer.MAX_VALUE * Math.random()); int n = 4; Perlin2d pn = new Perlin2d(p, n, seed); double[][] y = pn.createTiledArray(width, height); //draw ...
6b5aedf3-91a2-43ef-9804-5d9d070c1a7a
@Override public void paintComponent(Graphics g) { g.drawImage(result, 0, 0, null); }
5b5f2791-06e4-4d10-84df-e9545e61c4de
public static void display(JPanel panel) { final JFrame frame = new JFrame(); frame.add(panel); frame.setVisible(true); Dimension panelDim = panel.getPreferredSize(); frame.setSize(new Dimension(panelDim.width + frame.getInsets().left + frame.getInsets().right, panelDim.height + ...
a5ab2a89-a73b-44c4-9d72-641faa5d54c4
@Override public void run() { while (true) { try { sleep(10); } catch (InterruptedException ex) { Logger.getLogger(Perlin2d.class.getName()).log(Level.SEVERE, null, ex); } ...
3408ffb3-6f1a-4ce9-8efe-03bdfe644b40
public static void savePng(BufferedImage image, String fname) { new File("output images/").mkdir(); try { ImageIO.write(image, "png", new File("output images/" + fname)); } catch (IOException ex) { Logger.getLogger(Perlin2d.class.getName()).log(Level.SEVERE, null, ex); ...
7c95ade7-d19e-40aa-b3d4-4ae7c8c702ef
public static void saveGif(BufferedImage[] images, String fname) { new File("output images/").mkdir(); try { new File("output images").mkdir(); ImageOutputStream output = new FileImageOutputStream(new File("output images/" + fname)); GifSequenceWriter writer = new Gif...
08acf71c-cb70-4965-9868-915a6a6970cc
public double interpolate(double a, double b, double fractional);
4c0b4987-85a5-4e3d-be61-e266bb77e212
@Override public double interpolate(double a, double b, double fractional) { double ft = fractional * 3.1415927; double f = (1 - Math.cos(ft)) * .5; return a * (1 - f) + b * f; }
46234616-1436-4009-84c0-8fd3916e3f4e
@Override public double interpolate(double a, double b, double fractional) { return a * (1 - fractional) + b * fractional; }
596ca6fb-5e9f-477c-affe-ec6578b448a2
void applyForces(double seconds);
cba8dbda-ae10-4e3a-a3c2-350925743b0e
void addForce(Vector2D force);
8e5e2c44-f138-4ab1-a448-1926b36a98e9
Vector2D getVel();
0db08509-8dbf-47cf-90fe-68fcba8f95c2
void setVel(Vector2D vel);
f6aa74dd-b14d-486a-9f4d-3c67f5e0b48f
void applyDrag(double seconds);
149a7341-ac9c-48c9-a6a9-d416f96c9295
Rectangle getCollisionBox();
6e488177-d5b5-4227-b33c-8c2c6c45dd3d
void move(int x, int y);
863b9f01-ce9d-46df-adf5-f7400337e674
public Renderer() { }
e9780746-bdab-4927-8b2b-9048bcf26f8b
public void invalidate() { stale = true; }
6b03d275-5d69-426f-bbe6-3be95dd1bcc0
public void invalidate(Rectangle r) { updateComp(r); updateImage(r); }
23590c1c-e0df-47ad-b615-847870a6c51d
public void setWorld(Level w) { world = w; finComp = new BufferedImage( w.getBounds().width, w.getBounds().height, BufferedImage.TYPE_4BYTE_ABGR); precomp = new double[w.getBounds().width] [w.getBounds().height] ...
67e01a80-d2bc-4f3d-96b0-46e2875c1b5b
public void addStaticProp(Drawable prop) { staticProps.add(prop); }
658311e5-b3f5-4947-a2f6-e031be756220
public void addDynProp(Drawable prop) { dynProps.add(prop); }
c013f74c-079a-43ad-a300-c34d34eec6a0
private void updateComp() { // System.out.println("Updating everything."); double[][][] worldP = world.getPixels(); //convert array from double[] to int for (int x = 0; x < worldP.length; x++) { for (int y = 0; y < worldP[0].length; y++) { for (int ch = 0; ch <...
eeb868c3-8544-4a41-95af-8ceb10e8fc1d
private void updateComp(Rectangle bounds) { // System.out.println("Updating region."); double[][][] worldP = world.getPixels(); //convert array from double[] to int int left = Math.max(0, bounds.x); int bottom = Math.max(0, bounds.y); int right = Math.min(worldP.length,...
03127b99-5e49-48fa-82a8-4fca5abcaf2c
private void add(double[][][] pixels, Rectangle bound) { Rectangle worldBounds = world.getBounds(); int left = Math.max(bound.x, worldBounds.x); int bottom = Math.max(bound.y, worldBounds.y); int right = (Math.min(bound.x + bound.width, worldBounds.x +...
1eaafaa4-d75a-469b-8474-6fc844098e7b
private void add(double[][][] pixels, Rectangle bound, Rectangle region) { Rectangle worldBounds = world.getBounds(); int left = Math.max(bound.x, worldBounds.x); int bottom = Math.max(bound.y, worldBounds.y); int right = (Math.min(bound.x + bound.width, ...
9dfbcea0-aca7-40df-a832-6031649de757
private void updateImage() { Util.pixelsToImage(precomp, finComp); }
c6e78747-7d6e-4a17-9729-63e85745a491
private void updateImage(Rectangle region) { Util.pixelsToImage(precomp, finComp, region); }
ac5451c2-53b6-4ea4-9288-252acf76bad2
public void draw(Graphics g, Component comp, int offsetX, int offsetY) { if (stale) { updateComp(); updateImage(); } Rectangle bounds = world.getBounds(); if (screen == null || screen.getWidth() != comp.getWidth() ...
8eb32a35-c685-43ba-911c-ff1b0d43e1b6
private void mergeDynProps(int offsetX, int offsetY) { for (Drawable prop : dynProps) { mergeDynProp(prop, offsetX, offsetY); } }
398fdd28-4069-4e6d-bc73-640899b6d674
private void mergeDynProp(Drawable prop, int offsetX, int offsetY) { byte[] pixels = ((DataBufferByte) screen .getRaster() .getDataBuffer()) .getData(); Rectangle r = prop.getBounds(); ...
9d9256a2-7906-4830-8db0-0e8a25354ecb
private Texture(String path) { this.path = path; }
b784ef4a-9cb5-4bdf-b524-d9d9d0ad5604
public String getName() { return path; }
2104c47d-4cf8-46b8-8295-663bca44c337
public Engine() { addKeyListener(new Controller()); setBackground(Color.black); setPreferredSize(new Dimension(WIDTH, HEIGHT)); setFocusable(true); //init world final int defWidth = 150; final int defHeight = 15; final Random gen = new Ra...
835da74f-78e9-4919-9891-861e608f5f65
public void paintComponent(Graphics page) { super.paintComponent(page); setForeground(Color.cyan); renderer.draw(page, this, offX, offY); // hero.draw(this, page, offX, offY); for (Entity effect : effects) { effect.draw(this, page, offX, offY...
f619b62f-f283-4e0f-96fd-d553ebd89b25
public void simulate(Dynamic obj, long timeStep) { /* Actions: * 1. apply drag * 2. add forces to object * - gravity (unless resting on block) * - arrow keys * * 3. apply forces to object. (done by the object) * * 4. a) move i...
26c8a1f7-7aec-4578-a40d-c3057012210d
private Point getWorldCollision(Dynamic obj, Texture target) { Rectangle r = obj.getCollisionBox(); Point lowerLeft = new Point(r.x, r.y); Point upperRight = new Point(r.x + r.width - 1, r.y + r.height - 1); int cell = World.CELL_SIZE; //If x is negative, offset...
86ac3877-f8cf-4e0d-b6c5-400303e05079
public void keyPressed(KeyEvent event) { switch (event.getKeyCode()) { case KeyEvent.VK_UP: offY += SCROLL_SPEED; break; case KeyEvent.VK_DOWN: offY -= SCROLL_SPEED; break; case KeyEvent.VK_LEFT: ...
e3df3d15-123b-424e-8fe0-f5cde1819a6e
public void keyReleased(KeyEvent event) { switch (event.getKeyCode()) { case KeyEvent.VK_W: up = false; break; case KeyEvent.VK_S: down = false; break; case KeyEvent.VK_A: left = false; ...
7baf6eb0-de22-4c21-b212-c17a6dd8e9d0
private void updateForces() { keyForce.set(0.0, 0.0); if (up && !down) { keyForce.offset(0, SPEED); } else if (down && !up) { keyForce.offset(0, -SPEED); } if (left && !right) { keyForce.offset(-SPEED, 0); ...
ae210305-9d56-4fa3-8d34-b852823f8d05
public void run() { long start = System.nanoTime(); long elapsed; long wait; final int minTimeBetweenFrames = 5; while (running) { //Timing //elapsed is the number of nanoseconds since last frame elapsed = System.nanoTime() - start; ...
e4e321d0-bee8-445a-8b42-1294adc75191
private void testTriggers(ArrayList<Trigger> trigs) { for (Trigger trigger : trigs) { if (trigger.isTriggered(hero.getBounds())) { trigger.triggerAction(); Entity burst = new Burst(tp); burst.setPos(((Light) trigger).getPos()); effects....
b178bb41-7150-4075-93a7-a2b71cef6a4a
private void deleteDeadEntities() { ArrayList<Entity> deadFX = new ArrayList<Entity>(); for (Entity effect : effects) { if (effect instanceof Burst) { Burst pop = (Burst) effect; if (pop.isDead()) { deadFX.add((Entity) pop); ...
cd8c58f0-5cbb-434c-a8dc-77c4ac85787e
private void updateCamPos() { final int marginX = (int) (getWidth() * 0.25); final int marginTop = (int) (getHeight() * 0.2); final int marginBottom = (int) (getHeight() * 0.2); Point pos = hero.getPos(); Dimension dim = hero.getSize(); // update x motion //...
776ceeb6-94c8-40b3-ba6c-799e429d2471
double[][][] getPixels();
7bcf80f8-d217-41ca-a31c-a840f077a613
byte[] getData();
61b8715d-7ed0-4e75-80c1-23c8296642d9
Rectangle getBounds();
346af580-bd08-43be-988b-165224e56124
short getDrawType();
6fb6fc39-5b0f-4f8b-b2c6-f5fb370aa416
boolean isDrawn();
9b53c01b-28b8-451f-8b8d-130e8949462f
public Entity() { pos = new Point(0, 0); }
128a8774-72c3-483f-9776-56989703b211
public void setImage(Image image) { brush = image; pixels = new double[image.getWidth(null)] [image.getHeight(null)] [Util.CHANNELS]; Util.imageToPixels(Util.toBufferedImage(image), pixels); BufferedImage bi = Util.toBuffere...
10f5e045-a632-4253-8cfd-6a6dc740bd12
public Dimension getSize() { return new Dimension(brush.getWidth(null), brush.getHeight(null)); }
31d09451-f6f6-4f3a-9b1d-b16e067ad926
public Rectangle getBounds() { return new Rectangle(pos.x, pos.y, brush.getWidth(null), brush.getHeight(null)); }
4579812b-ee45-4730-b36b-41f9d7053a84
public void setPos(int x, int y) { pos.x = x; pos.y = y; }
cb9f811e-b8b4-4f93-a9b7-6e9fac5fd348
public void setPos(Point p) { pos = new Point(p); }
fe1e6b89-c00f-472c-bd2b-5ca2323c0dff
public void move(int x, int y) { pos.x += x; pos.y += y; }
773564a8-f499-4d89-aafd-1305fc9c8997
public Point getPos() { return pos; }
f72d092d-b927-4e74-a78e-1ab27741950d
public void draw(Component comp, Graphics page, int offsetX, int offsetY) { //set x, y to the bottom left corner int x = 0; int y = comp.getHeight() - brush.getHeight(null); //move right and up by the x and y values. x += pos.x; y -= pos.y; //this should...
baf66985-c5f8-49be-9dc0-cd6745d13d2b
@Override public double[][][] getPixels() { return pixels; }
dc6c02c6-8931-410d-b27f-243467182f1c
@Override public short getDrawType() { // TODO Auto-generated method stub return 0; }
a7c98913-e6a3-4508-ad06-334c5198cc84
@Override public boolean isDrawn() { return true; }
42fb1809-1d61-42d6-a740-b2ca2a7e142b
@Override public byte[] getData() { return data; }
915a4fec-c5ed-44d6-a65e-d509d7aa62f9
public TexturePack(String basePath) { String path; URL res; Toolkit defToolkit = Toolkit.getDefaultToolkit(); BufferedImage swap; for (int i = 0; i < Texture.values().length; i++) { path = basePath + Texture.values()[i].getName(); /* Don't ask me why this ...
3a450c50-4782-4b8a-ae93-86d1bc34242c
public Image get(Texture t) { return imgs[t.ordinal()]; }
61897964-0e93-4ad6-864e-fddadab1fb4d
public double[][][] getP(Texture t) { return pixels[t.ordinal()]; }
89c90896-67d2-4de8-baa7-b43766c45d65
boolean isTriggered(Rectangle bounds);
49946283-69e5-4984-a838-f56c26ee2566
void triggerAction();
ba4901f2-d8ab-4d89-b5fd-e73a7885e693
public static void main(String[] args) { JFrame frame = new JFrame("Direction"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(new Engine()); frame.pack(); frame.setVisible(true); }
f78ee3ed-f9f9-4af6-a296-acad1f1e4734
public static void imageToPixels(BufferedImage image, double[][][] result) { final double maxByte = 255.0; final byte[] pixels = ((DataBufferByte) image.getRaster() .getDataBuffer()).getData(); ...
4c8b209f-d62a-4721-af44-441d4371cce5
public static void pixelsToImage(double[][][] pixels, BufferedImage image) { if (image.getWidth() != pixels.length || image.getHeight() != pixels[0].length) { throw new IllegalArgumentException("Image size " + "does not match pixel array."); } //b...
41e410d2-da7d-4ddd-a8f1-9d2be30a9879
public static void pixelsToImage(double[][][] pixels, BufferedImage image, Rectangle region) { if (image.getWidth() != pixels.length || image.getHeight() != pixels[0].length) { throw new IllegalArgumentExcep...
b0516087-d7f6-4381-ad02-6563402cbbdd
public static void copyPixelArray(double[][][] source, double[][][] dest) { for (int x = 0; x < source.length; x++) { for (int y = 0; y < source[0].length; y++) { System.arraycopy( source[x][y], 0, dest[x][y], 0, ...
9c317cf3-5f14-41dd-b3d7-84566c86b022
public static BufferedImage toBufferedImage(Image img) { if (img instanceof BufferedImage) { return (BufferedImage) img; } // Create a buffered image with transparency BufferedImage bimage = new BufferedImage(img.getWidth(null), ...
3ce84813-0f8c-4cf8-9829-98c3144c719c
public static int toIntRGBA(double[] argb) { int channel = (int) (argb[Util.A] * B_MAX); channel = Math.max(0, Math.min(channel, B_MAX)); int color = channel << 0; channel = (int) (argb[Util.B] * B_MAX); channel = Math.max(0, Math.min(channel, B_MAX)); c...
7e9ce91b-946a-457d-ae8b-bf514d49bb4a
public Vector2D(double x, double y) { this.x = x; this.y = y; }
860b45f9-0a06-47f6-bfce-35a3219f7df1
public Vector2D(Vector2D v) { x = v.x; y = v.y; }
fc3043e9-2a00-4e33-ab1b-dc3873e8556f
public double getX() { return x; }
07fadcfd-352c-4d9e-aa14-eddb2328ed2b
public double getY() { return y; }
d4c5cd18-cf10-4328-bcc7-33b5edad50ea
public void setX(double newX) { x = newX; }
5010df81-1a7f-4154-97e2-63749a11c0ca
public void setY(double newY) { y = newY; }
5abdfbd5-938f-4467-a67f-1ec53a592088
public void set(double newX, double newY) { this.x = newX; this.y = newY; }
6e178a37-b32f-480a-901d-b56d7369396f
public double length() { return Math.sqrt(x * x + y * y); }
1687130f-f372-41cd-a74e-1d9fd425546e
public Vector2D normalize() { double a = length(); return new Vector2D(x / a, y / a); }
43ee18fc-c445-460d-8eef-42dd8bfdaff2
public Vector2D negate() { return new Vector2D(-x, -y); }
19407999-66aa-49b8-995b-cb7a6d87f8fd
public Vector2D multiply(double factor) { return new Vector2D(x * factor, y * factor); }
26f324fa-0c82-4323-8555-2499b72274e3
public void offset(Vector2D v) { x += v.x; y += v.y; }
15c1a226-3a67-486d-a345-8915811c5eba
public void offset(double offsetX, double offsetY) { x += offsetX; y += offsetY; }
a47d9abd-47a1-4105-badd-2e2145d827c6
public Hero() { super(); vel = new Vector2D(0, 0); force = new Vector2D(0, 0); }
f4144453-d466-4ad7-b7e6-fbf476c64f07
@Override public void applyForces(double seconds) { vel.x += force.x * seconds; vel.y += force.y * seconds; force.set(0, 0); }
01a1b8c4-5be4-44ca-87e2-11922c036ae1
@Override public void addForce(Vector2D f) { force.offset(f); }
a14f26f4-ba1b-4dfb-a0bc-4bd8b5f2b739
@Override public Vector2D getVel() { return vel; }
cb2facbe-8c5e-4bd8-8cb7-6044be547cb7
@Override public void setVel(Vector2D v) { vel = new Vector2D(v); }
3cedf7b8-8c78-40e4-adf0-38ae415d4647
@Override public void applyDrag(double seconds) { double factorX = 1 - DRAG_FACTOR * seconds; double factorY = 1 - (DRAG_FACTOR * DRAG_FACTOR_Y) * seconds; vel = new Vector2D(vel.x * factorX, vel.y * factorY); }
2b64f12f-29bd-4df3-95d0-495dec5a224c
@Override /** * Get the width and height of the entity. * @return A new dimension object holding the width and height. */ public Dimension getSize() { Dimension dim = super.getSize(); Dimension result = new Dimension((int) (dim.height * CBOX_SCALE), ...
64c7897a-453d-46ae-9579-fa382184f692
public boolean isOnGround() { return onGround; }
6e265b36-a326-4977-9a50-04e6916af032
public void setOnGround(boolean onGround) { this.onGround = onGround; }
075ad312-d7ce-4fc1-bfa0-ec05dee077f2
@Override public Rectangle getBounds() { Rectangle bounds = super.getBounds(); // bounds.x -= (bounds.width * (1 - CBOX_SCALE) / 2); // bounds.y -= (bounds.height * (1 - CBOX_SCALE) / 2); // bounds.width *= CBOX_SCALE; // bounds.height *= CBOX_SCALE; return bou...
1dd8494c-343f-4dfd-b34a-a0ac6871855d
@Override public Rectangle getCollisionBox() { Rectangle bounds = super.getBounds(); bounds.x += (bounds.width * (1 - CBOX_SCALE) / 2); bounds.y += (bounds.height * (1 - CBOX_SCALE) / 2); bounds.width *= CBOX_SCALE; bounds.height *= CBOX_SCALE; return bound...