source: 2011/26/JaakkoL/Rogue Agent 2372/Rogue Agent 2372/Rogue Agent 2372/Player.cs @ 2363

Revision 2363, 2.8 KB checked in by teeevasa, 8 years ago (diff)
Line 
1using System;
2using Jypeli;
3using Jypeli.Assets;
4using Jypeli.Controls;
5using Jypeli.Effects;
6using Jypeli.Widgets;
7
8public class Player : GenCharacter
9{
10    const int speed = 200;
11    const int jumpSpeed = 200;
12    const int mass = 10;
13    Image portrait = Game.LoadImage("Images/character");
14    SoundEffect walking = Game.LoadSoundEffect("Sounds/walkingsound");
15
16    Pistol pistol;
17    Rifle rifle;
18
19    Timer walker;
20
21        public Player(double width, double height, Shape shape, int HP = 100)
22        : base(width, height, shape, HP)
23        {
24        hitPoints = 100;
25        AddedToGame += addControls;
26        this.Image = portrait;
27        this.CollisionIgnoreGroup = 1;
28        this.Mass = mass;
29
30        // Creating the timer system that is *supposed* to enable a nice walking sound effect
31        walker = new Timer();
32        walker.Interval = 0.7;
33        walker.Trigger += playWalkingSound;
34
35        // Creating the weapon; for now only a pistol
36        pistol = new Pistol(20.0, 10.0);
37        pistol.X = 18;
38        pistol.Y = 11;
39        //this.Weapon = pistol; // The default weapon
40        pistol.ProjectileCollision += bulletReachedTarget;
41
42        rifle = new Rifle(20.0, 10.0);
43        rifle.X = 18;
44        rifle.Y = 11;
45        this.Weapon = rifle;
46        rifle.ProjectileCollision += bulletReachedTarget;
47       
48        // Collision handlers for enemies
49        //AddCollisionHandler();
50        }
51
52    private void addControls()
53    {
54        Game.Keyboard.Listen(Key.Right, ButtonState.Down, moveRight, "Moves right", speed);
55        Game.Keyboard.Listen(Key.Left, ButtonState.Down, moveLeft, "Moves left", speed);
56        Game.Keyboard.Listen(Key.Up, ButtonState.Down, jump, "Jumps", jumpSpeed);
57        Game.Keyboard.Listen(Key.Space, ButtonState.Pressed, shoot, "Shoots");
58    }
59
60    // Because all the weapons have infinite ammo, we don't have to check for null
61    private void shoot()
62    {
63        PhysicsObject bullet = this.Weapon.Shoot();
64    }
65
66    private void bulletReachedTarget(PhysicsObject bullet, PhysicsObject target)
67    {
68        bullet.Destroy();
69
70        if (target.Tag == "MeleeAlien")
71        {
72            MeleeAlien alien = (MeleeAlien)target;
73            //alien.reduceHitPointsBy(this.Weapon.firePower());
74            alien.reduceHitPointsBy(10);
75        }
76    }
77
78    private void playWalkingSound(Timer sender)
79    {
80        this.walking.Play();
81    }
82
83    private void moveRight(int speed)
84    {
85        walker.Start();
86        this.Walk(speed);
87        //this.Image = Image.Mirror(portrait); // Doesn't work at all!
88    }
89
90    private void moveLeft(int speed)
91    {
92        walker.Start();
93        this.Walk(-speed);
94        this.Image = Image.Mirror(portrait);
95    }
96
97    public void jump(int jumpSpeed)
98    {
99        this.Jump(jumpSpeed);
100    }
101}
Note: See TracBrowser for help on using the repository browser.