Skip to content

Commit

Permalink
updated inheritance model. Camera and Player both inherit from Lookin…
Browse files Browse the repository at this point in the history
…gObject which inherits from WorldObject. Both levels of superclasses have synchronization methods which will be used to keep the player and camera in sync
  • Loading branch information
adam-zethraeus committed Mar 7, 2012
1 parent 7cada04 commit 7c298a4
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 45 deletions.
3 changes: 2 additions & 1 deletion Cakefile
Expand Up @@ -2,8 +2,9 @@ fs = require 'fs'
{exec} = require 'child_process'

appFiles = [
'vector'
'vector'
'worldobject'
'lookingobject'
'player'
'camera'
'test'
Expand Down
2 changes: 1 addition & 1 deletion src/camera.coffee
@@ -1,3 +1,3 @@
class Camera extends WorldObject
class Camera extends LookingObject
constructor: (x=0,y=0,z=0)->
super(x,y,z)
53 changes: 53 additions & 0 deletions src/lookingobject.coffee
@@ -0,0 +1,53 @@
class LookingObject extends WorldObject
pitch: 0
yaw: 0
fresh_look: false
look: new Vector3(0,0,0)

constructor: (x=0,y=0,z=0) ->
super(x,y,z)

setLook: (x,y,z) ->
if x instanceof Vector3
@look = x
else if x? and y? and z?
@look.x = x
@look.y = y
@look.z = z

addToPitch: (p) ->
@pitch += p
if @pitch > 89.9
@pitch = 89.9
else if @pitch < -89.9
@pitch = 89.9
@fresh_look = false

addToYaw: (y) ->
@yaw += y + 360
@yaw %= 360
@fresh_look = false

getPitch: ->
return @pitch

getYaw: ->
return @yaw

getLook: ->
if not @fresh_look
@look = new Vector3(Math.sin((@yaw*Math.PI)/180)*Math.cos((@pitch*Math.PI)/180), -Math.sin((@pitch*Math.PI)/180), -Math.cos((@yaw*Math.PI)/180)*Math.cos((@pitch*Math.PI)/180))
@look.normalize()
@fresh_look = true
return @look

getHorizontalLook: ->
vlook = @getLook()
hlook = new Vector2(vlook.x, vlook.z)
hlook.normalize()
return hlook

synchronizeWithLookingObject: (obj)->
if obj.look instanceof Vector3
@look = obj.look
@synchronizeWithWorldObject(obj)
3 changes: 2 additions & 1 deletion src/player.coffee
@@ -1,3 +1,4 @@
class Player extends WorldObject
class Player extends LookingObject

constructor: (x=0, y=0, z=0) ->
super(x,y,z)
20 changes: 14 additions & 6 deletions src/test.coffee
@@ -1,7 +1,15 @@
#TEST VECTOR CONSTRUCTOR
v = new Vector2(1,2);
console.log (v)
console.log(v.y)
v = new Vector3(20, 1.3, 15)
v.normalize()
console.log(v.length())
y = v.normalize()
console.log(y)

w = new Player(0,0,0)
w.setLocation(new Vector3(1,2,3))
w.addToPitch(200)
w.addToYaw(370)
console.log(w)
console.log(w.getLook())
console.log(w.getLocation())

c = new Camera(0,0,0)
c.synchronizeWithLookingObject(w);
console.log(c)
8 changes: 5 additions & 3 deletions src/vector.coffee
Expand Up @@ -95,7 +95,7 @@ class Vector3
@y -= vec
@z -= vec

#multiply by a vector
#multiply by a Vector3 or scalar
mult: (vec) ->
if vec instanceof Vector3
@x *= vec.x
Expand All @@ -106,7 +106,7 @@ class Vector3
@y *= vec
@z *= vec

#divide by a vector
#divide by a Vector3 or scalar
divide: (vec) ->
if vec instanceof Vector3
@x /= vec.x
Expand All @@ -116,11 +116,13 @@ class Vector3
@x /= vec
@y /= vec
@z /= vec
#check equality

#check equality between Vector3s
equals: (vec) ->
if vec instanceof Vector3 and @x is vec.x and @y is vec.y and @z is vec.z
return true

#return length of vector
length: ->
return Math.sqrt(@lengthSquared())

Expand Down
57 changes: 24 additions & 33 deletions src/worldobject.coffee
@@ -1,36 +1,27 @@
###
***WorldObject***
The constructor for world object takes one of:
*Nothing; indicating a default location of (0,0,0)
*A Vector3 indicating the default location
*an x,y, and, z indicating the default location
###
class WorldObject
location : new Vector3(0,0,0)
look: 0
pitch: 0
yaw: 0
fresh_look: false
constructor: (x=0,y=0,z=0)->
@location = new Vector3(x,y,z)
@look = new Vector3(x,y,z)
setLocation: (x=0,y=0,z=0)->
@location = new Vector3(x,y,z)
setPitch: (p) ->
@pitch = p
@fresh_look = false
setYaw: (y) ->
@yaw = y
@fresh_look = false
getPitch: ->
return @pitch
getYaw: ->
return @yaw
location: new Vector3(0,0,0)

constructor: (x=0,y=0,z=0) ->
@setLocation(x,y,z)

setLocation: (x,y,z) ->
if x instanceof Vector3
@location = x
else if x? and y? and z?
@location.x = x
@location.y = y
@location.z = z

getLocation: ->
return @location
getLook: ->
if not @fresh_look
@look = new Vector3(Math.sin((@yaw*Math.PI)/180)*Math.cos((@pitch*Math.PI)/180), -Math.sin((@pitch*Math.PI)/180), -Math.cos((@yaw*Math.PI)/180)*Math.cos((@pitch*Math.PI)/180))
@look.normalize()
@fresh_look = true
return @look
getHorizontalLook: ->
vlook = @getLook()
hlook = new Vector2(vlook.x, vlook.z)
hlook.normalize()
return hlook
synchronizeWithWorldObject: (obj)->
@location = obj.getLocation()

synchronizeWithWorldObject: (obj) ->
if obj.location instanceof Vector3
@location = obj.location

0 comments on commit 7c298a4

Please sign in to comment.