PageRenderTime 24ms CodeModel.GetById 16ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/IronPython_Main/Languages/IronPython/Samples/Direct3D/demo1.py

#
Python | 69 lines | 39 code | 16 blank | 14 comment | 2 complexity | e1eea7ad2af2e2a8cc79d8eaf3134d88 MD5 | raw file
 1#####################################################################################
 2#
 3#  Copyright (c) Microsoft Corporation. All rights reserved.
 4#
 5# This source code is subject to terms and conditions of the Apache License, Version 2.0. A 
 6# copy of the license can be found in the License.html file at the root of this distribution. If 
 7# you cannot locate the  Apache License, Version 2.0, please send an email to 
 8# ironpy@microsoft.com. By using this source code in any fashion, you are agreeing to be bound 
 9# by the terms of the Apache License, Version 2.0.
10#
11# You must not remove this notice, or any other, from this software.
12#
13#
14#####################################################################################
15
16import clr
17from framework import *
18
19class DirectionalMover(object):
20    def __init__(self, obj, direction, speed, distance):
21        self.Object = obj
22        self.Speed = speed
23        self.Distance = distance
24        self.Direction = Vectorize(direction)
25        self.Direction.Normalize()
26
27    def OnFrame(self, elapsed):
28        toTravel = self.Speed * elapsed
29
30        if self.Distance < toTravel:
31            self.Object.Translate(self.Direction * single(self.Distance))
32            return True
33        else:
34            self.Distance -= toTravel
35            self.Object.Translate(self.Direction * single(toTravel))
36
37
38class DemoSceneCreator(object):
39    def OnSceneCreate(self, sm):
40        cam = sm.CreateCamera("Player Cam")
41        cam.Position = [0, 3, -5]
42        
43        box = sm.LoadBasicObject("box", "box 1", Drawing.Color.Red, 0.25, 0.25, 0.25)
44        box.Position = [-1, 0, 0]
45        
46        box = sm.LoadBasicObject("box", "box 2", Drawing.Color.Green, 0.25, 0.25, 0.25)
47        box.Position = [0, 0, 0]
48        cam.LookAt(box.Position)
49        
50        box = sm.LoadBasicObject("box", "box 3", Drawing.Color.Blue, 0.25, 0.25, 0.25)
51        box.Position = [1, 0, 0]
52
53        return True
54
55    def OnSceneBegin(self, sm):
56        dm = DirectionalMover(sm.Objects["box 1"], [1, 0, 0], 0.3, 3)
57        sm.AddListener(dm)
58        
59        dm = DirectionalMover(sm.Objects["box 2"], [0, 1, 0], 0.2, 2)
60        sm.AddListener(dm)
61        
62        dm = DirectionalMover(sm.Objects["box 3"], [0, 0, 1], 1, 10)
63        sm.AddListener(dm)
64
65        return True
66
67
68if __name__ == '__main__':
69    Root().Main([DemoSceneCreator])