Planet Samuro (France)
Messages
Guest_35859
Planet Samuro (France)
 
Messages
3D Chat
Mini-Chat

Message Panels : Aide Technique : script pour meuble à 4 places (deux couples)
<<<First<<prev.>next.>>>Last>RepReply^Discussion ^vDiscussion vDelDelete the discussion
SamuroSamuro27/04Sent: 27/04/2024 01:28:091 / 3Message 1 from 3

// script pour meuble à 4 places (deux couples)

const int SIT_PLACES = 4;   // how many people can sit (must be multiple of 2)
const int MAX_ANIMS  = 1;   // how many anims on each sit place

struct SIT
{
  vector    pos;
  vector    rot;
  string(20) anim;
}

type ROW is SIT[SIT_PLACES];


// *** for couch ***

const ROW g_sit[MAX_ANIMS] =
  {
    {
      {pos  => {0.4, -1.4, 1.4},     // seat 1
       rot  => {0.0, 0.0,65.0},
       anim => "beachchair2-f-A2"},
      {pos  => {0.35, 0.80, 0.60},   // seat 2
       rot  => {0.0, 0.0, -90.0},
       anim => "assise2"},
      {pos  => {0.45, -0.70, 0.60},  // seat 3
       rot  => {0.0, 0.0, -90.0},
       anim => "assise2-A1"},
      {pos  => {0.25, 1.8, 1.40},    // seat 4
       rot  => {3.2, 0.8, 0.0},
       anim => "beachchair2-f-A3"},
    },
  };

//-------------------------------------------------------

const key NULL_KEY = {0,0,0,0};

int[SIT_PLACES/2] g_anim_set;   // anim set (0 to MAX_ANIMS-1)
key[SIT_PLACES]   g_avatar;

//-------------------------------------------------------

// update the table g_avatar : remove any avatar that is not sitting anymore

void update_avatars()
{
  sitter s;
  int    i;
  bool   valid[SIT_PLACES];

  clear valid;
  while (get_sitter (out s))
  {
    for (i=0; i<SIT_PLACES; i++)
    {
      if (same_key (s.avatar, g_avatar[i]))
        valid[i] = true;
    }
  }

  for (i=0; i<SIT_PLACES; i++)
  {
    if (!valid[i])
      g_avatar[i] = NULL_KEY;
  }
}

//-------------------------------------------------------

void sit_avatar (int anim, int ava, key k)
{
  if (sit (mesh_nr  => 1,
           position => g_sit[anim][ava].pos,
           rotation => g_sit[anim][ava].rot,
           avatar   => k))
  {
    start_animation (g_sit[anim][ava].anim, k);
    g_avatar[ava] = k;
  }
}

//-------------------------------------------------------

// must return seat from 0 to SIT_PLACES-1

int touched_place ()
{
  int   i, seat;
  float dist, best_dist;
  vector tt = touched_mesh_position();
  float dx, dy, dz;

  seat = 0;
  best_dist = 999999999.0;
  for (i=0; i<SIT_PLACES; i++)
  {
    if (!same_key (g_avatar[i], NULL_KEY))  // taken
      continue;

    dx = tt.x - g_sit[0][i].pos.x;
    dy = tt.y - g_sit[0][i].pos.y;
    dz = tt.z - g_sit[0][i].pos.z;

    dist = dx*dx + dy*dy + dz*dz;
    if (dist < best_dist)
    {
      seat = i;
      best_dist = dist;
    }
  }

  return seat;
}

//-------------------------------------------------------

event touch()
{
  key k = touched_avatar();

  if (is_sitting (k))
  {
    unsit (k);
  }
  else
  {
    int ava;

    update_avatars();

    ava = touched_place();
    if (same_key (g_avatar[ava], NULL_KEY))   // no one sitting there yet
    {
      sit_avatar (anim => g_anim_set[ava/2], ava => ava, k => k);
    }
  }
}

//-------------------------------------------------------

event click_avatar()
{
  int i, h;
  key ava;

  update_avatars();

  // compute i = sitting avatar index
  ava = clicked_avatar();
  for (i=0; i<SIT_PLACES; i++)
  {
    if (same_key (g_avatar[i], ava))
      break;
  }
  if (i >= SIT_PLACES)
    return;

  // advance anim for couple having i
  h = i/2;
  g_anim_set[h]++;
  if (g_anim_set[h] == MAX_ANIMS)
    g_anim_set[h] = 0;

  // update couple
  for (i=h*2; i<h*2+2; i++)
  {
    if (!same_key (g_avatar[i], NULL_KEY))
      sit_avatar (anim => g_anim_set[h], ava => i, k => g_avatar[i]);
  }
}

//-------------------------------------------------------


SamuroSamuro27/04Sent: 27/04/2024 01:28:362 / 3Message 2 from 3
ce script est mieux que l'ancien, quand on clique sur le meuble l'avatar s'assied sur la position d'assise la plus proche, et cela (contrairement au script précédent) indépendamment du x,y,z du mesh

<<<First<<prev.>next.>>>Last>RepReply^Discussion ^vDiscussion vDelDelete the discussion