Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

added python aop sample

  • Loading branch information...
commit de713e410d470eb8d3b8c1c4fa111ee629059bf8 1 parent f5759c4
Stanislav authored May 17, 2012
111  java/jme_game/src/mygame/Main.java
... ...
@@ -1,118 +1,11 @@
1 1
 package mygame;
2 2
 
3  
-import com.jme3.app.SimpleApplication;
4  
-import com.jme3.input.KeyInput;
5  
-import com.jme3.input.controls.ActionListener;
6  
-import com.jme3.input.controls.AnalogListener;
7  
-import com.jme3.input.controls.KeyTrigger;
8  
-import com.jme3.light.DirectionalLight;
9  
-import com.jme3.material.Material;
10  
-import com.jme3.math.ColorRGBA;
11  
-import com.jme3.math.Vector3f;
12  
-import com.jme3.renderer.RenderManager;
13  
-import com.jme3.scene.Geometry;
14  
-import com.jme3.scene.Node;
15  
-import com.jme3.scene.Spatial;
16  
-import com.jme3.scene.shape.Box;
17  
-
18  
-public class Main extends SimpleApplication 
  3
+public class Main 
19 4
 {
20 5
     public static void main(String[] args) 
21 6
     {
22  
-        Main app = new Main();
  7
+        Shooter app = new Shooter();
23 8
         app.setShowSettings(false);
24 9
         app.start();
25 10
     }
26  
-
27  
-    @Override
28  
-    public void simpleInitApp() 
29  
-    {
30  
-        int n = 10000;
31  
-        for(int i = 0; i < n; ++i)
32  
-        {
33  
-            Box box1 = new Box(new Vector3f(n/2+i,1,1), 1,1,1);
34  
-            Geometry geom1 = new Geometry("Box", box1);
35  
-            Material mat1 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
36  
-            mat1.setColor("Color", ColorRGBA.White);
37  
-            //mat1.setTexture("ColorMap", assetManager.loadTexture("textures/monkey.jpg"));
38  
-            geom1.setMaterial(mat1);
39  
-            geom1.setLocalScale(0.05f);
40  
-            rootNode.attachChild(geom1);
41  
-        }
42  
-        /*Box box2 = new Box(new Vector3f(1,3,1), 1,1,1);
43  
-        Geometry geom2 = new Geometry("Box", box2);
44  
-        Material mat2 = new Material(assetManager, "Common/MatDefs/Misc/ShowNormals.j3md");
45  
-        //mat2.setColor("Color", ColorRGBA.Red);
46  
-        geom2.setMaterial(mat2);
47  
-        Node pivot = new Node("pivot");
48  
-        rootNode.attachChild(pivot);
49  
-        pivot.attachChild(geom1);
50  
-        pivot.attachChild(geom2);
51  
-        pivot.rotate(.4f,.4f,.0f);*/
52  
-        /*teapot = assetManager.loadModel("models/teapot.obj");
53  
-        //teapot.move(1, 1, 1);
54  
-        Material mat3 = new Material(assetManager, "Common/MatDefs/Misc/ShowNormals.j3md");
55  
-        teapot.setMaterial(mat3);
56  
-        rootNode.attachChild(teapot);
57  
-        Spatial wall = new Geometry("Box", new Box(Vector3f.ZERO, 2.5f, 2.5f, 1.0f));
58  
-        Material wall_material = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
59  
-        wall_material.setTexture("ColorMap", assetManager.loadTexture("textures/brickwall.jpg"));
60  
-        wall.setMaterial(wall_material);
61  
-        wall.setLocalTranslation(2.0f,-2.5f,0.0f);
62  
-        rootNode.attachChild(wall);
63  
-        viewPort.setBackgroundColor(ColorRGBA.LightGray);
64  
-        initKeys();*/
65  
-    }
66  
-
67  
-    @Override
68  
-    public void simpleUpdate(float tpf) 
69  
-    {
70  
-        //teapot.rotate(0, 2*tpf, 0);
71  
-    }
72  
-
73  
-    @Override
74  
-    public void simpleRender(RenderManager rm) {
75  
-        //TODO: add render code
76  
-    }
77  
-    
78  
-    private void initKeys()
79  
-    {
80  
-        inputManager.addMapping("Pause", new KeyTrigger(KeyInput.KEY_P));
81  
-        inputManager.addMapping("Left", new KeyTrigger(KeyInput.KEY_L));
82  
-        inputManager.addMapping("Right", new KeyTrigger(KeyInput.KEY_R));
83  
-        inputManager.addListener(actionListener, new String[]{"Pause"});
84  
-        inputManager.addListener(analogListener, new String[]{"Left","Right"});
85  
-    }
86  
-    
87  
-    private ActionListener actionListener = new ActionListener() 
88  
-    {
89  
-        public void onAction(String name, boolean isPressed, float tpf) 
90  
-        {
91  
-            if(name.equals("Pause") && !isPressed)
92  
-                isRunning = !isRunning;
93  
-        }
94  
-    };
95  
-    
96  
-    private AnalogListener analogListener = new AnalogListener() 
97  
-    {
98  
-        public void onAnalog(String name, float value, float tpf) 
99  
-        {
100  
-            if(isRunning)
101  
-            {
102  
-                if(name.equals("Left"))
103  
-                {
104  
-                    Vector3f v = teapot.getLocalTranslation();
105  
-                    teapot.setLocalTranslation(v.x - value*speed, v.y, v.z);
106  
-                }
107  
-                else if(name.equals("Right"))
108  
-                {
109  
-                    Vector3f v = teapot.getLocalTranslation();
110  
-                    teapot.setLocalTranslation(v.x + value*speed, v.y, v.z);
111  
-                }
112  
-            }
113  
-        }
114  
-    };
115  
-    
116  
-    protected Spatial teapot;
117  
-    protected boolean isRunning = true;
118 11
 }
29  java/jme_game/src/mygame/Shooter.java
@@ -10,6 +10,8 @@
10 10
 import com.jme3.input.controls.ActionListener;
11 11
 import com.jme3.input.controls.MouseButtonTrigger;
12 12
 import com.jme3.material.Material;
  13
+import com.jme3.math.FastMath;
  14
+import com.jme3.math.Quaternion;
13 15
 import com.jme3.math.Vector2f;
14 16
 import com.jme3.math.Vector3f;
15 17
 import com.jme3.scene.Geometry;
@@ -74,8 +76,11 @@ private void initPhysics()
74 76
     
75 77
     private void initCamera()
76 78
     {
77  
-        cam.setLocation(new Vector3f(0, 4f, 6f));
78  
-        cam.lookAt(new Vector3f(2,2,0), Vector3f.UNIT_Y);
  79
+        cam.setLocation(new Vector3f(0, 1f, 6f));
  80
+        //cam.lookAt(new Vector3f(0,0,0), new Vector3f(0,0,0));
  81
+        Quaternion rotator = new Quaternion();
  82
+        rotator.fromAngleAxis(FastMath.QUARTER_PI, Vector3f.UNIT_Y);
  83
+        //cam.setRotation(rotator);
79 84
     }
80 85
 
81 86
     private void initCrossHairs() 
@@ -111,11 +116,24 @@ private void initWall()
111 116
             for(int j = 0; j < 5; ++j)
112 117
             {
113 118
                 Vector3f loc = new Vector3f(j, i*2*brickHeight, 0);
114  
-                createBrick(loc, brickLength, brickWidth, brickHeight);
  119
+                createBrick(loc, brickLength, brickWidth, brickHeight, 2);
115 120
             }
  121
+        
  122
+        Vector3f loc2 = new Vector3f(0, 1, 2);
  123
+        Box brickBox = new Box(Vector3f.ZERO, brickLength, brickWidth, brickHeight);
  124
+        brickBox.scaleTextureCoordinates(new Vector2f(1f, .5f));
  125
+        Geometry brickGeo = new Geometry("brick", brickBox);
  126
+        brickGeo.setMaterial((Material)resources.get("materials.brick"));
  127
+        brickGeo.setLocalTranslation(loc2);
  128
+        rootNode.attachChild(brickGeo);
  129
+        Quaternion quat1 = new Quaternion();
  130
+        quat1.fromAngleAxis(-FastMath.QUARTER_PI, Vector3f.UNIT_Z);
  131
+        Quaternion quat2 = new Quaternion();
  132
+        quat2.fromAngleAxis(FastMath.QUARTER_PI, Vector3f.UNIT_Y);
  133
+        brickGeo.rotate(quat1.mult(quat2));
116 134
     }
117 135
     
118  
-    private void createBrick(Vector3f loc, float length, float width, float height)
  136
+    private Geometry createBrick(Vector3f loc, float length, float width, float height, float mass)
119 137
     {
120 138
         Box brickBox = new Box(Vector3f.ZERO, length, height, width);
121 139
         brickBox.scaleTextureCoordinates(new Vector2f(1f, .5f));
@@ -123,9 +141,10 @@ private void createBrick(Vector3f loc, float length, float width, float height)
123 141
         brickGeo.setMaterial((Material)resources.get("materials.brick"));
124 142
         brickGeo.setLocalTranslation(loc);
125 143
         rootNode.attachChild(brickGeo);
126  
-        RigidBodyControl brickPhysics = new RigidBodyControl(2f);
  144
+        RigidBodyControl brickPhysics = new RigidBodyControl(mass);
127 145
         brickGeo.addControl(brickPhysics);
128 146
         bulletAppState.getPhysicsSpace().add(brickPhysics);
  147
+        return brickGeo;
129 148
     }
130 149
     
131 150
     private void initKeys()
111  java/jme_game/src/mygame/Simple.java
... ...
@@ -0,0 +1,111 @@
  1
+package mygame;
  2
+
  3
+import com.jme3.app.SimpleApplication;
  4
+import com.jme3.input.KeyInput;
  5
+import com.jme3.input.controls.ActionListener;
  6
+import com.jme3.input.controls.AnalogListener;
  7
+import com.jme3.input.controls.KeyTrigger;
  8
+import com.jme3.light.DirectionalLight;
  9
+import com.jme3.material.Material;
  10
+import com.jme3.math.ColorRGBA;
  11
+import com.jme3.math.Vector3f;
  12
+import com.jme3.renderer.RenderManager;
  13
+import com.jme3.scene.Geometry;
  14
+import com.jme3.scene.Node;
  15
+import com.jme3.scene.Spatial;
  16
+import com.jme3.scene.shape.Box;
  17
+
  18
+public class Simple extends SimpleApplication 
  19
+{
  20
+    @Override
  21
+    public void simpleInitApp() 
  22
+    {
  23
+        int n = 10000;
  24
+        for(int i = 0; i < n; ++i)
  25
+        {
  26
+            Box box1 = new Box(new Vector3f(n/2+i,1,1), 1,1,1);
  27
+            Geometry geom1 = new Geometry("Box", box1);
  28
+            Material mat1 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
  29
+            mat1.setColor("Color", ColorRGBA.White);
  30
+            //mat1.setTexture("ColorMap", assetManager.loadTexture("textures/monkey.jpg"));
  31
+            geom1.setMaterial(mat1);
  32
+            geom1.setLocalScale(0.05f);
  33
+            rootNode.attachChild(geom1);
  34
+        }
  35
+        /*Box box2 = new Box(new Vector3f(1,3,1), 1,1,1);
  36
+        Geometry geom2 = new Geometry("Box", box2);
  37
+        Material mat2 = new Material(assetManager, "Common/MatDefs/Misc/ShowNormals.j3md");
  38
+        //mat2.setColor("Color", ColorRGBA.Red);
  39
+        geom2.setMaterial(mat2);
  40
+        Node pivot = new Node("pivot");
  41
+        rootNode.attachChild(pivot);
  42
+        pivot.attachChild(geom1);
  43
+        pivot.attachChild(geom2);
  44
+        pivot.rotate(.4f,.4f,.0f);*/
  45
+        /*teapot = assetManager.loadModel("models/teapot.obj");
  46
+        //teapot.move(1, 1, 1);
  47
+        Material mat3 = new Material(assetManager, "Common/MatDefs/Misc/ShowNormals.j3md");
  48
+        teapot.setMaterial(mat3);
  49
+        rootNode.attachChild(teapot);
  50
+        Spatial wall = new Geometry("Box", new Box(Vector3f.ZERO, 2.5f, 2.5f, 1.0f));
  51
+        Material wall_material = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
  52
+        wall_material.setTexture("ColorMap", assetManager.loadTexture("textures/brickwall.jpg"));
  53
+        wall.setMaterial(wall_material);
  54
+        wall.setLocalTranslation(2.0f,-2.5f,0.0f);
  55
+        rootNode.attachChild(wall);
  56
+        viewPort.setBackgroundColor(ColorRGBA.LightGray);
  57
+        initKeys();*/
  58
+    }
  59
+
  60
+    @Override
  61
+    public void simpleUpdate(float tpf) 
  62
+    {
  63
+        //teapot.rotate(0, 2*tpf, 0);
  64
+    }
  65
+
  66
+    @Override
  67
+    public void simpleRender(RenderManager rm) {
  68
+        //TODO: add render code
  69
+    }
  70
+    
  71
+    private void initKeys()
  72
+    {
  73
+        inputManager.addMapping("Pause", new KeyTrigger(KeyInput.KEY_P));
  74
+        inputManager.addMapping("Left", new KeyTrigger(KeyInput.KEY_L));
  75
+        inputManager.addMapping("Right", new KeyTrigger(KeyInput.KEY_R));
  76
+        inputManager.addListener(actionListener, new String[]{"Pause"});
  77
+        inputManager.addListener(analogListener, new String[]{"Left","Right"});
  78
+    }
  79
+    
  80
+    private ActionListener actionListener = new ActionListener() 
  81
+    {
  82
+        public void onAction(String name, boolean isPressed, float tpf) 
  83
+        {
  84
+            if(name.equals("Pause") && !isPressed)
  85
+                isRunning = !isRunning;
  86
+        }
  87
+    };
  88
+    
  89
+    private AnalogListener analogListener = new AnalogListener() 
  90
+    {
  91
+        public void onAnalog(String name, float value, float tpf) 
  92
+        {
  93
+            if(isRunning)
  94
+            {
  95
+                if(name.equals("Left"))
  96
+                {
  97
+                    Vector3f v = teapot.getLocalTranslation();
  98
+                    teapot.setLocalTranslation(v.x - value*speed, v.y, v.z);
  99
+                }
  100
+                else if(name.equals("Right"))
  101
+                {
  102
+                    Vector3f v = teapot.getLocalTranslation();
  103
+                    teapot.setLocalTranslation(v.x + value*speed, v.y, v.z);
  104
+                }
  105
+            }
  106
+        }
  107
+    };
  108
+    
  109
+    protected Spatial teapot;
  110
+    protected boolean isRunning = true;
  111
+}
38  python/aop_sample.py
... ...
@@ -0,0 +1,38 @@
  1
+from sys import path
  2
+path.append("/home/stanislavfeldman/projects/python/putils/")
  3
+from putils.aop import Aspect
  4
+		
  5
+		
  6
+class MyAspect(Aspect):
  7
+	def on_enter(self, call):
  8
+		print "on enter: ", call.args
  9
+		
  10
+	def on_success(self, call):
  11
+		print "on success: ", call.result
  12
+		
  13
+	def on_fail(self, call):
  14
+		print "on fail: ", call.exception
  15
+
  16
+	
  17
+@MyAspect()
  18
+class MyClass(object):
  19
+	def __init__(self):
  20
+		self.var = 5
  21
+	def m1(self, n):
  22
+		n+1
  23
+	def m2(self, n):
  24
+		return n+1
  25
+	def m3(self, n):
  26
+		raise ValueError("error!")
  27
+
  28
+		
  29
+@MyAspect()
  30
+def func(x):
  31
+	return x+1
  32
+
  33
+		
  34
+mc = MyClass()
  35
+mc.m1(5)
  36
+mc.m2(6)
  37
+mc.m3(7)
  38
+func(555)
19  python/crypto.py
... ...
@@ -1,10 +1,23 @@
1 1
 from Crypto.Cipher import AES
2  
-
3 2
 key = "my super secret!"
4 3
 plaintext = "hi vasya z osaji"
5 4
 encoder = AES.new(key, AES.MODE_ECB)
6 5
 ciphertext = encoder.encrypt(plaintext)
7  
-print ciphertext.encode("hex")
8 6
 decoder = AES.new(key, AES.MODE_ECB)
9 7
 plaintext2 = decoder.decrypt(ciphertext)
10  
-print plaintext == plaintext2
  8
+print "symmetric crypto: ", plaintext == plaintext2
  9
+
  10
+from Crypto.Hash import SHA256
  11
+hash_function = SHA256.new()
  12
+hash_function.update(plaintext)
  13
+h1 = hash_function.hexdigest()
  14
+hash_function = SHA256.new()
  15
+hash_function.update("hi vasya z osaji")
  16
+h2 = hash_function.hexdigest()
  17
+print "hash: ", h1 == h2
  18
+
  19
+from Crypto.PublicKey import RSA
  20
+private_key = RSA.generate(1024)
  21
+public_key = private_key.publickey()
  22
+ciphertext = public_key.encrypt(plaintext, "random str")
  23
+print "assymetric crypto: ", plaintext == private_key.decrypt(ciphertext)

0 notes on commit de713e4

Please sign in to comment.
Something went wrong with that request. Please try again.