Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremy0613 committed Feb 3, 2018
1 parent d411db2 commit 6e3ad4b
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 20 deletions.
33 changes: 29 additions & 4 deletions _/components/js/_wes.mantooth.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ var $w = {
objects:{},
// @param {Object} objects that are on the stage but will never be looped
noloop:{},
looprunning:true,
// @param {Object} if set into the inits loop all objects will call an init function for all called by add_object
inits:{},
refs:[],
hasdepth:[],
gamespeed:0,
gamespeed:0,
global:{},
// @param {Object} pre-load all the assets into this object
assets: {
img:[],
Expand Down Expand Up @@ -126,31 +128,54 @@ var $w = {
}
}
}
if(ra){
if(ra && $w.looprunning){
window.setTimeout(function(){
window.requestAnimationFrame(function(){
$w.loop(ra,i,fps);
});
},$w.gamespeed);
}
},
clearloop: function() {
$w.looprunning = false;
},
/**
* set a variable for all objects of type obj
* @param {String} the name of the target object
* @param {String} the name of the object to update
* @param {Variant}
* @returns {Void}
* */
obj_set_var: function(obj,v,val) {
obj_set_var: function(obj,v,val,callback) {
if (this.objects.hasOwnProperty(obj)) {
var l = this.objects[obj].length;
for(var j = 0; j < l; j++) {
if (this.objects[obj][j] != null)
this.objects[obj][j][v] = val;
}
}
if (typeof callback === 'function')
callback();
},
/**
* runs a method for all instances of an object
* @param {String} the name of the target object
* @param {String} the name of the object to update
* @param {Variant} parameter to pass to the function (probably an object)
* @returns {Void}
* */
obj_run_method: function(obj,f,p,callback) {
if (this.objects.hasOwnProperty(obj)) {
var l = this.objects[obj].length;
for(var j = 0; j < l; j++) {
if (this.objects[obj][j] != null)
if (typeof this.objects[obj][j][f] === 'function')
this.objects[obj][j][f](p);
}
}
if (typeof callback === 'function')
callback();
},


/* Game Hooks */

Expand Down
22 changes: 15 additions & 7 deletions _/components/js/game.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,13 @@ $w.game = {
* @param {Number} W
* @param {Number} H
*
* @returns {Number} Reference ID to the canvas
* @returns {Number} Reference ID to the canvas
* */
add_object_single: function(maxobjects,$target,params,$container,w,h) {
add_object_single: function(maxobjects,$target,params,$container,w,h,nozindex) {

var i,j;
var i,j, redim = false;

if (typeof nozindex === 'undefined') nozindex = true;

// if $container does not have a reference then this is the first run
if(typeof $container === 'object') {
Expand All @@ -70,17 +72,23 @@ $w.game = {
// get the reference
i = $container;
}
$w.objects[$target.name] = [];
if (typeof $w.objects[$target.name] === 'undefined') {
$w.objects[$target.name] = [];
redim = true;
}
// run a loop to create all the objects
for(j=0; j<maxobjects; j++){
params.i = i;
params.count = j;
params.z = j;
$w.objects[$target.name].push(new $target(params));
}
// redim the array to a length of 9999
for (var z=maxobjects; z<9999;z++) {
$w.objects[$target.name][z] = null;
if (redim && nozindex) {
// redim the array to a length of 9999
for (var z=maxobjects; z<9999;z++) {
if (typeof $w.objects[$target.name][z] !== 'object' && $w.objects[$target.name][z] != null)
$w.objects[$target.name][z] = null;
}
}
return i;
},
Expand Down
104 changes: 99 additions & 5 deletions _/components/js/threed.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,83 @@
* */
$w.threed = {
makeA3DPoint: function(x,y,z){
var point = new Object();
point.x = x;
point.y = y;
point.z = z;
return point;
var point = {
x:x,
y:y,
z:z
}
return point;
},
make2DPoint: function(x, y, depth, scaleRatio){
var point = {
x:x,
y:y,
depth:depth,
scaleRatio:scaleRatio
}
return point;
},
Transform3DPointsTo2DPoints: function(points, axisRotations, focalLength, zOrigin){

if (undefined === zOrigin) zOrigin = 1;

// the array to hold transformed 2D points - the 3D points
// from the point array which are here rotated and scaled
// to generate a point as it would appear on the screen
var TransformedPointsArray = [];
// Math calcs for angles - sin and cos for each (trig)
// this will be the only time sin or cos is used for the
// entire portion of calculating all rotations
var sx = Math.sin(axisRotations.x);
var cx = Math.cos(axisRotations.x);
var sy = Math.sin(axisRotations.y);
var cy = Math.cos(axisRotations.y);
var sz = Math.sin(axisRotations.z) * zOrigin;
var cz = Math.cos(axisRotations.z) * zOrigin;

// a couple of letiables to be used in the looping
// of all the points in the transform process
var x,y,z, xy,xz, yx,yz, zx,zy, scaleFactor;

// 3... 2... 1... loop!
// loop through all the points in your object/scene/space
// whatever - those points passed - so each is transformed
var i = points.length;
while (i--){
// apply Math to making transformations
// based on rotations

// assign letiables for the current x, y and z
var x = points[i].x;
var y = points[i].y;
var z = points[i].z;

// perform the rotations around each axis
// rotation around x
var xy = cx*y - sx*z;
var xz = sx*y + cx*z;
// rotation around y
var yz = cy*xz - sy*x;
var yx = sy*xz + cy*x;
// rotation around z
var zx = cz*yx - sz*xy;
var zy = sz*yx + cz*xy;

// now determine perspective scaling factor
// yz was the last calculated z value so its the
// final value for z depth
var scaleRatio = focalLength/(focalLength + yz);
// assign the new x, y and z (the last z calculated)
x = zx*scaleRatio;
y = zy*scaleRatio;
z = yz;
// create transformed 2D point with the calculated values
// adding it to the array holding all 2D points
TransformedPointsArray[i] = this.make2DPoint(x, y, -z, scaleRatio);
}
// after looping return the array of points as they
// exist after the rotation and scaling
return TransformedPointsArray;
},
convertPointIn3DToPointIn2D: function(pointIn3D,focalLength){
var scaleRatio = focalLength/(focalLength + pointIn3D.z);
Expand All @@ -16,6 +88,28 @@ $w.threed = {
y:(pointIn3D.y * scaleRatio)
};
},
drawLines: function(ci,screenPoints,facesArray,x,y,color,lineWidth){
var l = facesArray.length, prev, i = 0, j = 0, fl = 0;
for (i=0; i<l; i++){
if (0 == fl) {
fl = facesArray[0].length;
}
for(j=0; j<fl; j++){
if (j==0) {
prev = facesArray[i][j];
}else{
if (undefined !== screenPoints[prev] && undefined !== screenPoints[facesArray[i][j]]) {
$w.canvas.line(ci,
screenPoints[prev].x+x,
screenPoints[prev].y+y,
screenPoints[facesArray[i][j]].x+x,
screenPoints[facesArray[i][j]].y+y,color,lineWidth);
}
prev = facesArray[i][j];
}
}
}
},
cP3dto2d: function(pointIn3D,camera,origin){
var pointIn2D = [];

Expand Down
2 changes: 1 addition & 1 deletion _/js/wes.mantooth.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions examples/scripts.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

// if this is my local machine the run Grunt auto-reload
$local = '';
if('192.168.1.157' == $_SERVER['REMOTE_ADDR'])
$local = '<script src="http://192.168.1.154:35729/livereload.js"></script>';
if('192.168.1.160' == $_SERVER['REMOTE_ADDR'])
$local = '<script src="http://192.168.1.159:35729/livereload.js"></script>';

// allow for sub-pathing as neccesary
$path = isset($_GET['path']) ? $path : '../';
Expand Down
2 changes: 1 addition & 1 deletion index.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ function showSection($t) {
some of my old projects and found this. Version 2.0 represents a complete reworking of the code, pretty much from the ground up. It does incorporate the old code in some areas,
but 1. It no longer uses paper.js (It's actually all my own code) 2. I have learned quite a lot since I wrote 1.0. As usual, this was never intended for production use of any kind. It was just a hobby.
But anyone who finds this and would like to use it or participate, feel free to <a href="https://github.com/061375/Wes.Mantooth" >fork it</a>.</p>
<p>To read articles further explaining some of the content found on this website please <a href="http://jeremyheminger.com/taxonomy/term/182" target="_blank" rel="noopener">click here</a></p>
<p>To read articles further explaining some of the content found on this website please <a href="http://jeremyheminger.com/wes-mantooth" target="_blank" rel="noopener">click here</a></p>
<p>My name is <a href="http://www.jeremyheminger.com" >Jeremy Heminger</a> and I am a full LAMP stack developer located in Redlands California</p>
</header>
<div class="left">
Expand Down
2 changes: 2 additions & 0 deletions wip/pseudo3D/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,10 @@
* @returns {Void}
* */
Camera.prototype.Aup = function(e,s) {
console.log(s.d);
s.x+=Math.sin($w.math.radians(s.d))*WSPEED;
s.y+=Math.cos($w.math.radians(s.d))*WSPEED;
console.log(s.x);
}
/**
* Adown
Expand Down

0 comments on commit 6e3ad4b

Please sign in to comment.