summaryrefslogtreecommitdiff
path: root/public/assets/javascripts/rectangles/engine/scenery/randomize.js
diff options
context:
space:
mode:
authorJules Laplace <jules@okfoc.us>2014-10-01 15:42:21 -0400
committerJules Laplace <jules@okfoc.us>2014-10-01 15:42:21 -0400
commit7afac43f11d1ccae6f3b8d1febcd293db11bd2b7 (patch)
tree1907a9157b11417a98c9dd065be28a788345fd54 /public/assets/javascripts/rectangles/engine/scenery/randomize.js
parent3eacd977bb5df5d379e0ee9cc554af8f8f2a5b6c (diff)
randomly place images on the walls
Diffstat (limited to 'public/assets/javascripts/rectangles/engine/scenery/randomize.js')
-rw-r--r--public/assets/javascripts/rectangles/engine/scenery/randomize.js77
1 files changed, 77 insertions, 0 deletions
diff --git a/public/assets/javascripts/rectangles/engine/scenery/randomize.js b/public/assets/javascripts/rectangles/engine/scenery/randomize.js
new file mode 100644
index 0000000..4f1144a
--- /dev/null
+++ b/public/assets/javascripts/rectangles/engine/scenery/randomize.js
@@ -0,0 +1,77 @@
+/*
+ // get the list of media we want to place
+ var media_data = $(".mediaContainer").toArray().map(function(el){
+ return $(el).data("media")
+ })
+ Scenery.randomize( media_data )
+*/
+
+Scenery.randomize = function (media_data) {
+ var media_list = media_data.map(function(media){
+ var width, height
+ if (media.width > media.height) {
+ width = Math.min(300, media.width)
+ height = media.height/media.width * width
+ }
+ else {
+ height = Math.min(300, media.height)
+ width = media.width/media.height * height
+ }
+ return {
+ dimensions: new vec2( width, height ),
+ media: media,
+ }
+ })
+
+ // get a list of all walls
+ var walls = {}
+ Walls.forEach(function(wall){
+ walls[wall.id] = wall
+ })
+
+ // remove the walls that already have stuff on them
+ Scenery.forEach(function(scenery){
+ delete walls[scenery.wall.id]
+ })
+
+ var wall_ids = _.keys(walls)
+ if (! wall_ids.length) {
+ return
+ }
+
+ // randomize walls
+ shuffle(wall_ids)
+
+ // assign each of the media to the walls, until we run out of either
+ media_list.some(function(media){
+ if (wall_ids.length == 0) {
+ return true
+ }
+
+ var i, fits = -1
+
+ for (i = 0; i < wall_ids.length; i++) {
+ if (walls[wall_ids[i]].surface.fits(media.dimensions)) {
+ // walls[wall_ids[i]]
+ fits = i
+ break
+ }
+ }
+
+ if (fits != -1) {
+ var wall = walls[wall_ids[fits]]
+ wall_ids.splice(fits, 1)
+
+ Scenery.add({
+ media: media.media,
+ wall: wall,
+ index: 0,
+ })
+ }
+ else {
+ // artwork won't fit anywhere??
+ }
+
+ return false
+ })
+} \ No newline at end of file