-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflow.php
More file actions
4605 lines (3499 loc) · 283 KB
/
flow.php
File metadata and controls
4605 lines (3499 loc) · 283 KB
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
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<div id="svgContainerCreatures">
</div>
<div id="svgContainerUI">
</div>
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="450px" height="250px">
<svg id="svg1" onload="Start(evt)" height="250" version="1.1" width="450" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<script type="text/ecmascript">
<![CDATA[
//This begins the JavaScript code for LifeFLOW! :)
// Start(evt);
var time = 0;
var delta_time = 25;
var max_time = 1000;
var dir = 1;
var dir_purple_spiral = 1;
var opacity_ps = .9;
var lifetime = 0; // overall time elapsing.
var active_area = "creatures/avril3.svg"; //area for saving and loading in main view.
var active_tool = "Touch"; //tool for playing/interacting with creaturez! :)
//eventually p set default to Inspect
var test12345 = "debugdata"; //test stuff
var test23456 = "debugdata2"; //test stuff
// php interferes with local testing w/o a server, so comment out the URL GET area for local-only testing...
// eventually can mv this stuff into e.g. open.php, or craft an alternative solution...
//check URL for default area to load
//if filexists w/ name "creatures/$(GET).svg" then set that as active_area...
<?php
//set up the file path from the URL "area" parameter
//e.g. "/index.php?area=eagle5" opens "creatures/eagle5.svg"
$area = 'creatures/';
$area .= $_GET['area'];
$area .= '.svg';
//check if $area exists, and if so set it as active_area
if (file_exists($area)) {
echo 'active_area = "'.$area.'";';
}
?>
var the_rect;
var pink_rect;
var eg_niblet;
var eg_medy;
var eg_biggo;
var eg_newy;
var purple_spiral;
var avril_niblet_1;
var eaglynew;
var eg_newy2; // how's this? :)
// do we need to declare satellites 3 and 4?
var satellite3;
var satellite4;
var bg;
var creatures = svg1;
//array of clones! :)
var clone_array = [];
var clone_index = 0; //keep track of clones.
//array of creatures! :)
var creature_array = [];
var creature_index = 0; //keep track of creatures.
// var creatures_by_class = 0; // this should m be merged w/ the above creature_index...
// var area_array = []; // map of areas for navigator
var area_array = new Array(10); // map of areas for navigator
var navigator_open = false; // 0: area navigator closed. 1: area navigator open.
var species_array = new Array(10); // map of creatures for browser
var browser_open = false; // 0: creature browser closed. 1: creature browser open.
var xmlns = "http://www.w3.org/2000/svg" // set URI for SVG NameSpace
var coolMode = 0; // give cool creature a mode, default to 0
var staticMode = 0; // give static creature a mode, default to 0
var psyMode = 0; // these are kinda hacky and should p be done differently
var evoMode = 0; // and another...
var nextMode = 0; // I think we should replace these...
function Start(evt) {
//Start the mechanical magic!!! :)
LoadCreatures();
// LoadUI();
UpdateCreatures(evt);
// Set vars from dynamically loaded SVG
Oscillate();
// move the creatures!!! :)
}
function UpdateCreatures(evt){
//create vars from ajax svg creatures! :)
eaglynew = document.getElementById("eaglynew");
the_rect = document.getElementById("mediumcreature3");
pink_rect = document.getElementById("niblet3");
eg_niblet = document.getElementById("planet3");
eg_medy = document.getElementById("planet4");
eg_biggo = document.getElementById("bigbeauty3");
// eg_biggo = evt.target.ownerDocument.getElementById("BiggoEG");
eg_newy = document.getElementById("galaxy3");
eg_newy2 = document.getElementById("galaxy4");
satellite3 = document.getElementById("satellite3");
satellite4 = document.getElementById("satellite4");
back3 = document.getElementById("back3");
purple_spiral = document.getElementById("PurpleSpiral");
avril_niblet_1 = document.getElementById("AvrilNiblet1");
// TODO; loop through clone_array[] and get var from svg...
// p do sthg like "for each creature in svg, create a var..."
// Also same for creature_array[]? probably yes
//first I think we should clear the previously user-created creatures
// creature_array = [];
creature_array.length = 0; // different approach that deletes existing array contents instead of creating new array.
//then load them anew...
// creature_index = 0; // go back to beginning - is this necessary? probably yes. actually I think it shouldn't be with the below.
// creature_array[creature_index] = document.getElementById("creature_name"); // add creature to js from svg...
// i think we'll need to carefully loop through each svg element...
var creatures_by_class = document.getElementsByClassName("creatureClass"); // should create an array containing all creatures...
//var k; ...for now we're using creature_index instead, I think this should work... :)
for (creature_index = 0; creature_index < creatures_by_class.length; creature_index++) {
creature_array[creature_index] = creatures_by_class[creature_index];
}
// the_rect = evt.target.ownerDocument.getElementById("mediumcreature3");
// pink_rect = evt.target.ownerDocument.getElementById("niblet3");
// eg_niblet = evt.target.ownerDocument.getElementById("planet3");
// eg_medy = evt.target.ownerDocument.getElementById("planet4");
// eg_biggo = evt.target.ownerDocument.getElementById("bigbeauty3");
// eg_biggo = evt.target.ownerDocument.getElementById("BiggoEG");
// eg_newy = evt.target.ownerDocument.getElementById("galaxy3");
// eg_newy2 = evt.target.ownerDocument.getElementById("galaxy4");
// satellite3 = evt.target.ownerDocument.getElementById("satellite3");
// satellite4 = evt.target.ownerDocument.getElementById("satellite4");
// back3 = evt.target.ownerDocument.getElementById("back3");
// purple_spiral = document.getElementById("PurpleSpiral");
// avril_niblet_1 = evt.target.ownerDocument.getElementById("AvrilNiblet1");
}
function LoadCreatures() {
//Download creatures from server via AJAX requests.
//Could maybe be combined with OpenArea()
xhr = new XMLHttpRequest();
// xhr.open("GET","creatures/0123456789.svg",false);
// xhr.open("GET","creatures/allcreatures11.svg",false); //load dynamically! :)
// xhr.open("GET","creatures/avril2.svg",false); //load dynamically! :)
// var creatures_path = "creatures/"+active_area+".svg";
xhr.open("GET",active_area + ((/\?/).test(active_area) ? "&" : "?") + "time="+(new Date()).getTime(),false); //load dynamically! :)
// Following line is just to be on the safe side;
// not needed if your server delivers SVG with correct MIME type
xhr.overrideMimeType("image/svg+xml");
xhr.send("");
document.getElementById("svgContainerCreatures")
.appendChild(xhr.responseXML.documentElement);
}
function LoadUI() {
//Loads the user interface. The UI itself should be moved out of here and into its own file.
//SO what's in ui2.svg???
xhr = new XMLHttpRequest();
xhr.open("GET","ui/ui2.svg",false);
// Following line is just to be on the safe side;
// not needed if your server delivers SVG with correct MIME type
xhr.overrideMimeType("image/svg+xml");
xhr.send("");
document.getElementById("svgContainerUI")
.appendChild(xhr.responseXML.documentElement);
}
function OpenArea(area) {
// Go to a new area! :)
// basically, switch the active_area, then replaceChild the svg
// var new_area = "creatures/avril7.svg"; //this was around but I think now obsolete
navigator_open = false; // set toggle off.
// active_area = "creatures/avril7.svg"; // new active area
active_area = area; // new active area
var xhr_new = new XMLHttpRequest();
xhr_new.open("GET",active_area + ((/\?/).test(active_area) ? "&" : "?") + "time="+(new Date()).getTime(),false); //load dynamically! :)
// btw the test above is probably unecessary, as it should already be loading e.g. "...?area=Area-01", although it worx! :)
xhr_new.overrideMimeType("image/svg+xml");
xhr_new.send("");
document.getElementById("svgContainerCreatures")
.replaceChild(xhr_new.responseXML.documentElement,document.getElementById("svgContainerCreatures").childNodes[1]);
// .replaceChild(xhr_new.responseXML.documentElement,document.getElementById("svgContainerCreatures").lastElementChild);
//insert the dynamically loaded svg where the previous svg was
UpdateCreatures(); //Set them in motion again!!! :)
}
function toggle_visibility(id) {
// utility function to show/hide components of the site.
// in use for example to show/hide the contribution buttonz! :)
var toggleElement = document.getElementById(id); // use id as HTML element to show/hide
if (toggleElement.style.display != 'none')
toggleElement.style.display = 'none';
else
toggleElement.style.display = 'table-cell';
//the above toggles the element on/off. we may instead want to replace in the DOM between a Contribute blue/green button, and the actual contribution buttonz! :)
}
function showContribute() {
// show the contribution buttonz! :)
// for now anywayz we're just using toggle_visibility() instead, which we'll probably keep! :)
// build up the contribute text
// replace the placeholder with the full contribute text we've built above! :)
document.getElementById("contribute")
.replaceChild(document.getElementById("contributeFull").childNodes[1],document.getElementById("contributePlaceholder").childNodes[1]);
}
function dw_encodeVars(params) {
var str = '';
for (var i in params ) {
str += encodeURIComponent(i) + '=' + encodeURIComponent( params[i] ) + '&';
}
return str.slice(0, -1);
}
function PrepareCreatures() {
//convert creatures to proper file format for saving .svg in SaveCreatures()
//get the creaturez meta template from server
//php xhr_m1 = new XMLHttpRequest();
//php xhr_m1.open("GET","creatures/creaturez-meta.svg",false);
// Following line is just to be on the safe side;
// not needed if your server delivers SVG with correct MIME type
//php xhr_m1.overrideMimeType("image/svg+xml");
//php xhr_m1.send("");
//put together the creaturez into XML format for writing
// var creaturez_pre = xhr_m1.responseXML.documentElement + eaglynew; // combine meta with creaturez;
// var XMLS = new XMLSerializer(); //to convert creature DOMz into text for sending to save.php form
// var creaturez_xml = XMLS.serializeToString(creaturez_pre); // convert meta and creaturez to text.
// creaturez_xml += "</svg>"; // close svg
// var creaturez_xml = xhr_m1.responseXML.documentElement; // add basic svg outline
// creaturez_xml += XMLS.serializeToString(eaglynew); // add creaturez
var XMLS = new XMLSerializer(); //to convert creature DOMz into text for sending to save.php form
// var XMLS2 = new XMLSerializer(); //to convert creature DOMz into text for sending to save.php form
//php var meta_xml = XMLS.serializeToString(xhr_m1.responseXML.documentElement); // serialize meta
//php var creaturez_xml = meta_xml; // add basic svg outline
// var creaturez_xml = xhr_m1.responseXML.documentElement; // add basic svg outline
creaturez_xml = XMLS.serializeToString(eaglynew); // add creaturez
creaturez_xml += XMLS.serializeToString(avril_niblet_1); // add creaturez */
creaturez_xml += XMLS.serializeToString(purple_spiral); // add creaturez
creaturez_xml += XMLS.serializeToString(back3); // add creaturez
creaturez_xml += XMLS.serializeToString(eg_newy); // add creaturez
creaturez_xml += XMLS.serializeToString(eg_newy2); // add creaturez
creaturez_xml += XMLS.serializeToString(eg_niblet); // add creaturez
creaturez_xml += XMLS.serializeToString(eg_medy); // add creaturez
creaturez_xml += XMLS.serializeToString(satellite3); // add creaturez
creaturez_xml += XMLS.serializeToString(satellite4); // add creaturez
creaturez_xml += XMLS.serializeToString(the_rect); // add creaturez
creaturez_xml += XMLS.serializeToString(pink_rect); // add creaturez
creaturez_xml += XMLS.serializeToString(eg_biggo); // add creaturez
for (i = 0; i < creature_index; i++) creaturez_xml += XMLS.serializeToString(creature_array[i]); // add user-created creatures
//php creaturez_xml += "</svg>" // close svg
//add clones too...
//and new creatures...
// for i = 0 to array index, serializeToString creature/clone[i]
return creaturez_xml;
}
function SaveCreatures() {
//creatures = svg1;
//$var = $_POST['foo'];
//file_put_contents('file_where_stored_value_is.php', $var);
// creaturezready = eaglynew.childNodes;
// areaready = PrepareArea(); //prepare area to save
creaturezready = PrepareCreatures(); //prepare creaturez to save
xhr = new XMLHttpRequest(); //create new request
// xhr.open("GET","save.php?tub=testnewtub4&creaturez=SVG headers...: " + creaturezready); //"creatures/1234567.svg"); //what goes in 2nd/3rd params? "index.php"? "creatures/1234567"? ...?
xhr.open("POST","save.php"); //"creatures/1234567.svg"); //what goes in 2nd/3rd params? "index.php"? "creatures/1234567"? ...?
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
var data = dw_encodeVars({area:active_area, creaturez:creaturezready}); // set up data to POST.
xhr.send(data);
//can js write the php? pn
// devsave = xhr; // test var
}
function setTool(tool){
//Set the active tool for interacting with creaturez! :)
//Unhighlight the previously active tool's icon.
document.getElementById(active_tool).setAttribute('fill-opacity','0.75');
//Set the selected tool as active.
active_tool = tool;
//Highlight the newly active tool's icon.
document.getElementById(tool).setAttribute('fill-opacity','0.99');
}
function tool(creature) {
//Use the active tool on the selected creature! :)
//eventually switch to a case statement or whatever...
//document.getElementById(active_tool)(creature);...
//basically run the active_tool on the creature
if (active_tool == "Touch") touch(creature); //touch(creature);
if (active_tool == "Clone") clone(creature); //clone(creature);
if (active_tool == "Create") create(creature); //create(creature);
if (active_tool == "Delete") deleteCreature(creature); //create(creature);
/* if (active_tool == "Download") touch(creature); //touch(creature);
if (active_tool == "Upload") touch(creature); //touch(creature);
if (active_tool == "Group") touch(creature); //touch(creature);
if (active_tool == "Ungroup") touch(creature); //touch(creature);
if (active_tool == "etc.") touch(creature); //touch(creature);
if (active_tool == "...") touch(creature); //touch(creature);
*/
}
function Move(creature, x, y) {
creature.setAttribute("transform", "translate(x, y)");
}
function clone(creature) {
//Clone a creature! :)
//This was originally written with reference to var creature. For reference to creature_name instead, check for "Creature-" at beginning.
var re_creature = /^Creature-/; //regex matching creature starting with "Creature-"
if (re_creature.test(creature) == 1) {creature = document.getElementById(creature);} //Set creature_names to their creature var
var clone_name = creature.getAttribute("id")+"-"+Math.random(); //e.g. "the_rect-0.17239898123"
clone_array[clone_index] = creature.cloneNode(true); // can we give the new clone a var on the basis of clone_name? let it use array instead.
clone_array[clone_index].setAttribute("id",clone_name); // give the new clone a different id.
clone_array[clone_index].setAttribute("transform","translate(-40,20)"); // translate the new clone. SHOULD P CHANGE WHERE EACH GOES
clone_array[clone_index].setAttribute("onmousedown","deleteClone('"+clone_name+"')"); // Self-destruct! :) although that would override other toolz... ok for now, later would ideally deal w/ clones more elegantly...
// insert clone_index into clone, for ease of deletion.
// clone_array[clone_index].setAttribute("clone_index",clone_index); // insert clone_index into clone, for ease of deletion.
// or should it be : "onmousedown=deleteClone(clone_index)"...? py...
document.getElementById("svg2")
.insertBefore(clone_array[clone_index], creature); // add the new clone into the inline svg (to get written automatically to file later)
clone_index++; // go to next clone
}
function deleteClone(clone_name) {
//or m use (creature)...
document.getElementById("svg2") // rm clone from inline svg
.removeChild(document.getElementById(""+clone_name+"")); //um...
// .removeChild(clone_array[clone_index]); //um...
}
function givePersonality(creature_name) {
//assign a personality (set of behaviors) to a creature
//insert a set of steps into SVG <desc>
//should yield basically eval("personalitycode")
//e.g. document.getElementById(creature_name).setAttribute("transform", "translate(3,8), rotate(90)";
document.getElementById(creature_name).setAttribute("transform", "translate(3,5)"); // transform the creature
//could assign the above to a var and later eval() it if necessary
}
function moveCreature(creature_name, x, y) {
//another take at improving creature personality
//do any preprocessing
x = x + document.getElementById(creature_name).getBoundingClientRect().x / 2 + 25*Math.random(); // update x
y = y + document.getElementById(creature_name).getBoundingClientRect().y / 10 + 50*Math.random(); // update y
/*orig x = x + document.getElementById(creature_name).getBoundingClientRect().x + 5; // update x
y = y + document.getElementById(creature_name).getBoundingClientRect().y + 5; // update y
*/
//translate creature_name x y
document.getElementById(creature_name).setAttribute("transform", "translate("+x+", "+y+"), rotate("+x*y/38+")"); // transform the creature
//now just make it relative to current position or o creature etc., instead of origin.
}
function getNearestNeighbor(creature_name) {
var distanceMeasure; // check how far apart creature_name is from o creatures
var distanceShortest = 10000000000000000000000; // tmp store the shortest distance while iterating through - should be 1000 but testing issues (was really big, now 1,000,000 just in case)
var nearestNeighbor; //default to self = creature_array.indexOf(creature_name); // store the nearest neighbor
//find the nearest other creature to creature_name
//test if (creature_index > 1) {
//if any other creatures around, calculate distances and keep the smallest
for (var i = 0; i < creature_index; i++) {
//iterate through creatures
//orig distanceMeasure = 5*Math.random(); // calculate distance, probably something like Math.sqrt(Math.square(x)+Math.square(y))..
distanceMeasure = (Math.abs(document.getElementById(creature_name).getBoundingClientRect().x - creature_array[i].getBoundingClientRect().x) + Math.abs(document.getElementById(creature_name).getBoundingClientRect().y - creature_array[i].getBoundingClientRect().y)); // calculate x diff
//btw what would diff x plus diff y give???
// 5*Math.random(); // calculate distance, probably something like Math.sqrt(Math.square(x)+Math.square(y))..
if ((distanceMeasure < distanceShortest) && (i != creature_array.indexOf(document.getElementById(creature_name)))) {
//if this is the shortest distance so far, then make this the return creature.
distanceShortest = distanceMeasure; //set the new shortest distance
nearestNeighbor = i; //store the index of the new shortest distance
}
}
return nearestNeighbor; //should we return the index, or the whole creature?
// } // if (creature_index > 1)
} // getNearestNeighbor()
function moveExtralCreature(creature_name, x, y) {
//another take at improving creature personality
//do any preprocessing
//here we should probably check for nearby creatures, and move somewhere along the way to the closest.
if (creature_index > 1) {
//if any other creatures, find the nearest creature and chase it! :)
var nearestNeighbor = getNearestNeighbor(creature_name); // get (index of) nearbyest neighbor
//getNearestNeighbor(creature_name);
//iterate through creature_array[] and measure distance difference
//measure vector distance, only keep the smallest
//mv to a location between them
x = (document.getElementById(creature_name).getBoundingClientRect().x - creature_array[nearestNeighbor].getBoundingClientRect().x) / 2 * Math.random();// mv somewhere between creature_name and nearestNeighbor
y = (document.getElementById(creature_name).getBoundingClientRect().y - creature_array[nearestNeighbor].getBoundingClientRect().y) / 2 * Math.random();// mv somewhere between creature_name and nearestNeighbor
} //if other creatures (else just wander randomly)
else {
//update x and y
x = x + document.getElementById(creature_name).getBoundingClientRect().x / 2 + 2*Math.random(); // update x
y = y + document.getElementById(creature_name).getBoundingClientRect().y / 1000 + 10*Math.random(); // update y
/*orig x = x + document.getElementById(creature_name).getBoundingClientRect().x + 5; // update x
y = y + document.getElementById(creature_name).getBoundingClientRect().y + 5; // update y
*/
} // else wander around if no other creatures nearby
//translate creature_name x y
document.getElementById(creature_name).setAttribute("transform", "translate("+x+", "+y+")"); // transform the creature
//now just make it relative to current position or o creature etc., instead of origin.
}
function moveSmoothyCreature(creature_name, x, y) {
//another take at improving creature personality
//do any preprocessing
//here we should probably check for nearby creatures, and move somewhere along the way to the closest.
if (creature_index > 1) {
//if any other creatures, find the nearest creature and chase it! :)
var nearestNeighbor = getNearestNeighbor(creature_name); // get (index of) nearbyest neighbor
//getNearestNeighbor(creature_name);
//iterate through creature_array[] and measure distance difference
//measure vector distance, only keep the smallest
//mv to a location between them
test12345 = (document.getElementById(creature_name).getBoundingClientRect().x);
test23456 = creature_array[nearestNeighbor];//(creature_array[nearestNeighbor].getBoundingClientRect().x);
//test12345 = (document.getElementById(creature_name).getBoundingClientRect().x - creature_array[nearestNeighbor].getBoundingClientRect().x);
//debug if (Math.random() < 0.5) {x = document.getElementById(creature_name).getBoundingClientRect().x + 0.001} else {x = document.getElementById(creature_name).getBoundingClientRect().x - 0.001};
//debug if (Math.random() < 0.5) {y = document.getElementById(creature_name).getBoundingClientRect().y + 0.001} else {y = document.getElementById(creature_name).getBoundingClientRect().y - 0.001};
//new if ((document.getElementById(creature_name).getBoundingClientRect().x - creature_array[nearestNeighbor].getBoundingClientRect().x) < 0) {x = document.getElementById(creature_name).getBoundingClientRect().x * 0.001} else {x = document.getElementById(creature_name).getBoundingClientRect().x - 0.001};
//new if ((document.getElementById(creature_name).getBoundingClientRect().y - creature_array[nearestNeighbor].getBoundingClientRect().y) < 0) {y = document.getElementById(creature_name).getBoundingClientRect().y / 0.001} else {y = document.getElementById(creature_name).getBoundingClientRect().y - 0.001};
x = ((document.getElementById(creature_name).getBoundingClientRect().x + creature_array[nearestNeighbor].getBoundingClientRect().x) / 2 * Math.random());// mv somewhere between creature_name and nearestNeighbor
y = ((document.getElementById(creature_name).getBoundingClientRect().y + creature_array[nearestNeighbor].getBoundingClientRect().y) / 2 * Math.random());// mv somewhere between creature_name and nearestNeighbor
} //if other creatures (else just wander randomly)
else {
//update x and y
x = x * document.getElementById(creature_name).getBoundingClientRect().x / 5 + 20*Math.random(); // update x
y = y + document.getElementById(creature_name).getBoundingClientRect().y / 2 + 10*Math.random(); // update y
/*orig x = x + document.getElementById(creature_name).getBoundingClientRect().x + 5; // update x
y = y + document.getElementById(creature_name).getBoundingClientRect().y + 5; // update y
*/
} // else wander around if no other creatures nearby
//translate creature_name x y
document.getElementById(creature_name).setAttribute("transform", "translate("+x+", "+y+")"); // transform the creature
//now just make it relative to current position or o creature etc., instead of origin.
}
function moveStaticCreature(creature_name, x, y) {
//debug var blah = "testblah";
/*
// Figure out nearest neighboring creature! :)
if (creature_index > 1) {
var nearestNeighbor = getNearestNeighbor(creature_name); // get (index of) nearbyest neighbor
// Getting existing translate X and Y, in order to apply subsequent X and Y
var xforms = document.getElementById(creature_name).transform.baseVal; // An SVGTransformList
var firstXForm = xforms.getItem(0); // An SVGTransform
if (firstXForm.type == SVGTransform.SVG_TRANSFORM_TRANSLATE){
var firstX = firstXForm.matrix.e,
firstY = firstXForm.matrix.f;
}
if (staticMode == 0) {
//if way out of bounds then mv back
if ((document.getElementById(creature_name).getBoundingClientRect().right > 450) || (document.getElementById(creature_name).getBoundingClientRect().bottom > 450)) {staticMode = 1};
if (creature_array[nearestNeighbor].getBoundingClientRect().x - document.getElementById(creature_name).getBoundingClientRect().x > 0) {
x = firstX + 3 * Math.random();
y = firstY + 3 * Math.random();
}
else if (creature_array[nearestNeighbor].getBoundingClientRect().y - document.getElementById(creature_name).getBoundingClientRect().y < 0) {
x = firstX - 3 * Math.random();
y = firstY - 3 * Math.random();
}
else {
//they're touching! :)
x = firstX + 6 * (Math.random() - .5);
y = firstY + 6 * (Math.random() - .5);
}
//if way out of bounds then mv back
//probably move this or something like it into a more general creature-checking thing for area boundaryz! :)
if (document.getElementById(creature_name).getBoundingClientRect().right > 450) {x -= 200*Math.random()};
if (document.getElementById(creature_name).getBoundingClientRect().left < 0) {x += 200*Math.random()};
if (document.getElementById(creature_name).getBoundingClientRect().bottom > 450) {y -= 200*Math.random()};
if (document.getElementById(creature_name).getBoundingClientRect().top < 0) {y += 200*Math.random()};
}
else if (staticMode == 1) {
//if way out of bounds, mv bak towards center
x = firstX - 13;
y = firstY - 13;
if ((document.getElementById(creature_name).getBoundingClientRect().x < 450) && (document.getElementById(creature_name).getBoundingClientRect().y < 450)) {
staticMode = 0; //reset mode
}
}
}
//translate creature_name x y
pivot_x = (document.getElementById(creature_name).getBoundingClientRect().left + document.getElementById(creature_name).getBoundingClientRect().right) / 2; // average coordinates for pivot around middle.
pivot_y = (document.getElementById(creature_name).getBoundingClientRect().top + document.getElementById(creature_name).getBoundingClientRect().bottom) / 2; // average coordinates for pivot around middle.
document.getElementById(creature_name).setAttribute("transform", "translate("+x+", "+y+") rotate("+x/y*(Math.random()-0.5)+" "+pivot_x+" "+pivot_y+")"); // transform the creature
static creatures do not move.
*/
} //moveStaticCreature()
function moveSpikyCreature(creature_name, x, y) {
//debug var blah = "testblah";
// Figure out nearest neighboring creature! :)
if (creature_index > 1) {
var nearestNeighbor = getNearestNeighbor(creature_name); // get (index of) nearbyest neighbor
// Getting existing translate X and Y, in order to apply subsequent X and Y
var xforms = document.getElementById(creature_name).transform.baseVal; // An SVGTransformList
var firstXForm = xforms.getItem(0); // An SVGTransform
if (firstXForm.type == SVGTransform.SVG_TRANSFORM_TRANSLATE){
var firstX = firstXForm.matrix.e,
firstY = firstXForm.matrix.f;
}
if (staticMode == 0) {
//if way out of bounds then mv back
if ((document.getElementById(creature_name).getBoundingClientRect().right > 450) || (document.getElementById(creature_name).getBoundingClientRect().bottom > 450)) {staticMode = 1};
if (creature_array[nearestNeighbor].getBoundingClientRect().x - document.getElementById(creature_name).getBoundingClientRect().x > 0) {
x = firstX + 3 * Math.random();
y = firstY + 3 * Math.random();
}
else if (creature_array[nearestNeighbor].getBoundingClientRect().y - document.getElementById(creature_name).getBoundingClientRect().y < 0) {
x = firstX - 3 * Math.random();
y = firstY - 3 * Math.random();
}
else {
//they're touching! :)
x = firstX + 6 * (Math.random() - .5);
y = firstY + 6 * (Math.random() - .5);
}
//if way out of bounds then mv back
//probably move this or something like it into a more general creature-checking thing for area boundaryz! :)
if (document.getElementById(creature_name).getBoundingClientRect().right > 450) {x -= 200*Math.random()};
if (document.getElementById(creature_name).getBoundingClientRect().left < 0) {x += 200*Math.random()};
if (document.getElementById(creature_name).getBoundingClientRect().bottom > 450) {y -= 200*Math.random()};
if (document.getElementById(creature_name).getBoundingClientRect().top < 0) {y += 200*Math.random()};
}
else if (staticMode == 1) {
//if way out of bounds, mv bak towards center
x = firstX - 13;
y = firstY - 13;
if ((document.getElementById(creature_name).getBoundingClientRect().x < 450) && (document.getElementById(creature_name).getBoundingClientRect().y < 450)) {
staticMode = 0; //reset mode
}
}
}
//translate creature_name x y
pivot_x = (document.getElementById(creature_name).getBoundingClientRect().left + document.getElementById(creature_name).getBoundingClientRect().right) / 2; // average coordinates for pivot around middle.
pivot_y = (document.getElementById(creature_name).getBoundingClientRect().top + document.getElementById(creature_name).getBoundingClientRect().bottom) / 2; // average coordinates for pivot around middle.
document.getElementById(creature_name).setAttribute("transform", "translate("+x+", "+y+") rotate("+x/y*(Math.random()-0.5)+" "+pivot_x+" "+pivot_y+")"); // transform the creature
} //moveSpikyCreature()
function moveLifePathCreature(creature_name, x, y) {
//debug var blah = "testblah";
// Figure out nearest neighboring creature! :)
if (creature_index > 1) {
var nearestNeighbor = getNearestNeighbor(creature_name); // get (index of) nearbyest neighbor
// Getting existing translate X and Y, in order to apply subsequent X and Y
var xforms = document.getElementById(creature_name).transform.baseVal; // An SVGTransformList
var firstXForm = xforms.getItem(0); // An SVGTransform
if (firstXForm.type == SVGTransform.SVG_TRANSFORM_TRANSLATE){
var firstX = firstXForm.matrix.e,
firstY = firstXForm.matrix.f;
}
if (staticMode == 0) {
//if way out of bounds then mv back
if ((document.getElementById(creature_name).getBoundingClientRect().right > 450) || (document.getElementById(creature_name).getBoundingClientRect().bottom > 450) || (document.getElementById(creature_name).getBoundingClientRect().top < 0) || (document.getElementById(creature_name).getBoundingClientRect().left < 0)) {staticMode = 1};
// if ((document.getElementById(creature_name).getBoundingClientRect().right > 450) || (document.getElementById(creature_name).getBoundingClientRect().bottom > 450)) {staticMode = 1};
if (creature_array[nearestNeighbor].getBoundingClientRect().x - document.getElementById(creature_name).getBoundingClientRect().x > 0) {
x = firstX + 3 * Math.random();
y = firstY + 3 * Math.random();
}
else if (creature_array[nearestNeighbor].getBoundingClientRect().y - document.getElementById(creature_name).getBoundingClientRect().y < 0) {
x = firstX - 3 * Math.random();
y = firstY - 3 * Math.random();
}
else {
//they're touching! :)
x = firstX + 6 * (Math.random() - .5);
y = firstY + 6 * (Math.random() - .5);
}
//if way out of bounds then mv back
//probably move this or something like it into a more general creature-checking thing for area boundaryz! :)
if (document.getElementById(creature_name).getBoundingClientRect().right > 450) {x -= 200*Math.random()};
if (document.getElementById(creature_name).getBoundingClientRect().left < 0) {x += 200*Math.random()};
if (document.getElementById(creature_name).getBoundingClientRect().bottom > 450) {y -= 200*Math.random()};
if (document.getElementById(creature_name).getBoundingClientRect().top < 0) {y += 200*Math.random()};
}
else if (staticMode == 1) {
//if way out of bounds, mv bak towards center
x = firstX - 13;
y = firstY - 13;
if ((document.getElementById(creature_name).getBoundingClientRect().x < 450) && (document.getElementById(creature_name).getBoundingClientRect().y < 450)) {
staticMode = 0; //reset mode
}
}
}
//translate creature_name x y
pivot_x = (document.getElementById(creature_name).getBoundingClientRect().left + document.getElementById(creature_name).getBoundingClientRect().right) / 2; // average coordinates for pivot around middle.
pivot_y = (document.getElementById(creature_name).getBoundingClientRect().top + document.getElementById(creature_name).getBoundingClientRect().bottom) / 2; // average coordinates for pivot around middle.
document.getElementById(creature_name).setAttribute("transform", "translate("+x+", "+y+") rotate("+x/y*(Math.random()-0.5)+" "+pivot_x+" "+pivot_y+")"); // transform the creature
} //moveLifePathCreature()
function moveLifePathCreature2(creature_name, x, y) {
//debug var blah = "testblah";
// Figure out nearest neighboring creature! :)
if (creature_index > 1) {
var nearestNeighbor = getNearestNeighbor(creature_name); // get (index of) nearbyest neighbor
// Getting existing translate X and Y, in order to apply subsequent X and Y
var xforms = document.getElementById(creature_name).transform.baseVal; // An SVGTransformList
var firstXForm = xforms.getItem(0); // An SVGTransform
if (firstXForm.type == SVGTransform.SVG_TRANSFORM_TRANSLATE){
var firstX = firstXForm.matrix.e,
firstY = firstXForm.matrix.f;
}
if (staticMode == 0) {
//if way out of bounds then mv back
if ((document.getElementById(creature_name).getBoundingClientRect().right > 450) || (document.getElementById(creature_name).getBoundingClientRect().bottom > 450) || (document.getElementById(creature_name).getBoundingClientRect().top < 0) || (document.getElementById(creature_name).getBoundingClientRect().left < 0)) {staticMode = 1};
// if ((document.getElementById(creature_name).getBoundingClientRect().right > 450) || (document.getElementById(creature_name).getBoundingClientRect().bottom > 450)) {staticMode = 1};
if (creature_array[nearestNeighbor].getBoundingClientRect().x - document.getElementById(creature_name).getBoundingClientRect().x > 0) {
x = firstX + 3 * Math.random();
y = firstY + 3 * Math.random();
}
else if (creature_array[nearestNeighbor].getBoundingClientRect().y - document.getElementById(creature_name).getBoundingClientRect().y < 0) {
x = firstX - 3 * Math.random();
y = firstY - 3 * Math.random();
}
else {
//they're touching! :)
//check for eating, mating, etc.! :)
var special_event = Math.random(); // roll the dice...
//can change this to a switch statement... although maybe don't want to...
//can also check for random mutations, e.g. to articulation or other elements of code! :)
/*
if (special_event <= 0.03) { // three in a hundred
clone(creature_name); //clone for now, could also reproduce sexually, etc.! :)
return;
}
***** this cloning causes a problem, in which deleting the creature through the later case fails *****
else*/ if (special_event <= 0.06) { // three in a hundred
createLifePath2(); //give birth! :)
createLifePath3(); //give birth to a litter! :)
createLifePath4(Math.random(), Math.random()); //give birth to a litter! :)
return;
//can later recombine creature codez... mutate... etc.! :)
}
//get eaten once every ten times...
//there's some issue w/ deleting if there are clones of this creature around...
else if (special_event <= 0.3) { //one in ten, five...
//could also have creature e.g. reproduce and die at the same time... (by de-elsing this conditional and making other adjustments...)
deleteCreature(creature_name); //get eaten
return; //stop running function on deleted/eaten creature
}
//do other cool stuff! :)
else if (special_event <= 0.4 && x > 5 && y < 200) { //one in ten, five...
//could also have creature e.g. reproduce and die at the same time... (by de-elsing this conditional and making other adjustments...)
document.getElementById(creature_name).setAttributeNS(null,"d","M "+450*Math.random()+", "+450*Math.random()+" Q "+450*Math.random()+", "+450*Math.random()+" "+450*Math.random()+", "+450*Math.random()+" T "+450*Math.random()+", "+450*Math.random()+", "+450*Math.random()+" "+450*Math.random()+", "+450*Math.random()+" "+450*Math.random()+", "+450*Math.random()+" "+450*Math.random()+", "+450*Math.random()+" "+450*Math.random()+" z"); // give the creature a new shape.
return; //stop running function on deleted/eaten creature
}
else { //default case
x = firstX + 6 * (Math.random() - .5);
y = firstY + 6 * (Math.random() - .5);
//mv about randomly...
}
}
//if way out of bounds then mv back
//probably move this or something like it into a more general creature-checking thing for area boundaryz! :)
if (document.getElementById(creature_name).getBoundingClientRect().right > 450) {x -= 200*Math.random()};
if (document.getElementById(creature_name).getBoundingClientRect().left < 0) {x += 200*Math.random()};
if (document.getElementById(creature_name).getBoundingClientRect().bottom > 450) {y -= 200*Math.random()};
if (document.getElementById(creature_name).getBoundingClientRect().top < 0) {y += 200*Math.random()};
} //staticmode0
else if (staticMode == 1) {
//if way out of bounds, mv bak towards center
x = 450*Math.random();
y = 450*Math.random();
//if ((document.getElementById(creature_name).getBoundingClientRect().x < 450) && (document.getElementById(creature_name).getBoundingClientRect().y < 450)) {
staticMode = 0; //reset mode
//}
}
} // if multiple creatures (figure out nearest neighbor)
//translate creature_name x y
pivot_x = (document.getElementById(creature_name).getBoundingClientRect().left + document.getElementById(creature_name).getBoundingClientRect().right) / 2; // average coordinates for pivot around middle.
pivot_y = (document.getElementById(creature_name).getBoundingClientRect().top + document.getElementById(creature_name).getBoundingClientRect().bottom) / 2; // average coordinates for pivot around middle.
document.getElementById(creature_name).setAttribute("transform", "translate("+x+", "+y+") rotate("+x/y*(Math.random()-0.5)+" "+pivot_x+" "+pivot_y+")"); // transform the creature
} //moveLifePathCreature2()
function moveLifePathCreatureNext(creature_name, x, y) {
//debug var blah = "testblah";
// Figure out nearest neighboring creature! :)
if (creature_index > 1) {
var nearestNeighbor = getNearestNeighbor(creature_name); // get (index of) nearbyest neighbor
// Getting existing translate X and Y, in order to apply subsequent X and Y
var xforms = document.getElementById(creature_name).transform.baseVal; // An SVGTransformList
var firstXForm = xforms.getItem(0); // An SVGTransform
if (firstXForm.type == SVGTransform.SVG_TRANSFORM_TRANSLATE){
var firstX = firstXForm.matrix.e,
firstY = firstXForm.matrix.f;
}
if (nextMode == 0) {
//if way out of bounds then mv back
if ((document.getElementById(creature_name).getBoundingClientRect().right > 450) || (document.getElementById(creature_name).getBoundingClientRect().bottom > 450) || (document.getElementById(creature_name).getBoundingClientRect().top < 0) || (document.getElementById(creature_name).getBoundingClientRect().left < 0)) {nextMode = 1};
// if ((document.getElementById(creature_name).getBoundingClientRect().right > 450) || (document.getElementById(creature_name).getBoundingClientRect().bottom > 450)) {staticMode = 1};
if (creature_array[nearestNeighbor].getBoundingClientRect().x - document.getElementById(creature_name).getBoundingClientRect().x > 0) {
x = firstX - 3 * Math.random();
y = firstY - 3 * Math.random();
}
else if (creature_array[nearestNeighbor].getBoundingClientRect().y - document.getElementById(creature_name).getBoundingClientRect().y < 0) {
x = firstX + 3 * Math.random();
y = firstY + 3 * Math.random();
}
else {
//they're touching! :)
//check for eating, mating, etc.! :)
var special_event = Math.random(); // roll the dice...
//can change this to a switch statement... although maybe don't want to...
//can also check for random mutations, e.g. to articulation or other elements of code! :)
/*
if (special_event <= 0.03) { // three in a hundred
clone(creature_name); //clone for now, could also reproduce sexually, etc.! :)
return;
}
***** this cloning causes a problem, in which deleting the creature through the later case fails *****
else*/ if (special_event <= 0.06) { // three in a hundred
createLifePath2(); //give birth! :)
createLifePath3(); //give birth to a litter! :)
createLifePath4(Math.random(), Math.random()); //give birth to a litter! :)
return;
//can later recombine creature codez... mutate... etc.! :)
}
//get eaten once every ten times...
//there's some issue w/ deleting if there are clones of this creature around...
else if (special_event <= 0.3) { //one in ten, five...
//could also have creature e.g. reproduce and die at the same time... (by de-elsing this conditional and making other adjustments...)
deleteCreature(creature_name); //get eaten
return; //stop running function on deleted/eaten creature
}
//do other cool stuff! :)
else if (special_event <= 0.4 && x > 5 && y < 200) { //one in ten, five...
//could also have creature e.g. reproduce and die at the same time... (by de-elsing this conditional and making other adjustments...)
document.getElementById(creature_name).setAttributeNS(null,"d","M "+450*Math.random()+", "+450*Math.random()+" Q "+450*Math.random()+", "+450*Math.random()+" "+450*Math.random()+", "+450*Math.random()+" T "+450*Math.random()+", "+450*Math.random()+", "+450*Math.random()+" "+450*Math.random()+", "+450*Math.random()+" "+450*Math.random()+", "+450*Math.random()+" "+450*Math.random()+", "+450*Math.random()+" "+450*Math.random()+" z"); // give the creature a new shape.
return; //stop running function on deleted/eaten creature
}
else { //default case
x = firstX + 6 * (Math.random() - .5);
y = firstY + 6 * (Math.random() - .5);
//mv about randomly...
}
}
//if way out of bounds then mv back
//probably move this or something like it into a more general creature-checking thing for area boundaryz! :)
if (document.getElementById(creature_name).getBoundingClientRect().right > 450) {x -= 200*Math.random()};
if (document.getElementById(creature_name).getBoundingClientRect().left < 0) {x += 200*Math.random()};
if (document.getElementById(creature_name).getBoundingClientRect().bottom > 450) {y -= 200*Math.random()};
if (document.getElementById(creature_name).getBoundingClientRect().top < 0) {y += 200*Math.random()};