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

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