| 1 | |||||||||||||||||||||||||||||
| 2 | # World - contains everything in the game world | ||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | package Games::3D::World; | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | # (C) by Tels <http://bloodgate.com/> | ||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use vars qw/$VERSION/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use Games::3D::Template; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 2 | $VERSION = '0.07'; | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | ############################################################################## | ||||||||||||||||||||||||||||
| 15 | # protected vars | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | { | ||||||||||||||||||||||||||||
| 18 | 2 | my $id = 1; | |||||||||||||||||||||||||||
| 19 | 0 | ▶ | sub ID { return $id++;} | ||||||||||||||||||||||||||
|
Subroutine
ID: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 20 | 1 | ▶ | sub _reset_ID { $id = 1; } | ||||||||||||||||||||||||||
|
Subroutine
_reset_ID: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | } | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | ############################################################################## | ||||||||||||||||||||||||||||
| 24 | # methods | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | sub new | ||||||||||||||||||||||||||||
| 27 | 2 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||
| 28 | # create a new world | ||||||||||||||||||||||||||||
| 29 | 1 | my $class = shift; | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 1 | my $self = bless {}, $class; | |||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 1 | $self->{things} = { }; | |||||||||||||||||||||||||||
| 34 | 1 | $self->{render} = { }; | |||||||||||||||||||||||||||
| 35 | 1 | $self->{thinks} = { }; | |||||||||||||||||||||||||||
| 36 | 1 | $self->{templates} = {}; | |||||||||||||||||||||||||||
| 37 | 1 | _reset_ID(); | |||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 1 | ▶ | if (@_ == 2) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | { | ||||||||||||||||||||||||||||
| 41 | 0 | $self->load_templates($_[0]); | |||||||||||||||||||||||||||
| 42 | 0 | $self->load_from_file($_[1]); | |||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 1 | $self->{time} = 0; | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 1 | $self; | |||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | sub load_from_file | ||||||||||||||||||||||||||||
| 51 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
load_from_file: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 52 | 0 | my ($self,$file) = @_; | |||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 0 | $self->{file} = $file; | |||||||||||||||||||||||||||
| 55 | 0 | $self->{things} = { }; | |||||||||||||||||||||||||||
| 56 | 0 | _reset_ID(); | |||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 0 | $self; | |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | sub load_templates | ||||||||||||||||||||||||||||
| 62 | 1 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
load_templates: called CC=7 SCAR=21.0
Pod
documented
| |||||||||||||||||||||||||||||
| 63 | 1 | my ($self,$file) = @_; | |||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 1 | ▶ | if (!ref($file)) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | { | ||||||||||||||||||||||||||||
| 67 | # got filename, so read in data | ||||||||||||||||||||||||||||
| 68 | 1 | ▶ | open FILE, "$file" or die ("Cant read $file: $!"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | 1 | local $/ = undef; # slurp mode | |||||||||||||||||||||||||||
| 70 | 1 | my $doc = <FILE>; | |||||||||||||||||||||||||||
| 71 | 1 | $file = \$doc; | |||||||||||||||||||||||||||
| 72 | 1 | close FILE; | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 1 | $self->{templates} = {}; | |||||||||||||||||||||||||||
| 76 | 1 | my @objects = Games::3D::Template::from_string($$file); | |||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 1 | ▶ | if (@objects == 1 && !ref($objects[0])) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
@objects == 1 and not ref $objects[0]
MC/DC0%0/2
@objects == 1 and not ref $objects[0]
@objects == 1
not ref $objects[0]
Truth table33%1/3
A and not(B)
A= @objects == 1B= not ref $objects[0]
| |||||||||||||||||||||||||||||
| 79 | { | ||||||||||||||||||||||||||||
| 80 | 0 | die($objects[0]); | |||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | 1 | foreach my $o (@objects) | |||||||||||||||||||||||||||
| 83 | { | ||||||||||||||||||||||||||||
| 84 | 9 | ▶ | if (exists $self->{templates}->{$o->{class}}) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | { | ||||||||||||||||||||||||||||
| 86 | 0 | warn ("Template for class $o->{class} already seen"); | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | 9 | $self->{templates}->{$o->{class}} = $o; | |||||||||||||||||||||||||||
| 89 | 9 | $o->{_world} = $self; | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 1 | $self; | |||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | sub templates | ||||||||||||||||||||||||||||
| 96 | 1 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
templates: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 97 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 98 | 1 | scalar keys %{$self->{templates}}; | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | sub save_templates | ||||||||||||||||||||||||||||
| 102 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
save_templates: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 103 | 0 | my ($self,$file) = @_; | |||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 0 | $self; | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | sub save_to_file | ||||||||||||||||||||||||||||
| 109 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
save_to_file: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 110 | 0 | my ($self,$file) = @_; | |||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 0 | $self->{file} = $file; | |||||||||||||||||||||||||||
| 113 | 0 | $self; | |||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | sub reload | ||||||||||||||||||||||||||||
| 117 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
reload: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 118 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 0 | $self->load_from_file($self->{file}); | |||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | sub register | ||||||||||||||||||||||||||||
| 124 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
register: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 125 | # register an object with yourself | ||||||||||||||||||||||||||||
| 126 | 0 | my ($self,$obj) = @_; | |||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 0 | $obj->{id} = ID(); # get it a new ID | |||||||||||||||||||||||||||
| 129 | 0 | $self->{things}->{$obj->{id}} = $obj; # store it | |||||||||||||||||||||||||||
| 130 | 0 | $self->{things}->{_world} = $self; # give thing access to us | |||||||||||||||||||||||||||
| 131 | 0 | ▶ | if ($obj->{visible}) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | { | ||||||||||||||||||||||||||||
| 133 | 0 | $self->{render}->{$obj->{id}} = $obj; # store it | |||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | 0 | ▶ | if ($obj->{think_time} != 0) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 136 | { | ||||||||||||||||||||||||||||
| 137 | 0 | $self->{think}->{$obj->{id}} = $obj; # store it | |||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 0 | $self; | |||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | sub unregister | ||||||||||||||||||||||||||||
| 144 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
unregister: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 145 | # should ONLY be called via $thing->{_world}->unregister($thing) ! | ||||||||||||||||||||||||||||
| 146 | 0 | my ($self,$thing) = @_; | |||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 0 | my $id = $thing->{id}; | |||||||||||||||||||||||||||
| 149 | 0 | delete $self->{render}->{$id}; | |||||||||||||||||||||||||||
| 150 | 0 | delete $self->{things}->{$id}; | |||||||||||||||||||||||||||
| 151 | # tricky, what happens if called inside update()? | ||||||||||||||||||||||||||||
| 152 | 0 | delete $self->{think}->{$id}; | |||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 0 | $self; | |||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | sub things | ||||||||||||||||||||||||||||
| 158 | 1 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
things: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 159 | # get count of things | ||||||||||||||||||||||||||||
| 160 | 1 | my ($self) = @_; | |||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 1 | scalar keys %{$self->{things}}; | |||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | sub thinkers | ||||||||||||||||||||||||||||
| 166 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
thinkers: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 167 | # get count of thinking things | ||||||||||||||||||||||||||||
| 168 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | 0 | scalar keys %{$self->{think}}; | |||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | sub update | ||||||||||||||||||||||||||||
| 174 | 1 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
update: called CC=4 SCAR=20.4
Pod
documented
| |||||||||||||||||||||||||||||
| 175 | 1 | my ($self,$now) = @_; | |||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | 1 | $self->{time} = $now; # cache time | |||||||||||||||||||||||||||
| 178 | 1 | foreach my $id (keys %{$self->{think}}) | |||||||||||||||||||||||||||
| 179 | { | ||||||||||||||||||||||||||||
| 180 | 0 | my $thing = $self->{think}->{$id}; | |||||||||||||||||||||||||||
| 181 | 0 | ▶ | if ($thing->{next_think} >= $now) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 182 | { | ||||||||||||||||||||||||||||
| 183 | 0 | $thing->think($now); | |||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||
| 185 | # if the thing is in transition between states, let it update itself | ||||||||||||||||||||||||||||
| 186 | 0 | ▶ | $thing->update($now) if $thing->{state_endtime} != 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | # XXX TODO: does not handle things that no longer want to think() | ||||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||
| 190 | 1 | $self; | |||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | sub time | ||||||||||||||||||||||||||||
| 194 | 1 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
time: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 195 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | 1 | $self->{time}; | |||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | sub render | ||||||||||||||||||||||||||||
| 201 | 1 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
render: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||
| 202 | 1 | my ($self,$now,$callback) = @_; | |||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | 1 | foreach my $id (keys %{$self->{render}}) | |||||||||||||||||||||||||||
| 205 | { | ||||||||||||||||||||||||||||
| 206 | 0 | &$callback ( $now, $self->{render}->{$id} ); | |||||||||||||||||||||||||||
| 207 | } | ||||||||||||||||||||||||||||
| 208 | 1 | $self; | |||||||||||||||||||||||||||
| 209 | } | ||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | sub create | ||||||||||||||||||||||||||||
| 212 | 3 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
create: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||
| 213 | # create an object based on a template (class name) | ||||||||||||||||||||||||||||
| 214 | 3 | my ($self,$class) = @_; | |||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | 3 | ▶ | return undef if !exists $self->{templates}->{$class}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 217 | 3 | $self->{templates}->{$class}->create_thing(); | |||||||||||||||||||||||||||
| 218 | } | ||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | sub find_template | ||||||||||||||||||||||||||||
| 221 | 6 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
find_template: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 222 | # given a class name, return the template object for it | ||||||||||||||||||||||||||||
| 223 | 6 | my ($self,$class) = @_; | |||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | 6 | $self->{templates}->{$class}; | |||||||||||||||||||||||||||
| 226 | } | ||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | 1 | ▶ | sub id { 0; } | ||||||||||||||||||||||||||
|
Subroutine
id: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | 2 | 1; | |||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | __END__ | ||||||||||||||||||||||||||||