view TaskManager/Test/test_render/cube_action.cpp @ 213:159519cdca1f

add SceneGraph "3D Super-Dandy"
author gongo@localhost.localdomain
date Sun, 01 Feb 2009 20:45:07 +0900
parents fe2cc32cd94d
children 7ca6a2ef5be9
line wrap: on
line source

#include "SceneGraphRoot.h"
#include "SGList.h"

static void cube_move_left(SceneGraphPtr node, int screen_w, int screen_h);
static void cube_move_right(SceneGraphPtr node, int screen_w, int screen_h);
static void cube_move_idle(SceneGraphPtr node, int screen_w, int screen_h);
static void cube_collision(SceneGraphPtr node, int screen_w, int screen_h,
			   SceneGraphPtr tree);

static void cube_split(SceneGraphPtr root);

static void
cube_move_left(SceneGraphPtr node, int screen_w, int screen_h)
{
    node->xyz[0] -= node->stack_xyz[0];
    node->xyz[1] -= node->stack_xyz[0] * node->stack_xyz[1];
    node->xyz[2] -= node->stack_xyz[0] * node->stack_xyz[2];

    if (node->xyz[0] < 0) {
	node->set_move_collision(cube_move_right, cube_collision);
    }

    if (node->xyz[1] < 0 || node->xyz[1] > screen_h) {
	node->stack_xyz[1] = -node->stack_xyz[1];
    }

    node->angle[0] += 2.0f;
    node->angle[1] += 2.0f * node->stack_xyz[1];
    node->angle[2] += 2.0f * node->stack_xyz[2];
    
#if 0
    if (sgroot->controller->circle.isPush()) {
      cube_split(node);
    }

#endif
}

int time = 0;

static void
cube_move_right(SceneGraphPtr node, int screen_w, int screen_h)
{
    node->xyz[0] += node->stack_xyz[0];
    node->xyz[1] -= node->stack_xyz[0] * node->stack_xyz[1];
    node->xyz[2] -= node->stack_xyz[0] * node->stack_xyz[2];

    if (node->xyz[0] > screen_w) {
	node->set_move_collision(cube_move_left, cube_collision);
    }

    if (node->xyz[1] < 0 || node->xyz[1] > screen_h) {
	node->stack_xyz[1] = -node->stack_xyz[1];
    }

    node->angle[0] += 2.0f;
    node->angle[1] += 2.0f * node->stack_xyz[1];
    node->angle[2] += 2.0f * node->stack_xyz[2];

#if 1
    if (time++ > 10 && sgroot->controller->circle.isPush()) {
      cube_split(node);
      time = 0;
    }
#endif
}

static void
cube_split(SceneGraphPtr root)
{
    SceneGraphPtr p = root->clone();
    root->addBrother(p);

    root->set_move_collision(cube_move_left, cube_collision);
    p->set_move_collision(cube_move_right, cube_collision);
    
    p->xyz[0] = root->xyz[0] + 2;
    p->xyz[1] = root->xyz[1];
    p->xyz[2] = root->xyz[2];

    p->stack_xyz[0] = 2.0f;
    p->stack_xyz[1] = random()%3-1;
    p->stack_xyz[2] = random()%3-1;

    root->xyz[0] -= 2;
    root->stack_xyz[0] = 2.0f;
    root->stack_xyz[1] = random()%3-1;
    root->stack_xyz[2] = random()%3-1;
}


static void
cube_move_idle(SceneGraphPtr node, int screen_w, int screen_h)
{
    node->xyz[0] = screen_w/2;
    node->xyz[1] = screen_h/2;
    //node->xyz[2] = -300.0f;

    if (sgroot->controller->circle.isPush()) {
      cube_split(node);
    }
}

static void
cube_collision(SceneGraphPtr node, int screen_w, int screen_h,
	       SceneGraphPtr tree)
{
}

void
create_cube_split(int number)
{
    SceneGraphPtr cube;

    //sgroot->createFromXMLfile("xml_file/cube_split.xml");
    sgroot->createFromXMLfile("xml_file/cube.xml");

    //if (number == 0) {
    //cube = sgroot->createSceneGraph(SmallCube);
//} else {
    cube = sgroot->createSceneGraph(Cube);
    cube->xyz[0] = 960.0f;
    cube->xyz[1] = 540.0f;
//}

    cube->set_move_collision(cube_move_idle, cube_collision);

    sgroot->setSceneData(cube);
}