forked from Racoonacoon/Ragnarok
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRagnarok.py
More file actions
3135 lines (2574 loc) · 111 KB
/
Ragnarok.py
File metadata and controls
3135 lines (2574 loc) · 111 KB
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
## Ragnarok Engine copyright 2010 Clinton Myers
## This library is free software: you can redistribute it and/or modify
## it under the terms of the GNU Lesser General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU Lesser General Public License for more details.
##
## You should have received a copy of the GNU Lesser General Public License
## along with this program. If not, see <http://www.gnu.org/licenses/lgpl.html>.
## Ragnarok Version 1.0
##
##
## Developer Note: This is a very early version of a work in progress. Several of the classes
## have many segments commented out and may not function properly. If you have any questions, contact
## me at lotusxp@live.com
import pygame, math, numbers, operator, random, StateTypes, os
os.environ['SDL_VIDEO_WINDOW_POS'] = 'center'
class Vector2(object):
def __init__(self, X = 0.0, Y = 0.0):
self.X = X
self.Y = Y
def __add__(self, other):
if isinstance(other, Vector2):
new_vec = Vector2()
new_vec.X = self.X + other.X
new_vec.Y = self.Y + other.Y
return new_vec
else:
raise TypeError("other must be of type Vector2")
def __radd__(self, other):
return self.__add__(other)
def __sub__(self, other):
if isinstance(other, Vector2):
new_vec = Vector2()
new_vec.X = self.X - other.X
new_vec.Y = self.Y - other.Y
return new_vec
else:
raise TypeError("other must be of type Vector2")
def __rsub__(self, other):
return self.__sub__(other)
def __mul__(self, value):
if isinstance(value, numbers.Number):
new_vec = self.copy()
new_vec.X = new_vec.X * value
new_vec.Y = new_vec.Y * value
return new_vec
else:
raise TypeError("value must be a number.")
def __rmul__(self, value):
return self.__mul__(value)
def __div__(self, value):
if isinstance(value, numbers.Number):
if not(value == 0):
new_vec = self.copy()
new_vec.X /= value
new_vec.Y /= value
return new_vec
else:
raise ZeroDivisionError("Cannot divide by zero.")
else:
raise TypeError("value must be a number.")
def __rdiv__(self, value):
return self.__div__(value)
def __eq__(self, other):
"""Check to see if two Vector2 objects are equal"""
if isinstance(other, Vector2):
if self.X == other.X \
and self.Y == other.Y:
return True
else:
raise TypeError("other must be of type Vector2")
return False
def __neg__(self):
return Vector2(-self.X, -self.Y)
def __getitem__(self, index):
if index > 1:
raise IndexError("Index must be less than 2")
if index == 0:
return self.X
else:
return self.Y
def __setitem__(self, index, value):
if index > 1:
raise IndexError("Index must be less than 2")
if index == 0:
self.X = value
else:
self.Y = value
def __str__(self):
return "<Vector2> [ " + str(self.X) + ", " + str(self.Y) + " ]"
def __len__(self):
return 2
#Define our properties
def zero():
"""Returns a Vector2 with all attributes set to 0"""
return Vector2(0, 0)
def one():
"""Returns a Vector2 with all attribures set to 1"""
return Vector2(1, 1)
def copy(self):
"""Create a copy of this Vector"""
new_vec = Vector2()
new_vec.X = self.X
new_vec.Y = self.Y
return new_vec
def length(self):
"""Gets the length of this Vector"""
return math.sqrt( (self.X * self.X) + (self.Y * self.Y) )
def normalize(self):
"""Gets the normalized Vector"""
length = self.length()
if length > 0:
self.X /= length
self.Y /= length
else:
print "Length 0, cannot normalize."
def normalize_copy(self):
"""Create a copy of this Vector, normalize it, and return it."""
vec = self.copy()
vec.normalize()
return vec
def distance(vec1, vec2):
"""Calculate the distance between two Vectors"""
if isinstance(vec1, Vector2) \
and isinstance(vec2, Vector2):
dist_vec = vec2 - vec1
return dist_vec.length()
else:
raise TypeError("vec1 and vec2 must be Vector2's")
def dot(vec1, vec2):
"""Calculate the dot product between two Vectors"""
if isinstance(vec1, Vector2) \
and isinstance(vec2, Vector2):
return ( (vec1.X * vec2.X) + (vec1.Y * vec2.Y) )
else:
raise TypeError("vec1 and vec2 must be Vector2's")
def angle(vec1, vec2):
"""Calculate the angle between two Vector2's"""
dotp = Vector2.dot(vec1, vec2)
mag1 = vec1.length()
mag2 = vec2.length()
result = dotp / (mag1 * mag2)
return math.acos(result)
def lerp(vec1, vec2, time):
"""Lerp between vec1 to vec2 based on time. Time is clamped between 0 and 1."""
if isinstance(vec1, Vector2) \
and isinstance(vec2, Vector2):
#Clamp the time value into the 0-1 range.
if time < 0:
time = 0
elif time > 1:
time = 1
x_lerp = vec1[0] + time * (vec2[0] - vec1[0])
y_lerp = vec1[1] + time * (vec2[1] - vec1[1])
return Vector2(x_lerp, y_lerp)
else:
raise TypeError("Objects must be of type Vector2")
def from_polar(degrees, magnitude):
"""Convert polar coordinates to Carteasian Coordinates"""
vec = Vector2()
vec.X = math.cos(math.radians(degrees)) * magnitude
#Negate because y in screen coordinates points down, oppisite from what is
#expected in traditional mathematics.
vec.Y = -math.sin(math.radians(degrees)) * magnitude
return vec
def component_mul(vec1, vec2):
"""Multiply the components of the vectors and return the result."""
new_vec = Vector2()
new_vec.X = vec1.X * vec2.X
new_vec.Y = vec1.Y * vec2.Y
return new_vec
def component_div(vec1, vec2):
"""Divide the components of the vectors and return the result."""
new_vec = Vector2()
new_vec.X = vec1.X / vec2.X
new_vec.Y = vec1.Y / vec2.Y
return new_vec
zero = staticmethod(zero)
one = staticmethod(one)
distance = staticmethod(distance)
dot = staticmethod(dot)
lerp = staticmethod(lerp)
from_polar = staticmethod(from_polar)
component_mul = staticmethod(component_mul)
component_div = staticmethod(component_div)
class Vector3(object):
"""Provides basic 3D Vector operations"""
def __init__(self, x = 0.0, y = 0.0, z = 0.0):
"""Constructs a Vector3 object with the default position of the object."""
#Check for potential problems while converting the values.
try:
self.x = float(x)
self.y = float(y)
self.z = float(z)
except (TypeError):
raise TypeError("x, y, and z must be a numerical type.")
def __len__(self):
"""Returns the number of attributes contained in this class."""
return 3
def __str__(self):
"""Builds a string representation of this object."""
return "<Vector3>: { " + str(self.x) + ", " + str(self.y) + ", " + str(self.z) + " }"
#Provide our operator overloaded methods
def __eq__(self, other):
"""Check to see if two Vector3 instances are equal"""
if not isinstance(other, Vector3):
raise TypeError("other must be of type Vector3")
if self.x == other.x \
and self.y == other.y \
and self.z == other.z:
return True
else:
return False
def __ne__(self, other):
"""Check to see if two Vector3 instances are not equal"""
if not isinstance(other, Vector3):
raise TypeError("other must be of type Vector3")
if not(self.x == other.x) \
or not(self.y == other.y) \
or not(self.z == other.z):
#True, the objects are not equal to each other.
return True
else:
#False, the objects are equal to each other
return False
def __add__(self, other):
"""Adds two Vector3 objects, or a single float to the x, y, and z attributes."""
if isinstance(other, Vector3):
vec3 = Vector3()
vec3.x = self.x + other.x
vec3.y = self.y + other.y
vec3.z = self.z + other.z
return vec3
else:
#The object isn't of a correct type, return self to prevent errors.
raise TypeError("other must be of type Vector3")
def __radd__(self, other):
return self.__add__(other)
def __sub__(self, other):
"""Subtract two Vector3 objects, or a single float from the x, y, and z attributes"""
if isinstance(other, Vector3):
vec3 = Vector3()
vec3.x = self.x - other.x
vec3.y = self.y - other.y
vec3.z = self.z - other.z
return vec3
else:
raise TypeError("other must be of type Vector3")
def __rsub__(self, other):
return self.__sub__(other)
def __mul__(self, other):
"""Multiply a Vector3 and a scalar."""
if isinstance(other, numbers.Number):
vec3 = Vector3()
vec3.x = self.x * other
vec3.y = self.y * other
vec3.z = self.z * other
return vec3
else:
raise TypeError("other must be a single number")
def __rmul__(self, other):
return self.__mul__(other)
def __div__(self, other):
"""Divide a Vector3 and a scalar."""
if operator.isNumberType(other):
vec3 = Vector3()
vec3.x = self.x / other
vec3.y = self.y / other
vec3.z = self.z / other
return vec3
else:
raise TypeError("other must be a single number")
def __rdiv__(self, other):
return self.__div__(other)
def __neg__(self):
"""Negate the Vector"""
neg_vec = Vector3()
neg_vec.x = -self.x
neg_vec.y = -self.y
neg_vec.z = -self.z
return neg_vec
def __setitem__(self, index, value):
"""Set an internal value."""
if index > 2:
raise IndexError("index must be between 0 and 2 inclusive.")
try:
if index == 0:
self.x = value
elif index == 1:
self.y = value
else:
self.z = value
except (TypeError):
raise TypeError("value must be a numerical type.")
def __getitem__(self, index):
"""Get an internal value."""
if index > 2:
raise IndexError("index must be between 0 and 2 inclusive.")
if index == 0:
return self.x
elif index == 1:
return self.y
else:
return self.z
#Define our methods
def copy(self):
"""Create a copy of the Vector"""
cpy_vec = Vector3()
cpy_vec.x = self.x
cpy_vec.y = self.y
cpy_vec.z = self.z
return cpy_vec
def to_vec4(self, isPoint):
"""Converts this vector3 into a vector4 instance."""
vec4 = Vector4()
vec4.x = self.x
vec4.y = self.y
vec4.z = self.z
if isPoint:
vec4.w = 1
else:
vec4.w = 0
return vec4
def length(self):
"""Gets the length of the Vector"""
return math.sqrt((self.x * self.x) + (self.y * self.y) + (self.z * self.z))
def normalize(self):
"""Normalizes this Vector"""
vlength = self.length()
#Make sure the length isn't 0
if vlength > 0:
self.x /= vlength
self.y /= vlength
self.z /= vlength
else:
return Vector3(0, 0, 0)
def normalize_copy(self):
"""Creates and returns a new Vector3 that is a normalized version of this Vector"""
newVec = self.copy()
newVec.normalize()
return newVec
def clamp(self, clampVal):
"""Clamps all the components in the vector to the specified clampVal."""
if self.x > clampVal:
self.x = clampVal
if self.y > clampVal:
self.y = clampVal
if self.z > clampVal:
self.z = clampVal
#Define our static methods
def up():
"""Return a Vector that points in the up direction."""
return Vector3(0, 1, 0)
up = staticmethod(up)
def tuple_as_vec(xyz):
"""
Generates a Vector3 from a tuple or list.
"""
vec = Vector3()
vec[0] = xyz[0]
vec[1] = xyz[1]
vec[2] = xyz[2]
return vec
tuple_as_vec = staticmethod(tuple_as_vec)
def cross(vec1, vec2):
"""Returns the cross product of two Vectors"""
if isinstance(vec1, Vector3) and isinstance(vec2, Vector3):
vec3 = Vector3()
vec3.x = (vec1.y * vec2.z) - (vec1.z * vec2.y)
vec3.y = (vec1.z * vec2.x) - (vec1.x * vec2.z)
vec3.z = (vec1.x * vec2.y) - (vec1.y * vec2.x)
return vec3
else:
raise TypeError("vec1 and vec2 must be Vector3's")
cross = staticmethod(cross)
class Vector4(object):
"""Provides basic 3D Vector operations"""
def __init__(self, x = 0.0, y = 0.0, z = 0.0, w = 0.0):
"""Constructs a Vector4 object with the default position of the object."""
#Check for potential problems while converting the values.
try:
self.x = float(x)
self.y = float(y)
self.z = float(z)
self.w = float(w)
except (TypeError):
raise TypeError("x, y, z, and w must be a numerical type.")
def __len__(self):
"""Returns the number of attributes contained in this class."""
return 4
def __str__(self):
"""Builds a string representation of this object."""
return "<Vector4>: { " + str(self.x) + ", " + str(self.y) + ", " + str(self.z) + ", " + str(self.w) + " }"
#Provide our operator overloaded methods
def __eq__(self, other):
"""Check to see if two Vector4 instances are equal"""
if not isinstance(other, Vector4):
return False
if self.x == other.x \
and self.y == other.y \
and self.z == other.z \
and self.w == other.w:
return True
else:
return False
def __ne__(self, other):
"""Check to see if two Vector4 instances are not equal"""
if not isinstance(other, Vector4):
raise TypeError("other must be of type Vector4")
if not(self.x == other.x) \
or not(self.y == other.y) \
or not(self.z == other.z) \
or not(self.w == other.w):
#True, the objects are not equal to each other.
return True
else:
#False, the objects are equal to each other
return False
def __add__(self, other):
"""Adds two Vector4 objects."""
if isinstance(other, Vector4):
vec4 = Vector4()
vec4.x = self.x + other.x
vec4.y = self.y + other.y
vec4.z = self.z + other.z
vec4.w = self.w + other.w
return vec4
else:
#The object isn't of a correct type, return self to prevent errors.
raise TypeError("other must be of type Vector3")
def __radd__(self, other):
return self.__add__(other)
def __sub__(self, other):
"""Subtract two Vector4 objects."""
if isinstance(other, Vector4):
vec4 = Vector4()
vec4.x = self.x - other.x
vec4.y = self.y - other.y
vec4.z = self.z - other.z
vec4.w = self.w - other.w
return vec4
else:
raise TypeError("other must be of type Vector4")
def __rsub__(self, other):
return self.__sub__(other)
def __mul__(self, other):
"""Multiply a Vector4 and a scalar."""
if isinstance(other, numbers.Number):
vec4 = Vector4()
vec4.x = self.x * other
vec4.y = self.y * other
vec4.z = self.z * other
vec4.w = self.w * other
return vec4
else:
raise TypeError("other must be a single number")
def __rmul__(self, other):
return self.__mul__(other)
def __div__(self, other):
"""Divide a Vector4 and a scalar."""
if operator.isNumberType(other):
vec4 = Vector4()
vec4.x = self.x / other
vec4.y = self.y / other
vec4.z = self.z / other
vec4.w = self.w / other
return vec4
else:
raise TypeError("other must be a single number")
def __rdiv__(self, other):
return self.__div__(other)
def __neg__(self):
"""Negate the Vector"""
neg_vec = Vector4()
neg_vec.x = -self.x
neg_vec.y = -self.y
neg_vec.z = -self.z
neg_vec.w = -self.w
return neg_vec
def __setitem__(self, index, value):
"""Set an internal value."""
if index > 3:
raise IndexError("index must be between 0 and 3 inclusive.")
try:
if index == 0:
self.x = value
elif index == 1:
self.y = value
elif index == 2:
self.z = value
else:
self.w = value
except (TypeError):
raise TypeError("value must be a numerical type.")
def __getitem__(self, index):
"""Get an internal value."""
if index > 3:
raise IndexError("index must be between 0 and 3 inclusive.")
if index == 0:
return self.x
elif index == 1:
return self.y
elif index == 2:
return self.z
else:
return self.w
#Define our methods
def copy(self):
"""Create a copy of the Vector"""
cpy_vec = Vector4()
cpy_vec.x = self.x
cpy_vec.y = self.y
cpy_vec.z = self.z
cpy_vec.w = self.w
return cpy_vec
def to_vec3(self):
"""Convert this vector4 instance into a vector3 instance."""
vec3 = Vector3()
vec3.x = self.x
vec3.y = self.y
vec3.z = self.z
if self.w != 0:
vec3 /= self.w
return vec3
def length(self):
"""Gets the length of the Vector"""
return math.sqrt((self.x * self.x) + (self.y * self.y) + (self.z * self.z) + (self.w * self.w))
def normalize(self):
"""Normalizes this Vector"""
vlength = self.length()
#Make sure the length isn't 0
if vlength > 0:
self.x /= vlength
self.y /= vlength
self.z /= vlength
self.w /= vlength
else:
return Vector4(0, 0, 0, 0)
def normalize_copy(self):
"""Creates and returns a new Vector3 that is a normalized version of this Vector"""
newVec = self.copy()
newVec.normalize()
return newVec
def clamp(self, clampVal):
"""Clamps all the components in the vector to the specified clampVal."""
if self.x > clampVal:
self.x = clampVal
if self.y > clampVal:
self.y = clampVal
if self.z > clampVal:
self.z = clampVal
if self.w > clampVal:
self.w = clampVal
#Define our static methods
def up():
"""Return a Vector that points in the up direction."""
return Vector4(0, 1, 0, 0)
up = staticmethod(up)
def tuple_as_vec(xyzw):
"""
Generates a Vector4 from a tuple or list.
"""
vec = Vector4()
vec[0] = xyzw[0]
vec[1] = xyzw[1]
vec[2] = xyzw[2]
vec[3] = xyzw[3]
return vec
tuple_as_vec = staticmethod(tuple_as_vec)
def dot(vec1, vec2):
"""Returns the dot product of two Vectors"""
if isinstance(vec1, Vector3) and isinstance(vec2, Vector3):
return (vec1.x * vec2.x) + (vec1.y * vec2.y) + (vec1.z * vec2.z)
elif isinstance(vec1, Vector4) and isinstance(vec2, Vector4):
return (vec1.x * vec2.x) + (vec1.y * vec2.y) + (vec1.z * vec2.z) + (vec1.w * vec2.w)
else:
raise TypeError("vec1 and vec2 must a Vector type")
def distance(vec1, vec2):
"""Returns the distance between two Vectors"""
vec3 = vec2 - vec1
return vec3.length()
def angle(vec1, vec2):
"""Returns the angle between two vectors"""
dot_vec = dot(vec1, vec2)
mag1 = vec1.length()
mag2 = vec2.length()
result = dot_vec / (mag1 * mag2)
return math.acos(result)
def project(vec1, vec2):
"""Project vector1 onto vector2."""
if isinstance(vec1, Vector3) and isinstance(vec2, Vector3) \
or isinstance(vec1, Vector4) and isinstance(vec2, Vector4):
return dot(vec1, vec2) / vec2.length() * vec2.normalize_copy()
else:
raise ValueError("vec1 and vec2 must be Vector3 or Vector4 objects.")
def component_add(vec1, vec2):
"""Add each of the components of vec1 and vec2 together and return a new vector."""
if isinstance(vec1, Vector3) and isinstance(vec2, Vector3):
addVec = Vector3()
addVec.x = vec1.x + vec2.x
addVec.y = vec1.y + vec2.y
addVec.z = vec1.z + vec2.z
return addVec
if isinstance(vec1, Vector4) and isinstance(vec2, Vector4):
addVec = Vector4()
addVec.x = vec1.x + vec2.x
addVec.y = vec1.y + vec2.y
addVec.z = vec1.z + vec2.z
addVec.w = vec1.w + vec2.w
return addVec
def reflect(vec1, vec2):
"""Take vec1 and reflect it about vec2."""
if isinstance(vec1, Vector3) and isinstance(vec2, Vector3) \
or isinstance(vec1, Vector4) and isinstance(vec2, Vector4):
return 2 * dot(vec1, vec2) * vec2 - vec2
else:
raise ValueError("vec1 and vec2 must both be a Vector type")
def sign(val):
"""
Returns the sign of a number.
"""
if val > 0:
return 1
elif val < 0:
return -1
return 0
class Ray(object):
def __init__(self, origin = Vector3(0,0,0), direction = Vector3(0,0,0)):
self.origin = origin
self.direction = direction
#Normalize our Vector
self.direction.normalize()
def get_point(self, scalar_val):
"""Get a point along the ray."""
return scalar_val * self.direction + self.origin
class Matrix4(object):
def __init__(self, *args):
"""
*args defines a function that can accept a variable number of parameters.
This allows us to be a bit more flexible in how we can create a Matrix,
as the user can define all four rows, or only one or two rows upon init.
"""
#A list of four Vector4's
self.dta = []
#Find the number of arguments the user passed to us.
argLen = len(args)
#Make sure we don't accept more than 4 lists.
if argLen > 4:
raise ValueError("*args should not contain more than four lists.")
#Create an identity matrix if the user isn't filling all the rows with data.
if argLen != 4:
self.dta = Matrix4.identity().dta
#Take each arg and append it to our dta list.
for index, arg in enumerate(args):
if isinstance(arg, Vector4) \
or isinstance(arg, tuple) \
or isinstance(arg, list):
if len(arg) == 4:
self.dta.append(Vector4(arg[0], arg[1], arg[2], arg[3]))
else:
raise ValueError("Each argument must contain four values or be a Vector4.")
else:
raise ValueError("Each argument must contain four values or be a Vector4.")
def __getitem__(self, index):
"""Get a row from the matrix."""
return self.get_row(index)
def get_row(self, row):
if row > -1 and row < 4:
return self.dta[row].copy()
def set_row(self, row, vec4):
if row > -1 and row < 4:
self.dta[row] = vec4.copy()
def get_col(self, col):
if col > -1 and col < 4:
return Vector4(self.dta[0][col],
self.dta[1][col],
self.dta[2][col],
self.dta[3][col])
def set_col(self, col, vec4):
if col > -1 and col < 4:
self.dta[col][0] = vec4.x,
self.dta[col][1] = vec4.y,
self.dta[col][2] = vec4.z,
self.dta[col][3] = vec4.w
def __mul__(self, other):
if isinstance(other, Matrix4):
return Matrix4 ( (dot(self.get_row(0), other.get_col(0)), dot(self.get_row(0), other.get_col(1)), dot(self.get_row(0), other.get_col(2)), dot(self.get_row(0), other.get_col(3))),
(dot(self.get_row(1), other.get_col(0)), dot(self.get_row(1), other.get_col(1)), dot(self.get_row(1), other.get_col(2)), dot(self.get_row(1), other.get_col(3))),
(dot(self.get_row(2), other.get_col(0)), dot(self.get_row(2), other.get_col(1)), dot(self.get_row(2), other.get_col(2)), dot(self.get_row(2), other.get_col(3))),
(dot(self.get_row(3), other.get_col(0)), dot(self.get_row(3), other.get_col(1)), dot(self.get_row(3), other.get_col(2)), dot(self.get_row(3), other.get_col(3))) )
if isinstance(other, Vector4):
vec = Vector4( dot(self.get_row(0), other),
dot(self.get_row(1), other),
dot(self.get_row(2), other),
dot(self.get_row(3), other) )
return vec
if isinstance(other, numbers.Number):
return Matrix4( self.dta[0] * other,
self.dta[1] * other,
self.dta[2] * other,
self.dta[3] * other )
raise TypeError("other must be of type Matrix4, Vector4, or number")
def __rmul__(self, other):
return self.__mul__(other)
def __str__(self):
string = ""
for vec in self.dta:
string += "[ " + str(vec.x) + " " + str(vec.y) + " " + str(vec.z) + " " + str(vec.w) + " ]\n"
return string
def transpose(self):
"""Create a transpose of this matrix."""
ma4 = Matrix4( self.get_col(0),
self.get_col(1),
self.get_col(2),
self.get_col(3) )
return ma4
def is_identity():
"""Check to see if this matrix is an identity matrix."""
for index, row in enumerate(self.dta):
if row[index] == 1:
for num, element in enumerate(row):
if num != index:
if element != 0:
return False
else:
return False
return True
def is_orthogonal():
"""Check to see if this matrix is orthogonal."""
return (self * self.transpose()).is_identity()
def inverse_as_orghogonal():
"""Get the inverse of this matrix, assuming this matrix is orthogonal."""
return self.transpose()
def identity():
"""Create and return an identity matrix."""
ma4 = Matrix4( (1, 0, 0, 0),
(0, 1, 0, 0),
(0, 0, 1, 0),
(0, 0, 0, 1) )
return ma4
def translate(translationAmt):
"""Create a translation matrix."""
if not isinstance(translationAmt, Vector3):
raise ValueError("translationAmt must be a Vector3")
ma4 = Matrix4( (1, 0, 0, translationAmt.x),
(0, 1, 0, translationAmt.y),
(0, 0, 1, translationAmt.z),
(0, 0, 0, 1) )
return ma4
def scale(scaleAmt):
"""
Create a scale matrix.
scaleAmt is a Vector3 defining the x, y, and z scale values.
"""
if not isinstance(scaleAmt, Vector3):
raise ValueError("scaleAmt must be a Vector3")
ma4 = Matrix4( (scaleAmt.x, 0, 0, 0),
(0, scaleAmt.y, 0, 0),
(0, 0, scaleAmt.z, 0),
(0, 0, 0, 1) )
return ma4
def z_rotate(rotationAmt):
"""Create a matrix that rotates around the z axis."""
ma4 = Matrix4( (math.cos(rotationAmt), -math.sin(rotationAmt), 0, 0),
(math.sin(rotationAmt), math.cos(rotationAmt), 0, 0),
(0, 0, 1, 0),
(0, 0, 0, 1) )
return ma4
def y_rotate(rotationAmt):
"""Create a matrix that rotates around the y axis."""
ma4 = Matrix4( (math.cos(rotationAmt), 0, math.sin(rotationAmt), 0),
(0, 1, 0, 0),
(-math.sin(rotationAmt), 0, math.cos(rotationAmt), 0),
(0, 0, 0, 1) )
return ma4
def x_rotate(rotationAmt):
"""Create a matrix that rotates around the x axis."""
ma4 = Matrix4( (1, 0, 0, 0),
(0, math.cos(rotationAmt), -math.sin(rotationAmt), 0),
(0, math.sin(rotationAmt), math.cos(rotationAmt), 0),
(0, 0, 0, 1) )
return ma4
def build_rotation(vec3):
"""
Build a rotation matrix.
vec3 is a Vector3 defining the axis about which to rotate the object.
"""
if not isinstance(vec3, Vector3):
raise ValueError("rotAmt must be a Vector3")
return Matrix4.x_rotate(vec3.x) * Matrix4.y_rotate(vec3.y) * Matrix4.z_rotate(vec3.z)
identity = staticmethod(identity)
translate = staticmethod(translate)
scale = staticmethod(scale)
z_rotate = staticmethod(z_rotate)
y_rotate = staticmethod(y_rotate)
x_rotate = staticmethod(x_rotate)
build_rotation = staticmethod(build_rotation)
class Ragnarok(object):
world = None
def __init__(self, preferred_backbuffer_size = (-1, -1), window_name = "Ragnarok Game"):
global world
"""
Prepares the Ragnarok library for use.
preferred_back_size attempts to set the resolution of the window to the specified size. (-1, -1) attempts native resolution.
window_name is the text that displays on the top-left of the pygame window.
"""
print 'Booting Ragnarok\n'
pygame.init()
self.__is_running = True
#The number of times our engine should update per second.
self.preferred_fps = 60
#Create a clock to manage our update frequency
self.__clock = pygame.time.Clock()
#Init our world
world = World()
if preferred_backbuffer_size == Vector2(-1, -1):
world.set_display_at_native_res()
else:
world.set_backbuffer(preferred_backbuffer_size)
pygame.display.set_caption(window_name)
#Used to print the fps of the game onto the console window.
self.fpsTimer = 0.0
#Should Ragnarok print the number of frames the game is running at?
self.print_frames = False
#The number of milliseconds between printing out the frame rate
self.print_fps_frequency = 1000
#Used to tell Ragnarok how to handle certain events.
#Once set, it must be explicitely unset
#Flag 0 means to skip updating
#Flag 1 means to skip drawing
#Flag 2 means to discard timing values