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

Revision 2371, 3.5 KB checked in by teeevasa, 8 years ago (diff)

final commit of the day

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    string currentWeapon;
17    Pistol pistol;
18    Rifle rifle;
19
20    Timer walker;
21
22        public Player(double width, double height, Shape shape, int HP = 100)
23        : base(width, height, shape, HP)
24        {
25        hitPoints = 100;
26        AddedToGame += addControls;
27        this.Image = portrait;
28        this.CollisionIgnoreGroup = 1;
29        this.Mass = mass;
30
31        // Creating the timer system that is *supposed* to enable a nice walking sound effect
32        walker = new Timer();
33        walker.Interval = 0.7;
34        walker.Trigger += playWalkingSound;
35
36        // Creating the weapon; for now only a pistol
37        pistol = new Pistol(20.0, 10.0);
38        pistol.X = 18;
39        pistol.Y = 11;
40        pistol.ProjectileCollision += bulletReachedTarget;
41
42        rifle = new Rifle(59.0, 11.0);
43        rifle.X = 14;
44        rifle.Y = 8;
45        rifle.ProjectileCollision += bulletReachedTarget;
46
47        this.changeWeapon(rifle.name());
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.Down, shoot, "Shoots");
58        Game.Keyboard.Listen(Key.D1, ButtonState.Pressed, changeWeapon, "Changes weapon to the pistol", pistol.name());
59        Game.Keyboard.Listen(Key.D2, ButtonState.Pressed, changeWeapon, "Changes weapon to the rifle", rifle.name());
60    }
61
62    private void changeWeapon(string weaponName)
63    {
64        if (weaponName == "Pistol")
65        {
66            this.Weapon = pistol;
67        }
68        else if (weaponName == "Rifle")
69        {
70            this.Weapon = rifle;
71        }
72        else
73        {
74            throw new Exception("Invalid weapon name: '" + weaponName + "'");
75        }
76
77        // We only get here if the weaponName is valid
78        currentWeapon = weaponName;
79    }
80
81    // Because all the weapons have infinite ammo, we don't have to check for null
82    private void shoot()
83    {
84        PhysicsObject bullet = this.Weapon.Shoot();
85    }
86
87    private void bulletReachedTarget(PhysicsObject bullet, PhysicsObject target)
88    {
89        bullet.Destroy();
90
91        if (target.Tag == "MeleeAlien")
92        {
93            MeleeAlien alien = (MeleeAlien)target;
94            //alien.reduceHitPointsBy(this.Weapon.firePower());
95            alien.reduceHitPointsBy(10);
96        }
97    }
98
99    private void playWalkingSound(Timer sender)
100    {
101        this.walking.Play();
102    }
103
104    private void moveRight(int speed)
105    {
106        walker.Start();
107        this.Walk(speed);
108        //this.Image = Image.Mirror(portrait); // Doesn't work at all!
109    }
110
111    private void moveLeft(int speed)
112    {
113        walker.Start();
114        this.Walk(-speed);
115        this.Image = Image.Mirror(portrait);
116    }
117
118    public void jump(int jumpSpeed)
119    {
120        this.Jump(jumpSpeed);
121    }
122}
Note: See TracBrowser for help on using the repository browser.