示例#1
0
frog::frog() {
    camera_ = std::make_shared<perspective_camera>(90, 1, 0.01, 10);
    headlight_ = std::make_shared<light_source>(GL_LIGHT7);
    bounding_box() = ::bounding_box(-1.3, -1.3, -1.3, 1.3, 1.3, 1.3);

    headlight()->exponent() = 15;
    headlight()->cutoff() = 30;
    headlight()->toggle_key() = 'h';
}
示例#2
0
void game_manager::init(int w, int h) {
    WINDOW_WIDTH = w;
    WINDOW_HEIGHT = h;

    last_time_ = glutGet(GLUT_ELAPSED_TIME);

    auto frog_ = std::make_shared<frog>();
    frog_->position() = vector3(0.0, FROG_LEVEL, 1.95);
    frog_->scale(0.1);
    game_objects_.push_back(frog_);
    collision_manager::instance().register_object(frog_);

    auto car1_ = std::make_shared<car>();
    car1_->position() = vector3(-1.2, ROAD_LEVEL, LINE_4);
    car1_->scale(0.1);
    car1_->speed().x() = 1.0;
    game_objects_.push_back(car1_);
    collision_manager::instance().register_object(car1_);

    for (int i = 0; i < 3; i++) {
        auto truck_ = std::make_shared<truck>();
        truck_->position() = vector3(-1.5 + i * 1.5, ROAD_LEVEL, LINE_3);
        truck_->scale(0.1);
        truck_->speed().x() = 0.5;
        game_objects_.push_back(truck_);
        collision_manager::instance().register_object(truck_);
    }

    auto bus1_ = std::make_shared<bus>();
    bus1_->position() = vector3(0.9, ROAD_LEVEL, LINE_5);
    bus1_->scale(0.1);
    bus1_->speed().x() = 1.5;
    game_objects_.push_back(bus1_);
    collision_manager::instance().register_object(bus1_);

    auto bus2_ = std::make_shared<bus>();
    bus2_->position() = vector3(-0.1, ROAD_LEVEL, LINE_4);
    bus2_->scale(0.1);
    bus2_->speed().x() = 1.0;
    game_objects_.push_back(bus2_);
    collision_manager::instance().register_object(bus2_);

    for (int i = 0; i < 2; i++) {
        auto log_ = std::make_shared<timberlog>();
        log_->position() = vector3(-0.6 + i * 2.0, RIVER_LEVEL, LINE_1);
        log_->scale(0.1);
        log_->speed().x() = 0.5;
        game_objects_.push_back(log_);
        collision_manager::instance().register_object(log_);

        auto log1_ = std::make_shared<timberlog>();
        log1_->position() = vector3(-1.1 + i * 1.9, RIVER_LEVEL, LINE_2);
        log1_->scale(0.1);
        log1_->speed().x() = 0.75;
        game_objects_.push_back(log1_);
        collision_manager::instance().register_object(log1_);
    }

    for (int i = 0; i < 4; i++) {
        auto turtle_ = std::make_shared<turtle>();
        turtle_->position() =
            vector3(-2.5 + i * 1.3, RIVER_LEVEL + 0.05, LINE_6);
        turtle_->scale(0.1);
        turtle_->speed().x() = 0.75;
        game_objects_.push_back(turtle_);
        collision_manager::instance().register_object(turtle_);
    }

    auto river_ = std::make_shared<river>();
    river_->position() = vector3(0.0, 0.0, LINE_1);
    game_objects_.push_back(river_);
    collision_manager::instance().register_object(river_);

    auto tunnel_ = std::make_shared<tunnel>();
    tunnel_->position() = vector3(0.0, 0.0, LINE_1);
    game_objects_.push_back(tunnel_);

    auto road_ = std::make_shared<road>();
    road_->position() = vector3(0.0, 0.0, LINE_4);
    game_objects_.push_back(road_);

    // INVALID CAMERAS: they will be set correctly on reshape
    // Camera 0: top view orthogonal camera
    cameras_.push_back(std::make_shared<orthogonal_camera>());
    cameras_.back()->eye().set(0.0, 0.0, 0.0);
    cameras_.back()->at().set(0.0, -1.0, 0.0);
    cameras_.back()->up().set(0.0, 0.0, -1.0);

    // Camera 1: top view perspective camera
    cameras_.push_back(std::make_shared<perspective_camera>());
    cameras_.back()->eye().set(0.0, 3.0, 2.0);
    cameras_.back()->at().set(0.0, 0.0, 0.0);
    cameras_.back()->up().set(0.0, 0.0, -1.0);

    // Camera 2: frog perspective camera
    cameras_.push_back(frog_->cam());

    light_sources_.push_back(std::make_shared<light_source>(GL_LIGHT0));
    light_sources_.push_back(std::make_shared<light_source>(GL_LIGHT1));
    light_sources_.push_back(std::make_shared<light_source>(GL_LIGHT2));
    light_sources_.push_back(std::make_shared<light_source>(GL_LIGHT3));
    light_sources_.push_back(std::make_shared<light_source>(GL_LIGHT4));
    light_sources_.push_back(std::make_shared<light_source>(GL_LIGHT5));
    light_sources_.push_back(std::make_shared<light_source>(GL_LIGHT6));
    light_sources_.push_back(frog_->headlight());

    auto light = light_sources_[0];
    light->ambient().set(0.2, 0.2, 0.2, 1.0);
    light->diffuse().set(0.3, 0.3, 0.3, 1.0);
    light->position().set(0.0, 5.0, 0.0, 1.0);
    light->turn_on();
    light->toggle_key() = 'n';

    for (size_t i : {1, 2, 3, 4, 5, 6}) {
        light = light_sources_[i];
        light->toggle_key() = 'c';
        light->diffuse().set(0.23, 0.23, 0.23, 1.0);
        light->turn_off();

        light->position().x() = i % 2 == 0 ? 2.0 : -2.0;
        light->direction().x() = -light->position().x() / 2;

        light->position().y() = 1.5;
        light->direction().y() = -light->position().y();

        light->position().z() = -2.0 + 2.0 * ((i - 1) / 2);
        light->direction().z() = -light->position().z() / 2;

        // make positional light
        light->position().w() = 1;
        light->cutoff() = 30;
        light->exponent() = 15;
    }
}
示例#3
0
void frog::update(glut_time_t dt) {
    dynamic_object::update(dt);

    auto collisions = collision_manager::instance().collisions(this);
    if (collisions.size() != 0) {
        vector3::scalar_t collisionSpeed = 0;

        bool touchesRiver = false, touchesLog = false, touchesTurtle = false,
             touchesOther = false;

        for (auto col : collisions) {
            std::shared_ptr<game_object> obj;
            dynamic_object *dyn = nullptr;
            class bounding_box bb;

            std::tie(bb, obj) = col;

            if (dynamic_cast<river *>(obj.get()) != nullptr) {
                touchesRiver = true;
                continue;
            } else if ((dyn = dynamic_cast<timberlog *>(obj.get())) !=
                       nullptr) {
                touchesLog = true;
                collisionSpeed = std::max(collisionSpeed, dyn->speed().x());
                continue;
            } else if ((dyn = dynamic_cast<turtle *>(obj.get())) != nullptr) {
                touchesTurtle = true;
                collisionSpeed = std::max(collisionSpeed, dyn->speed().x());
                continue;
            } else if ((dyn = dynamic_cast<frog *>(obj.get())) != nullptr) {
                assert(!"Frog should not collide with himself!");
            } else {
                touchesOther = true;
                break;
            }
        }

        position().x() += collisionSpeed * dt;

        if (touchesOther || (touchesRiver && !(touchesLog || touchesTurtle))) {
            position() = vector3(0.0, 0.05, 1.95);
        }
    }

    position().x() =
        std::fmax(position().x(), game_manager::instance().frog_bounds().x1());
    position().x() =
        std::fmin(position().x(), game_manager::instance().frog_bounds().x2());
    position().z() =
        std::fmax(position().z(), game_manager::instance().frog_bounds().z1());
    position().z() =
        std::fmin(position().z(), game_manager::instance().frog_bounds().z2());

    headlight()->position() = position() + vector3(0.0, 1.1, 0.0);
    headlight()->position().w() = 1;
    headlight()->direction() = vector3(0.0, -3.0, -2.0).norm();
    headlight()->diffuse().set(0.7, 0.7, 0.7, 1.0);

    cam()->eye() = position() + vector3(0, 1.5, 0.5);
    cam()->at() = position() + vector3(0.0, 0.0, -0.5);
    cam()->up() = vector3(0.0, 1.0, 0.0);
}
示例#4
0
文件: cb2bus.c 项目: nesl/ragobots
int8_t module(void *state, Message *msg)
#endif
{
  cb2bus_state *s = (cb2bus_state*) state;
  MsgParam *p = (MsgParam*)(msg->data);

  switch (msg->type) {
	//-------------------------------------------------------------------------
	// KERNEL MESSAGE - INITIALIZE MODULE
	//-------------------------------------------------------------------------
  case MSG_INIT:
	{
	  s->pid = msg->did;
	  
	  CB2_init(s);
	  return SOS_OK;
	}
  case MSG_DECKLIGHT1: 
	{
	  
	  decklight1(s, p->byte);
	  return SOS_OK;
	}
  case MSG_DECKLIGHT2: 
	{
	  decklight2(s, p->byte);
	  return SOS_OK;
	}
  case MSG_DECKLIGHT3: 
	{
	  decklight3(s, p->byte);
	  return SOS_OK;
	}
  case MSG_DECKLIGHT4: 
	{
	  decklight4(s, p->byte);
	  return SOS_OK;
	}
  case MSG_DECKLIGHT5: 
	{
	  decklight5(s, p->byte);
	  return SOS_OK;
	}
  case MSG_DECKLIGHT6: 
	{
	  decklight6(s, p->byte);
	  return SOS_OK;
	}
  case MSG_CLEARDECKLIGHTS: 
  	{
	  cleardecklights(s, p->byte);
	  return SOS_OK;
	}
  case MSG_BARPOSITION:
	{
	  barPosition(s, p->byte);
	  return SOS_OK;
	}
  case MSG_BARPERCENT:
	{
	  barPercent(s, p->byte);
	  return SOS_OK;
	}
  case MSG_BARBINARY:
	{
	  barBinary(s, p->word);
	  return SOS_OK;
	}
  case MSG_HEADLIGHT:
	{
	  headlight(s, p->byte, p->word);
	  return SOS_OK;
	}
  case MSG_LOAD:
	{
	  ker_cb2bus_load(s->config, sizeof(s->config));
	  return SOS_OK;
	}
  case MSG_SERVO_EN:
	{
	  if (p->byte == ENABLE) 
		sbi(s->config[3], 7);
	  else 
		cbi(s->config[3], 7);
	  return SOS_OK;
	}
  case MSG_IRR_EN:
	{
	  if (p->byte == ENABLE) 
		sbi(s->config[3], 6);
	  else 
		cbi(s->config[3], 6);
	  return SOS_OK;
	}
  /*case MSG_BC_EN:
	{
	  if (p->byte == ENABLE) 
		sbi(s->config[3], 4);
	  else 
		cbi(s->config[3], 4);
	  return SOS_OK;
	} */
  case MSG_MAG_RESET:
	{
	  cbi(s->config[3], 2);
	  ker_cb2bus_load(s->config, sizeof(s->config));
	  TOSH_uwait(3000);
	  sbi(s->config[3], 2);
	  ker_cb2bus_load(s->config, sizeof(s->config));
	  
	  return SOS_OK;
	}
  case MSG_MAG_EN:
	{
	  if (p->byte == ENABLE) 
		sbi(s->config[3], 3);
	  else 
		cbi(s->config[3], 3);
	  return SOS_OK;
	}
  case MSG_ACC_EN:
	{
	  if (p->byte == ENABLE) 
		sbi(s->config[3], 1);
	  else 
		cbi(s->config[3], 1);
	  return SOS_OK;
	}
  case MSG_5V_EN:
	{
	  if (p->byte == ENABLE) 
		cbi(s->config[3], 0);
	  else 
		sbi(s->config[3], 0);
	  return SOS_OK;
	}
  case MSG_RFID_RESET:
	{
	  cbi(s->config[3], 5);
	  ker_cb2bus_load(s->config, sizeof(s->config));
	  sbi(s->config[3], 5);
	  ker_cb2bus_load(s->config, sizeof(s->config));
	  return SOS_OK;
	}
  case MSG_FINAL:
	{
	  return SOS_OK;
	}
  default:
	return -EINVAL;
  }
}