forked from SodiumFRP/sodium
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Stephen Blackheath
committed
Feb 12, 2015
1 parent
ce1beea
commit a5b4d85
Showing
10 changed files
with
406 additions
and
199 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import javax.imageio.*; | ||
import javax.swing.*; | ||
import java.io.IOException; | ||
import java.net.MalformedURLException; | ||
import java.net.URL; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.awt.Dimension; | ||
import java.awt.Graphics; | ||
import java.awt.Point; | ||
import java.awt.Toolkit; | ||
import java.awt.image.*; | ||
import java.awt.event.WindowAdapter; | ||
import java.awt.event.WindowEvent; | ||
import sodium.*; | ||
|
||
public class Animate extends JPanel { | ||
private final double t0 = 0.0; | ||
private final Dimension windowSize = new Dimension(600, 400); | ||
private final BufferedImage sapienImgL; | ||
private final BufferedImage sapienImgR; | ||
private final BufferedImage zombicusImgL; | ||
private final BufferedImage zombicusImgR; | ||
private final BufferedImage coneImg; | ||
private Cell<List<Character>> characters; | ||
private CellSink<Double> clock; | ||
private StreamSink<Unit> sTick; | ||
|
||
public interface Animation { | ||
public Cell<List<Character>> create( | ||
double t0, Cell<Double> clock, Stream<Unit> sTick, | ||
Dimension screenSize); | ||
} | ||
|
||
public Animate(Animation animation) | ||
throws MalformedURLException, IOException | ||
{ | ||
URL rootURL = new URL("file:."); | ||
sapienImgL = ImageIO.read(new URL(rootURL, "../images/homo-sapien-left.png")); | ||
sapienImgR = ImageIO.read(new URL(rootURL, "../images/homo-sapien-right.png")); | ||
zombicusImgL = ImageIO.read(new URL(rootURL, "../images/homo-zombicus-left.png")); | ||
zombicusImgR = ImageIO.read(new URL(rootURL, "../images/homo-zombicus-right.png")); | ||
coneImg = ImageIO.read(new URL(rootURL, "../images/roadius-conium.png")); | ||
Transaction.runVoid(() -> { | ||
clock = new CellSink<Double>(t0); | ||
sTick = new StreamSink<Unit>(); | ||
this.characters = animation.create(t0, clock, sTick, windowSize); | ||
}); | ||
} | ||
|
||
public void paintComponent(Graphics g) { | ||
super.paintComponent(g); | ||
Transaction.runVoid(() -> { | ||
List<Character> chars = new ArrayList<Character>(characters.sample()); | ||
chars.sort((a, b) -> a.pos.y == b.pos.y ? 0 : | ||
a.pos.y < b.pos.y ? -1 : 1); | ||
for (Character c : chars) { | ||
if (c.type == CharacterType.SAPIEN) | ||
if (c.velocity.dx < 0) | ||
g.drawImage(sapienImgL, c.pos.x-30, c.pos.y-73, null); | ||
else | ||
g.drawImage(sapienImgR, c.pos.x-23, c.pos.y-73, null); | ||
else | ||
if (c.velocity.dx < 0) | ||
g.drawImage(zombicusImgL, c.pos.x-39, c.pos.y-73, null); | ||
else | ||
g.drawImage(zombicusImgR, c.pos.x-23, c.pos.y-73, null); | ||
} | ||
}); | ||
Toolkit.getDefaultToolkit().sync(); | ||
} | ||
|
||
public Dimension getPreferredSize() { | ||
return windowSize; | ||
} | ||
|
||
public static void animate(String title, Animation animation) | ||
{ | ||
try { | ||
JFrame frame = new JFrame(title); | ||
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | ||
Animate view = new Animate(animation); | ||
frame.setContentPane(view); | ||
frame.pack(); | ||
frame.setVisible(true); | ||
long t0 = System.currentTimeMillis(); | ||
long tLast = t0; | ||
while (true) { | ||
long t = System.currentTimeMillis(); | ||
long tIdeal = tLast + 20; | ||
long toWait = tIdeal - t; | ||
if (toWait > 0) | ||
try { Thread.sleep(toWait); } catch (InterruptedException e) {} | ||
view.clock.send((double)(tIdeal - t0) * 0.001); | ||
view.sTick.send(Unit.UNIT); | ||
view.repaint(0); | ||
tLast = tIdeal; | ||
} | ||
} | ||
catch (MalformedURLException e) { | ||
e.printStackTrace(); | ||
} | ||
catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import java.awt.Point; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.Random; | ||
import sodium.*; | ||
|
||
public class BitableHomoSapien { | ||
private static class All { | ||
All(Character character, double t) { | ||
this.character = character; | ||
this.t = t; | ||
} | ||
Character character; | ||
double t; | ||
} | ||
public BitableHomoSapien( | ||
World world, | ||
int self, | ||
double tInit, | ||
Point posInit, | ||
Cell<Double> clock, | ||
Stream<Unit> sTick, | ||
Stream<Integer> sBite, | ||
Cell<List<Character>> others) | ||
{ | ||
HomoSapien h = new HomoSapien(world, self, tInit, posInit, clock, sTick); | ||
Stream<Integer> sBiteMe = sBite.filter(id -> id == self); | ||
Cell<All> all = Cell.lift( | ||
(ch, t) -> new All(ch, t), | ||
h.character, clock); | ||
Stream<HomoZombicus> sBecome = sBiteMe.snapshot( | ||
all, | ||
(id, a) -> new HomoZombicus( | ||
world, self, | ||
a.t, a.character.pos, | ||
clock, | ||
sTick, others | ||
) | ||
); | ||
this.character = Cell.switchC( | ||
sBecome.map(z -> z.character).hold(h.character) | ||
); | ||
this.sBite = Cell.switchS( | ||
sBecome.map(z -> z.sBite).hold(new Stream<Integer>()) | ||
); | ||
} | ||
public final Cell<Character> character; | ||
public final Stream<Integer> sBite; | ||
} | ||
|
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.