Games-3D

 view release on metacpan or  search on metacpan

lib/Games/3D/World.pm  view on Meta::CPAN

  $self;
  }

sub reload
  {
  my $self = shift;

  $self->load_from_file($self->{file});
  }

sub register
  {
  # register an object with yourself
  my ($self,$obj) = @_;

  $obj->{id} = ID();				# get it a new ID
  $self->{things}->{$obj->{id}} = $obj;   	# store it
  $self->{things}->{_world} = $self;		# give thing access to us
  if ($obj->{visible})
    {
    $self->{render}->{$obj->{id}} = $obj;   	# store it
    }
  if ($obj->{think_time} != 0)
    {
    $self->{think}->{$obj->{id}} = $obj;   	# store it
    }

  $self;
  }

sub unregister
  {
  # should ONLY be called via $thing->{_world}->unregister($thing) !
  my ($self,$thing) = @_;

  my $id = $thing->{id};
  delete $self->{render}->{$id};
  delete $self->{things}->{$id};
  # tricky, what happens if called inside update()?  
  delete $self->{think}->{$id};
 
  $self;
  }

sub things
  {
  # get count of things
  my ($self) = @_;

  scalar keys %{$self->{things}};
  }

sub thinkers
  {
  # get count of thinking things
  my ($self) = @_;

  scalar keys %{$self->{think}};
  }

sub update
  {
  my ($self,$now) = @_;

  $self->{time} = $now;				# cache time
  foreach my $id (keys %{$self->{think}})
    {
    my $thing = $self->{think}->{$id};
    if ($thing->{next_think} >= $now)
      {
      $thing->think($now);
      }
    # if the thing is in transition between states, let it update itself
    $thing->update($now) if $thing->{state_endtime} != 0;

    # XXX TODO: does not handle things that no longer want to think()
    }
  $self;
  }

sub time
  {
  my $self = shift;

  $self->{time};
  }

sub render
  {
  my ($self,$now,$callback) = @_;

  foreach my $id (keys %{$self->{render}})
    {
    &$callback ( $now, $self->{render}->{$id} );
    }
  $self;
  }

sub create
  {
  # create an object based on a template (class name)
  my ($self,$class) = @_;

  return undef if !exists $self->{templates}->{$class};
  $self->{templates}->{$class}->create_thing();
  }

sub find_template
  {
  # given a class name, return the template object for it
  my ($self,$class) = @_;

  $self->{templates}->{$class};
  }

sub id { 0; }

1;

__END__



( run in 2.568 seconds using v1.01-cache-2.11-cpan-5837b0d9d2c )