1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 |
|
18 #include "racecar.h" |
|
19 |
|
20 // ----------------------------------------------------------------------------- |
|
21 // |
|
22 // ----------------------------------------------------------------------------- |
|
23 // |
|
24 CRaceCar* CRaceCar::NewL( const TDesC& aColor, TUint aFuelTankSize ) |
|
25 { |
|
26 CRaceCar* self = NewLC( aColor, aFuelTankSize ); |
|
27 CleanupStack::Pop( self ); |
|
28 return self; |
|
29 } |
|
30 |
|
31 // ----------------------------------------------------------------------------- |
|
32 // |
|
33 // ----------------------------------------------------------------------------- |
|
34 // |
|
35 CRaceCar* CRaceCar::NewLC( const TDesC& aColor, TUint aFuelTankSize ) |
|
36 { |
|
37 CRaceCar* self = new( ELeave )CRaceCar( aFuelTankSize ); |
|
38 CleanupStack::PushL( self ); |
|
39 self->ConstructL( aColor ); |
|
40 return self; |
|
41 } |
|
42 |
|
43 // ----------------------------------------------------------------------------- |
|
44 // |
|
45 // ----------------------------------------------------------------------------- |
|
46 // |
|
47 CRaceCar::CRaceCar( TUint aFuelTankSize ) |
|
48 : iFuelTankSize( aFuelTankSize ) |
|
49 { |
|
50 } |
|
51 |
|
52 // ----------------------------------------------------------------------------- |
|
53 // |
|
54 // ----------------------------------------------------------------------------- |
|
55 // |
|
56 void CRaceCar::ConstructL( const TDesC& aColor ) |
|
57 { |
|
58 iColor = aColor.AllocL(); |
|
59 } |
|
60 |
|
61 // ----------------------------------------------------------------------------- |
|
62 // |
|
63 // ----------------------------------------------------------------------------- |
|
64 // |
|
65 CRaceCar::~CRaceCar() |
|
66 { |
|
67 delete iColor; |
|
68 } |
|
69 |
|
70 // ----------------------------------------------------------------------------- |
|
71 // |
|
72 // ----------------------------------------------------------------------------- |
|
73 // |
|
74 TUint CRaceCar::FuelTankSize() const |
|
75 { |
|
76 return iFuelTankSize; |
|
77 } |
|
78 |
|
79 // ----------------------------------------------------------------------------- |
|
80 // |
|
81 // ----------------------------------------------------------------------------- |
|
82 // |
|
83 TUint CRaceCar::FuelLeft() const |
|
84 { |
|
85 return iFuel; |
|
86 } |
|
87 |
|
88 // ----------------------------------------------------------------------------- |
|
89 // |
|
90 // ----------------------------------------------------------------------------- |
|
91 // |
|
92 TUint CRaceCar::AddFuel( TUint aFuel ) |
|
93 { |
|
94 TUint extraFuel = 0; |
|
95 if ( aFuel > iFuelTankSize - iFuel ) |
|
96 { |
|
97 extraFuel = aFuel- ( iFuelTankSize - iFuel ); |
|
98 iFuel = iFuelTankSize; |
|
99 } |
|
100 else |
|
101 { |
|
102 iFuel += aFuel; |
|
103 } |
|
104 return extraFuel; |
|
105 } |
|
106 |
|
107 // ----------------------------------------------------------------------------- |
|
108 // |
|
109 // ----------------------------------------------------------------------------- |
|
110 // |
|
111 TInt CRaceCar::Steer( TInt aAngle ) |
|
112 { |
|
113 if ( ( iDirection + aAngle ) < -KMaxTyreAngle || |
|
114 ( iDirection + aAngle ) > KMaxTyreAngle ) |
|
115 { |
|
116 return KErrArgument; |
|
117 } |
|
118 iDirection += aAngle; |
|
119 return KErrNone; |
|
120 } |
|
121 |
|
122 // ----------------------------------------------------------------------------- |
|
123 // |
|
124 // ----------------------------------------------------------------------------- |
|
125 // |
|
126 TInt CRaceCar::Direction() const |
|
127 { |
|
128 return iDirection; |
|
129 } |
|
130 |
|
131 // ----------------------------------------------------------------------------- |
|
132 // |
|
133 // ----------------------------------------------------------------------------- |
|
134 // |
|
135 TInt CRaceCar::Accelerate( TUint aForce ) |
|
136 { |
|
137 if ( iFuel == 0 ) |
|
138 { |
|
139 return KErrGeneral; |
|
140 } |
|
141 else |
|
142 { |
|
143 iFuel--; |
|
144 iSpeed += aForce; |
|
145 } |
|
146 return KErrNone; |
|
147 } |
|
148 |
|
149 // ----------------------------------------------------------------------------- |
|
150 // |
|
151 // ----------------------------------------------------------------------------- |
|
152 // |
|
153 TUint CRaceCar::Speed() const |
|
154 { |
|
155 return iSpeed; |
|
156 } |
|
157 |
|
158 // ----------------------------------------------------------------------------- |
|
159 // |
|
160 // ----------------------------------------------------------------------------- |
|
161 // |
|
162 void CRaceCar::Brake( TUint aForce ) |
|
163 { |
|
164 if ( iSpeed < aForce ) |
|
165 { |
|
166 iSpeed = 0; |
|
167 } |
|
168 else |
|
169 { |
|
170 iSpeed -= aForce; |
|
171 } |
|
172 } |
|
173 |
|
174 // ----------------------------------------------------------------------------- |
|
175 // |
|
176 // ----------------------------------------------------------------------------- |
|
177 // |
|
178 const TDesC& CRaceCar::Color() const |
|
179 { |
|
180 return *iColor; |
|
181 } |
|
182 |
|
183 // ----------------------------------------------------------------------------- |
|
184 // |
|
185 // ----------------------------------------------------------------------------- |
|
186 // |
|
187 void CRaceCar::ChangeColorL( const TDesC& aNewColor ) |
|
188 { |
|
189 __ASSERT_ALWAYS ( aNewColor.Length() > 0, User::Leave( KErrArgument ) ); |
|
190 |
|
191 HBufC* tmpColor = aNewColor.AllocL(); |
|
192 delete iColor; |
|
193 iColor = tmpColor; |
|
194 } |
|