####################################################################################
#nodebeam, node/beam engine test implementation
####################################################################################
#notes:
#       test program , goal:
#               -not to fuck around with the stable test function in nworld.py
#               -simulate real program using node/beams
#

import pyg, nworld
from nworld import *

## world init
world = nworld(0,0, pyg.width, pyg.height-10)           # create world  (start coords, end coords)

## demo toggle functions
demonodes(world)                                        # create some free nodes
demoshape(world)                                        # create some node/beam shape
demosquare(world)                                       # create a square
pin(world.nodeLst[3])                                   # nail a node down       
#demopush(world)                                        # impulse it all around for demo

## demo counters
counter = 0
d=-4
a=1
## demo hydro beam setup       
world.beamLst.append(beam(world.nodeLst[20],world.nodeLst[9],beamtype))   # hydro beam
hydro = 26                                              # position in list
world.beamLst[hydro].rigidity = 0.8                    # demo hydro beam rigidity


#### main
while pyg.main():
## draw world
        pyg.line(world.box[0],world.box[3],world.box[2],world.box[3])   # draw ground line    
        pyg.line(pointA.x, pointA.y, pointB.x, pointB.y, green)
        pyg.line(pointC.x, pointC.y, pointD.x, pointD.y, red)
## demo timed impulse on a node
        counter += 0.2
        if counter > 100:
                counter = 0
                a = -a
                push(world.nodeLst[4], vector(3000*a,-3000))
                push(world.nodeLst[5], vector(3000*a,-3000))
                impulse(world.nodeLst[20], vector(800*a,-800))
                impulse(world.nodeLst[18], vector(800*a,-800))
## demo hydro beam
        if world.beamLst[hydro].restlenght > 180:                          # change beam lenght in realtime
                world.beamLst[hydro].restlenght = 180
                d = -d
        if world.beamLst[hydro].restlenght < 5:
                world.beamLst[hydro].restlenght = 5
                d = -d
        world.beamLst[hydro].restlenght += d

## physics update
        update(world)                                                   # one physics tick
## draw objects                            
        for i in world.nodeLst:                                         # render nodes
                pyg.circle(i.p.x, i.p.y, 2, i.col)
                i.col = red                                             # reset collision color
                pyg.line(i.p.x, i.p.y, i.op.x, i.op.y, blue)
        for i in world.beamLst:                                         # render beams
                colorratio = 4500
                cf = int(i.pushforce * colorratio)      # make some force color
                
                if cf > 127:
                        cf = 127
                elif cf < -127:
                        cf = -127                                       # testing force color
                color = 127-cf, 127, cf+127
                pyg.line(i.nA.p.x,i.nA.p.y, i.nB.p.x,i.nB.p.y, color)
        
        

