forked from adamredwoods/minib3d-monkey
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tentity.monkey
2215 lines (1417 loc) · 42.4 KB
/
tentity.monkey
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
Import minib3d
Import minib3d.math.matrix
Import monkey.math
Import minib3d.tbone
''NOTES:
'' - use EntityListAdd()
'' - test transform point, using different global matrix approach, not sure if needed or not
'' - added CollisionSetup(type,picktype,x, [y,z,w,d,h]) for faster setup
'' -- if you need faster operations on mobile, you'll need a fixed-point math library
'' -- note: loc_mat is not updated if parent transformations... use px,py,pz,rx,ry,rz
'' --pitch is flipped, z pos is flipped
Const FXFLAG_NONE% = 0
Const FXFLAG_FULLBRIGHT% = 1
Const FXFLAG_VERTEXCOLORS% = 2
Const FXFLAG_FLATSHADE% = 4
Const FXFLAG_DISABLE_FOG% = 8
Const FXFLAG_DISABLE_CULLING% = 16
Const FXFLAG_FORCE_ALPHA% = 32
Const FXFLAG_DISABLE_DEPTH% = 64
Class TEntity
Const inverse_255:Float = 1.0/255.0
Global entity_list:EntityList<TEntity> = New EntityList<TEntity>
Field child_list:EntityList<TEntity> = New EntityList<TEntity>
Field entity_link:list.Node<TEntity> '' entity_list node, stored for quick removal of entity from list
Field parent_link:list.Node<TEntity> '' allows removal from parent's child_list
Field pick_link:list.Node<TEntity>
Field parent:TEntity
Field mat:Matrix=New Matrix 'global matrix
Field loc_mat:Matrix = New Matrix ''local matrix 'rot 'trans 'scale ''-- note: this isnt always kept up-to-date
'Field mat_sp:Matrix ''moved to TSprite
Field px#,py#,pz#,sx#=1.0,sy#=1.0,sz#=1.0,rx#,ry#,rz#,qw#,qx#,qy#,qz#
'Field gpx#=0.0,gpy#=0.0,gpz#=0.0 ''global position
'Field grx#=0.0,gry#=0.0,grz#=0.0 ''global rotation
Field gsx#=1.0,gsy#=1.0,gsz#=1.0 ''global scale
Field name$
Field classname$
Field hide:Bool =False
Field order%,alpha_order#
Field auto_fade%,fade_near#,fade_far#,fade_alpha#
Field cull_radius#
Field brush:TBrush=New TBrush
Field shader_brush:TShader ''don't forget to fill in copy, etc.
Field anim:Int ' =1 if mesh contains bone anim data, =2 if vertex anim data
Field anim_render:Int ' true to render as anim mesh
Field anim_mode:Int
Field anim_time#
Field anim_speed#
Field anim_seq:Int
Field anim_trans:Int
Field anim_dir:Int=1 ' 1=forward, -1=backward
Field anim_seqs_first:Int[1]
Field anim_seqs_last:Int[1]
Field no_seqs:Int=0
Field anim_update:Int
Field no_collisions:Int
Field collision:TCollision = New TCollision
Field collision_pair:TCollisionPair = New TCollisionPair
Field pick_mode%,obscurer%
' used by TCollisions
Field old_x#
Field old_y#
Field old_z#
'' used by TCamera for camera layer
Field use_cam_layer:Bool = False
Field cam_layer:TCamera
Field frustum_cache:Int ''frustum cache
''blitz3d functions-- can be deprecated
Global global_mat:Matrix = New Matrix
Global tformed_x#
Global tformed_y#
Global tformed_z#
''internal temp use
Private
Global temp_mat:Matrix = New Matrix
Public
Method CopyEntity:TEntity(parent_ent:TEntity=Null) Abstract
Method Update(cam:TCamera=Null) Abstract
Method New()
mat.LoadIdentity()
loc_mat.LoadIdentity()
End
Method CopyBaseEntityTo:Void(ent:TEntity, parent_ent:TEntity=Null)
' copy contents of child list before adding parent
For Local ent2:TEntity=Eachin child_list
ent2.CopyEntity(ent)
Next
' lists
' add parent, then add to list
ent.AddParent(parent_ent)
ent.entity_link = entity_list.EntityListAdd(ent)
' add to collision entity list
If collision.type<>0
TCollisionPair.ent_lists[collision.type].AddLast(ent)
Endif
' add to pick entity list
If pick_mode<>0
ent.pick_link = TPick.ent_list.AddLast(ent)
Endif
' update matrix
If ent.parent<>Null
ent.mat.Overwrite(ent.parent.mat)
Else
ent.mat.LoadIdentity()
Endif
' copy entity info
ent.mat.Multiply(mat)
ent.loc_mat = loc_mat
ent.px=px
ent.py=py
ent.pz=pz
ent.sx=sx
ent.sy=sy
ent.sz=sz
ent.rx=rx
ent.ry=ry
ent.rz=rz
ent.qw=qw
ent.qx=qx
ent.qy=qy
ent.qz=qz
ent.gsx = gsx
ent.gsy = gsy
ent.gsz = gsz
ent.name=name
ent.classname=classname
ent.order=order
ent.hide=False
ent.auto_fade=auto_fade
ent.fade_near=fade_near
ent.fade_far=fade_far
ent.brush=brush.Copy()
ent.anim=anim
ent.anim_render=anim_render
ent.anim_mode=anim_mode
ent.anim_time=anim_time
ent.anim_speed=anim_speed
ent.anim_seq=anim_seq
ent.anim_trans=anim_trans
ent.anim_dir=anim_dir
ent.anim_seqs_first=anim_seqs_first[..]
ent.anim_seqs_last=anim_seqs_last[..]
ent.no_seqs=no_seqs
ent.anim_update=anim_update
ent.cull_radius=cull_radius
'ent.collision_type=collision_type
ent.collision = collision.Copy()
ent.pick_mode=pick_mode
ent.obscurer=obscurer
ent.use_cam_layer = use_cam_layer
ent.cam_layer = cam_layer
End
Method FreeEntity()
If entity_link Then entity_link.Remove()
' remove from collision entity lists
If collision.type<>0 collision_pair.ListRemove(Self, collision.type)
' remove from pick entity list
If pick_mode<>0 Then pick_link.Remove()
' free self from parent's child_list
If parent<>Null
parent_link.Remove()
Endif
parent=Null
mat=New Matrix
brush=New TBrush
' free children entities
For Local ent:TEntity =Eachin child_list
ent.FreeEntity()
ent=Null
Next
child_list.Clear()
End
''
'' method properties return GLOBAL position
''
Method X:Float() Property
Return mat.grid[3][0]
End
Method Y:Float() Property
Return mat.grid[3][1]
End
Method Z:Float() Property
Return -mat.grid[3][2]
End
Method X:Void(xx:Float) Property
mat.grid[3][0] = xx
End
Method Y:Void(yy:Float) Property
mat.grid[3][1] = yy
End
Method Z:Void(zz:Float) Property
mat.grid[3][2] = -zz
End
' Entity movement
Method PositionEntity(e:TEntity)
PositionEntity(e.X,e.Y,e.Z,True)
End
Method PositionEntity(x#,y#,z#,glob=False)
''negate z for opengl
z=-z
' conv glob to local. x/y/z always local to parent or global if no parent
If glob=True And parent<>Null
'px = x; py= y; pz= z
'UpdateMatTrans(True)
'If child_list.IsEmpty()<>True Then UpdateChildren(Self,1)
'Return
temp_mat=parent.mat.Copy().Inverse()
'
'z=-z
'x=(x+parent.mat.grid[3][0])
'y=(y+parent.mat.grid[3][1])
'z=(z+parent.mat.grid[3][2])
Local psx#=parent.gsx
Local psy#=parent.gsy
Local psz#=parent.gsz
temp_mat.InverseScale(psx,psy,psz)
'temp_mat.Inverse() ''only 3x3 inverse
Local pos:Float[] = temp_mat.TransformPoint(x,y,z) '-z
x= pos[0]
y= pos[1]
z= pos[2]
Endif
'' treat bones differently
If TBone(Self) <> Null Then TBone(Self).PositionBone(x,y,z,glob); Return
px=x
py=y
pz=z
If parent<>Null
''global
'mat.Overwrite(parent.mat)
'UpdateMat()
UpdateMatTrans()
Else
''local
'UpdateMat(True)
UpdateMatTrans(True)
Endif
If child_list.IsEmpty()<>True Then UpdateChildren(Self,1)
End
Method MoveEntity(mx#,my#,mz#)
mz=-mz ' opengl -z
Local pos:Float[]
If gsx<>1.0 Then mx = mx/gsx
If gsy<>1.0 Then my = my/gsy
If gsz<>1.0 Then mz = mz/gsz
''rotate the direction
pos = mat.TransformPoint(mx,my,mz,0.0) ''0 creates an offset, not a point
px=px+pos[0]
py=py+pos[1]
pz=pz+pos[2]
'' treat bones differently
If TBone(Self) <> Null Then TBone(Self).PositionBone(px,py,pz); Return
If parent<>Null
''global
'mat.Overwrite(parent.mat)
'UpdateMat()
UpdateMatTrans()
Else
''Local
'UpdateMat(True)
UpdateMatTrans(True)
Endif
If child_list.IsEmpty()<>True Then UpdateChildren(Self,1)
End
Method TranslateEntity(tx#,ty#,tz#,glob=False)
'Local tx#=x
'Local ty#=y
tz=-tz
' conv glob to local. x/y/z always local to parent or global if no parent
If glob=True And parent<>Null
'Local temp_mat:Matrix=New Matrix
temp_mat = parent.mat.Copy().Inverse()
'temp_mat.LoadIdentity()
'temp_mat.Rotate(-ax,-ay,-az)
'temp_mat.Translate(tx,ty,tz)
tx=temp_mat.grid[3][0]
ty=temp_mat.grid[3][1]
tz=temp_mat.grid[3][2]
Endif
px=px+tx
py=py+ty
pz=pz+tz
'' treat bones differently
If TBone(Self) <> Null Then TBone(Self).PositionBone(px,py,pz, glob); Return
If parent<>Null
''global
'mat.Overwrite(parent.mat)
'UpdateMat()
UpdateMatTrans()
Else
''local
'UpdateMat(True)
UpdateMatTrans(True)
Endif
If child_list.IsEmpty()<>True Then UpdateChildren(Self,1)
End
Method ScaleEntity(e:TEntity)
ScaleEntity(e.EntityScaleX,e.EntityScaleY,e.EntityScaleZ,True)
End
Method ScaleEntity(x#,y#,z#,glob=False)
sx=x
sy=y
sz=z
' conv glob to local. x/y/z always local to parent or global if no parent
If glob=True And parent<>Null
sx = sx/parent.gsx
sy = sy/parent.gsy
sz = sz/parent.gsz
Endif
'' treat bones differently
If TBone(Self) <> Null
TBone(Self).ScaleBone(sx,sy,sz,glob)
Return
Endif
If parent<>Null
'mat.Overwrite(parent.mat)
'UpdateMat()
UpdateMatRot()
Else
'UpdateMat(True)
UpdateMatRot(True)
Endif
If child_list.IsEmpty()<>True Then UpdateChildren(Self)
End
Method RotateEntity(e:TEntity)
RotateEntity(e.rx, e.ry, e.rz, True)
End
Method RotateEntity(x#,y#,z#,glob=False)
rx=-x
ry=y
rz=z
' conv glob to local. pitch/yaw/roll always local to parent or global if no parent
If glob=True And parent<>Null
rx=rx+parent.EntityPitch(True)
ry=ry-parent.EntityYaw(True)
rz=rz-parent.EntityRoll(True)
Endif
'' treat bones differently
If TBone(Self) <> Null Then TBone(Self).RotateBone(rx,ry,rz, glob); Return
If parent<>Null
'mat.Overwrite(parent.mat)
'UpdateMat()
UpdateMatRot()
Else
'UpdateMat(True)
UpdateMatRot(True)
Endif
If child_list.IsEmpty()<>True Then UpdateChildren(Self)
End
Method TurnEntity(x#,y#,z#,glob=False)
' conv glob to local. x/y/z always local to parent or global if no parent
If glob=True And parent<>Null
'
Endif
rx=rx+(-x)
ry=ry+y
rz=rz+z
'' treat bones differently
If TBone(Self) <> Null Then TBone(Self).RotateBone(rx,ry,rz,glob); Return
If parent<>Null
'mat.Overwrite(parent.mat)
'UpdateMat()
UpdateMatRot()
Else ' glob=true or false
'UpdateMat(True)
UpdateMatRot(True)
Endif
If child_list.IsEmpty()<>True Then UpdateChildren(Self)
End
' Function by mongia2
Method PointEntity(target_ent:TEntity,roll#=0)
Local x#=target_ent.EntityX(True)
Local y#=target_ent.EntityY(True)
Local z#=target_ent.EntityZ(True)
Local xdiff#=Self.EntityX(True)-x
Local ydiff#=Self.EntityY(True)-y
Local zdiff#=Self.EntityZ(True)-z
Local dist22#=Sqrt((xdiff*xdiff)+(zdiff*zdiff))
Local pitch#=ATan2(ydiff,dist22)
Local yaw#=ATan2(xdiff,-zdiff)
Self.RotateEntity pitch,yaw,roll,True
End
Method AlignToVector(vx:Float,vy:Float,vz:Float, axi:Int=1, rate:Float=1.0)
Local dvec:Vector = New Vector(vx,vy,-vz)
Local avec:Vector
Local cvec:Vector
'Local p1#, p2#, p3#
Local dd# = dvec.Length()
If dd < 0.0001 Then Return
dd=1.0/dd
dvec.Update(dvec.x*dd, dvec.y*dd, dvec.z*dd )
'dvec = dvec.Normalize()
''slerp or lerp between the dvec and the current matrix forward, up, or left axis
If (axi=1) Then cvec = New Vector(mat.grid[0][0],mat.grid[0][1],mat.grid[0][2])
If (axi=2) Then cvec = New Vector(mat.grid[1][0],mat.grid[1][1],mat.grid[1][2])
If (axi=3) Then cvec = New Vector(mat.grid[2][0],mat.grid[2][1],mat.grid[2][2])
cvec = cvec.Normalize()
''lerp is inaccurate, but only on large distances
If rate>=0.0
dvec.Update( (cvec.x+dvec.x)*rate+cvec.x, (cvec.y+dvec.y)*rate+cvec.y, (cvec.z+dvec.z)*rate+cvec.z )
dvec = dvec.Normalize()
Endif
''get axis to get our angle from (b/c rotations start at axis)
If (axi=1) Then avec = New Vector(1.0,0.0,0.0)
If (axi=2) Then avec = New Vector(0.0,1.0,0.0)
If (axi=3) Then avec = New Vector(0.0,0.0,1.0)
''use axis-angle quat for slerp and convert to matrix,euler
Local angle:Float = ACos( dvec.Dot(avec) )
Local axis:Vector = dvec.Cross(avec)
If angle < 0.00001
'mat.LoadIdentity()
mat.grid[0][0]=gsx; mat.grid[1][0]=0.0; mat.grid[2][0]=0.0
mat.grid[0][1]=0.0; mat.grid[1][1]=gsy; mat.grid[2][1]=0.0
mat.grid[0][2]=0.0; mat.grid[1][2]=0.0; mat.grid[2][2]=gsz
'mat.Scale(gsx,gsy,gsz)
Return
Elseif angle > 179.9999
''flip
'ent.mat.LoadIdentity()
axis.Update(0.0,-1.0,0.0)
angle=179.9
Endif
axis = axis.Normalize()
Local c:Float = Cos(angle)
Local s:Float = Sin(angle)
Local t:Float = 1.0 - c
'' axis is normalised, include scaling
'Local new_mat:Matrix = New Matrix
temp_mat.grid[0][0] = (c + axis.x*axis.x*t) *Self.sx
temp_mat.grid[1][1] = (c + axis.y*axis.y*t) *Self.sy
temp_mat.grid[2][2] = (c + axis.z*axis.z*t) *Self.sz
Local tmp1:Float = axis.x*axis.y*t
Local tmp2:Float = axis.z*s
temp_mat.grid[1][0] = (tmp1 + tmp2) *Self.sy
temp_mat.grid[0][1] = (tmp1 - tmp2) *Self.sx
tmp1 = axis.x*axis.z*t
tmp2 = axis.y*s
temp_mat.grid[2][0] = (tmp1 - tmp2) *Self.sz
temp_mat.grid[0][2] = (tmp1 + tmp2) *Self.sx
tmp1 = axis.y*axis.z*t
tmp2 = axis.x*s
temp_mat.grid[2][1] = (tmp1 + tmp2) *Self.sz
temp_mat.grid[1][2] = (tmp1 - tmp2) *Self.sy
temp_mat.grid[3][0] = mat.grid[3][0] 'Self.px
temp_mat.grid[3][1] = mat.grid[3][1] 'Self.py
temp_mat.grid[3][2] = mat.grid[3][2] 'Self.pz
temp_mat.grid[3][3] = 1.0
If parent<>Null
mat.Overwrite(parent.mat)
mat.Multiply(temp_mat)
Else
mat.Overwrite(temp_mat )
loc_mat.Overwrite(temp_mat)
Endif
rx = mat.GetPitch()
ry = mat.GetYaw()
rz = mat.GetRoll()
Self.UpdateChildren(Self)
End
' Entity animation
' load anim seq - copies anim data from mesh to self
Method LoadAnimSeq(file:String)
' mesh that we will load anim seq from
Local mesh:TMesh=TModel.LoadAnimB3D(file)
If anim=False Then Return 0 ' self contains no anim data
If mesh.anim=False Then Return 0 ' mesh contains no anim data
no_seqs=no_seqs+1
' expand anim_seqs array
anim_seqs_first=anim_seqs_first.Resize(no_seqs+1)
anim_seqs_last=anim_seqs_last.Resize(no_seqs+1)
' update anim_seqs array
anim_seqs_first[no_seqs]=anim_seqs_last[0]
anim_seqs_last[no_seqs]=anim_seqs_last[0]+mesh.anim_seqs_last[0]
' update anim_seqs_last[0] - sequence 0 is for all frames, so this needs to be increased
' must be done after updating anim_seqs array above
anim_seqs_last[0]=anim_seqs_last[0]+mesh.anim_seqs_last[0]
If mesh<>Null
' go through all bones belonging to self
For Local bone:TBone=Eachin TMesh(Self).bones
' find bone in mesh that matches bone in self - search based on bone name
Local mesh_bone:TBone=TBone(TEntity(mesh).FindChild(bone.name))
If mesh_bone<>Null
' resize self arrays first so the one empty element at the end is removed
bone.keys.flags=bone.keys.flags[..bone.keys.flags.length-1]
bone.keys.px=bone.keys.px[..bone.keys.px.length-1]
bone.keys.py=bone.keys.py[..bone.keys.py.length-1]
bone.keys.pz=bone.keys.pz[..bone.keys.pz.length-1]
bone.keys.sx=bone.keys.sx[..bone.keys.sx.length-1]
bone.keys.sy=bone.keys.sy[..bone.keys.sy.length-1]
bone.keys.sz=bone.keys.sz[..bone.keys.sz.length-1]
bone.keys.qw=bone.keys.qw[..bone.keys.qw.length-1]
bone.keys.qx=bone.keys.qx[..bone.keys.qx.length-1]
bone.keys.qy=bone.keys.qy[..bone.keys.qy.length-1]
bone.keys.qz=bone.keys.qz[..bone.keys.qz.length-1]
' add mesh bone key arrays to self bone key arrays
bone.keys.frames=anim_seqs_last[0]
bone.keys.flags=bone.keys.flags+mesh_bone.keys.flags
bone.keys.px=bone.keys.px+mesh_bone.keys.px
bone.keys.py=bone.keys.py+mesh_bone.keys.py
bone.keys.pz=bone.keys.pz+mesh_bone.keys.pz
bone.keys.sx=bone.keys.sx+mesh_bone.keys.sx
bone.keys.sy=bone.keys.sy+mesh_bone.keys.sy
bone.keys.sz=bone.keys.sz+mesh_bone.keys.sz
bone.keys.qw=bone.keys.qw+mesh_bone.keys.qw
bone.keys.qx=bone.keys.qx+mesh_bone.keys.qx
bone.keys.qy=bone.keys.qy+mesh_bone.keys.qy
bone.keys.qz=bone.keys.qz+mesh_bone.keys.qz
Endif
Next
Endif
mesh.FreeEntity()
Return no_seqs
End
Method ExtractAnimSeq(first_frame,last_frame,seq=0)
no_seqs=no_seqs+1
' expand anim_seqs array
anim_seqs_first=anim_seqs_first.Resize(no_seqs+1)
anim_seqs_last=anim_seqs_last.Resize(no_seqs+1)
' if seq specifed then extract anim sequence from within existing sequnce
Local offset=0
If seq<>0
offset=anim_seqs_first[seq]
Endif
anim_seqs_first[no_seqs]=first_frame+offset
anim_seqs_last[no_seqs]=last_frame+offset
Return no_seqs
End
Method ActivateBones:Void()
anim_render = True
anim_trans=0
anim = 4
'nim_mode = 4
anim_update = True
End
Method ActivateVertexAnim:Void()
anim_render = True
anim_trans=0
anim = 2
anim_update = True
End
'' 0=none, 1=repeat, 2=ping-pong, 3=play once, 4 = boned anim
Method Animate:Void(mode:Int=1,speed:Float=1.0,seq:Int=0,trans:Int=0)
anim_mode=mode
anim_update=True ' update anim for all modes (including 0)
If mode<>4
anim_speed=speed
anim_seq=seq
anim_trans=trans
If Not anim_time Then anim_time=anim_seqs_first[seq]
Else
ActivateBones()
Endif
If trans>0
anim_time=0
Endif
End
Method SetAnimTime(time#, seq%=0)
anim_mode=-1 ' use a mode of -1 for setanimtime
anim_speed=0
anim_seq=seq
anim_trans=0
anim_time=time
anim_update=False ' set anim_update to false so UpdateWorld won't animate entity
Local first=anim_seqs_first[anim_seq]
Local last=anim_seqs_last[anim_seq]
Local first2last=anim_seqs_last[anim_seq]-anim_seqs_first[anim_seq]
time=time+first ' offset time so that anim time of 0 will equal first frame of sequence
If time>last And first2last>0 ' check that first2last>0 to prevent infinite loop
Repeat
time=time-first2last
Until time<=last
Endif
If time<first And first2last>0 ' check that first2last>0 to prevent infinite loop
Repeat
time=time+first2last
Until time>=first
Endif
If anim = 1 Then TAnimation.AnimateMesh(Self,time,first,last)
If anim = 2 Then TAnimation.AnimateVertex(Self,time,first,last)
anim_time=time ' update anim_time# to equal time#
End
Method AnimSeq:Int()
Return anim_seq ' current anim sequence
End
Method AnimLength:Int()
Return anim_seqs_last[anim_seq]-anim_seqs_first[anim_seq] ' no of frames in anim sequence
End
Method AnimTime:Float()
' if animation in transition, return 0 (anim_time actually will be somewhere between 0 and 1)
If anim_trans>0 Then Return 0
' for animate and setanimtime we want to return anim_time starting from 0 and ending at no. of frames in sequence
If anim_mode>0 Or anim_mode=-1
Return anim_time-anim_seqs_first[anim_seq]
Endif
Return 0
End
Method Animating:Bool()
If anim_trans>0 Then Return True
If anim_mode>0 Then Return True
Return False
End
' Entity control
Method EntityColor(r#,g#,b#)
brush.red =r * inverse_255
brush.green=g * inverse_255
brush.blue =b * inverse_255
End
Method EntityColorFloat(r#,g#,b#)
brush.red =r
brush.green=g
brush.blue =b
End
Method EntityAlpha(a#)
brush.alpha=a
End
Method EntityShininess(s#)
brush.shine=s
End
''EntityTexture()
Method EntityTexture(texture:TTexture,frame=0,index=0)
brush.tex[index]=texture
If index+1>brush.no_texs Then brush.no_texs=index+1
If frame<0 Then frame=0
If frame>texture.no_frames-1 Then frame=texture.no_frames-1
brush.tex[index].tex_frame=frame
If frame>0 And texture.no_frames>1
''move texture anim
Local x:Int = frame Mod texture.frame_xstep
Local y:Int =( frame/texture.frame_ystep) Mod texture.frame_ystep
brush.tex[index].u_pos = x*texture.frame_ustep
brush.tex[index].v_pos = y*texture.frame_vstep
Endif
End
Method AnimateTexture(frame:Int, loop:Bool=False, i:Int=0)
If Not brush Or Not brush.tex[0] Then Return
Local tf:Int = brush.tex[i].no_frames-1
Local nframe:Int = tf, bframe:Int = 0
If loop And tf
nframe = frame Mod tf; bframe = frame - (-frame Mod tf)
Endif
If frame<0 Then frame=bframe
If frame>tf Then frame= nframe
brush.tex[i].tex_frame=frame
If frame>0 And brush.tex[i].no_frames>1
''move texture
Local x:Int = frame Mod brush.tex[i].frame_xstep
Local y:Int =( frame/brush.tex[i].frame_ystep) Mod brush.tex[i].frame_ystep
brush.tex[i].u_pos = x*brush.tex[i].frame_ustep
brush.tex[i].v_pos = y*brush.tex[i].frame_vstep
Endif
End
Method EntityBlend(blend_no%)
brush.blend=blend_no
If TMesh(Self)<>Null
' overwrite surface blend modes with master blend mode
For Local surf:TSurface=Eachin TMesh(Self).surf_list
If surf.brush<>Null
surf.brush.blend=brush.blend
Endif
Next
Endif
End
'' EntityFX(fx_no)
''0: nothing (default)
''1: full-bright
''2: use vertex colors instead of brush color
''4: flatshaded
''8: disable fog
''16: disable backface culling
''32: force alpha-blending
''64: no depth test
Method EntityFX(fx_no%)
brush.fx=fx_no
End
Method EntityAutoFade(near#,far#)
auto_fade=True
fade_near=near
fade_far=far
End
Method PaintEntity(bru:TBrush)
If TShader(bru) = bru
If Not brush Then brush = bru
'Dprint "TBrush: shader paint"
shader_brush = TShader(bru) '.Copy()
Else
brush = bru.Copy()
Endif
End
'' does not paint with a copy
Method PaintEntityGlobal(bru:TBrush)
If TShader(bru) = bru
If Not brush Then brush = bru
'Dprint "TBrush: shader paint global"
shader_brush = TShader(bru) '.Copy()
Else
brush = bru