source: 2010/27/sijoseha/BuildingBuilder/Peli.cs @ 1139

Revision 1139, 4.7 KB checked in by sijoseha, 13 years ago (diff)

Working somehow.
Playable. You can lose, but you can't win.

Line 
1using System;
2using System.Collections.Generic;
3using Jypeli;
4using Jypeli.Widgets;
5using Jypeli.Assets;
6
7public class Peli : PhysicsGame
8{
9    PhysicsObject newestBlock, hittedBlock, dropArrow, ground;
10    //Queue<PhysicsObject> tower;
11    Stack<PhysicsObject> tower;
12    enum DropArrowDirection
13    {
14        Left,
15        Right
16    }
17    DropArrowDirection dAD;
18    Image dropArrowImage;
19    Boolean drop, cameraMoving;
20    WandererBrain dropArrowBrain;
21    double startZoom;
22
23    protected override void Begin()
24    {
25        LoadContent();
26        CreateLevel();
27        Keyboard.Listen(Key.Space, ButtonState.Released, CreateBlock, "");
28        //CreateBlock(new Vector(0, 200));
29        //CreateBlock(new Vector(10, 350));
30        //CreateBlock(new Vector(-10, 500));
31    }
32    void LoadContent()
33    {
34        dropArrowImage = LoadImage("dropArrow");
35    }
36    void CreateLevel()
37    {
38        tower = new Stack<PhysicsObject>();
39        Level.Width = 3000;
40        ground = Level.CreateGround(Level.Bottom, 40, 20, Color.Green, 20);
41        //tower.Push(ground);
42        Gravity = new Vector(0, -400);
43        drop = true;
44        cameraMoving = false;
45        startZoom = Camera.Y - ground.Y;
46        Camera.Zoom(0.8);
47        CreateDropArrow();
48    }
49
50    void CreateDropArrow()
51    {
52        dropArrow = new PhysicsObject(48, 48);
53        dropArrow.Image = dropArrowImage;
54        dropArrow.IgnoresGravity = true;
55        dropArrow.Position = new Vector(Camera.X, Camera.Y + 380);
56        Add(dropArrow);
57
58        UpdateDropArrowBrain();
59    }
60
61    void UpdateDropArrowBrain()
62    {
63        dropArrow.Y = Camera.Y + 350;
64        dropArrowBrain = new WandererBrain();
65        Vector point1 = new Vector(Camera.X - 150, Camera.Y + 380);
66        Vector point2 = new Vector(Camera.X + 150, Camera.Y + 380);
67        dropArrowBrain.Add(point1, point2);
68        dropArrowBrain.Active = true;
69        dropArrowBrain.MovingSpeed = 100;
70        //dropArrow.Brain = dropArrowBrain;
71    }
72
73    void CreateBlock()
74    {
75        if (drop)
76        {
77            drop = false;
78            PhysicsObject block = new PhysicsObject(100, 100, Shapes.Rectangle);
79            block.Position = new Vector(dropArrow.X, Camera.Y + 200);
80            block.KineticFriction = 1.0;
81            block.Color = Color.Red;
82            block.AngularVelocity = 0;
83            block.AngularDamping = 0.3;
84            //block.CanRotate = false;
85            block.LinearDamping = 1.0;
86            block.Restitution = 0.2;
87            block.Tag = "block";
88            Add(block);
89            newestBlock = block;
90            AddCollisionHandler(block, BlockCollision);
91        }
92    }
93
94    void BlockCollision(PhysicsObject collider, PhysicsObject target)
95    {
96        if (collider == newestBlock)
97        {
98            drop = true;
99        }
100        if (collider.Y > Camera.Y)
101        {
102            //cameraMoving = true;
103            //UpdateDropArrowBrain();
104        }
105        if (collider == newestBlock && target == ground && tower.Count == 0)
106        {
107            tower.Push(collider);
108            tower.Peek().Tag = "base";
109        }
110        if (collider.Tag.ToString() == "block" && target == ground)
111        {
112            MessageBox loppu = new MessageBox("HÄVISIT PELIN!", "HÄVISIT");
113            Add(loppu);
114        }
115        if (collider == newestBlock && target == tower.Peek())
116        {
117            tower.Peek().Color = Color.White;
118            tower.Push(collider);
119            //hittedBlock = collider;
120            //hittedBlock.Color = Color.Green;
121            tower.Peek().Color = Color.Green;
122            cameraMoving = true;
123        }
124    }
125
126    protected override void Update(Time time)
127    {
128        if (tower.Count > 0 && tower.Peek().Y < Camera.Y - 400 && tower.Peek().Tag.ToString() == "block" && Camera.ZoomFactor > (startZoom/(Camera.Y-ground.Y)-0.05))
129        {
130            Camera.Zoom(0.9);
131        }
132        dropArrow.Y = Camera.Y + 350;
133        if (cameraMoving)
134        {
135            Camera.Y += 2;
136            if (tower.Peek().Y < Camera.Y)
137            {
138                cameraMoving = false;
139            }
140        }
141        if (tower.Count > 0 && Camera.Y < tower.Peek().Y)
142        {
143            cameraMoving = true;
144        }
145
146        if (dAD == DropArrowDirection.Left)
147        {
148            dropArrow.X -= 5;
149            if (dropArrow.X <= -150)
150                dAD = DropArrowDirection.Right;
151        }
152        else if (dAD == DropArrowDirection.Right)
153        {
154            dropArrow.X += 5;
155            if (dropArrow.X >= 150)
156                dAD = DropArrowDirection.Left;
157        }
158        base.Update(time);
159    }
160}
Note: See TracBrowser for help on using the repository browser.