author | Henek |
Sat, 11 Dec 2010 14:03:49 +0100 | |
changeset 4506 | 37744d5c877e |
parent 4493 | 45db8e97d282 |
permissions | -rw-r--r-- |
3234 | 1 |
-- Hedgewars Shotgun Training |
2 |
-- Scripting Example |
|
3 |
||
4 |
-- Lines such as this one are comments - they are ignored |
|
5 |
-- by the game, no matter what kind of text is in there. |
|
6 |
-- It's also possible to place a comment after some real |
|
7 |
-- instruction as you see below. In short, everything |
|
8 |
-- following "--" is ignored. |
|
9 |
||
10 |
--------------------------------------------------------------- |
|
4506
37744d5c877e
Finnished up the lua translations by adding training maps, campaign is ignored for now
Henek
parents:
4493
diff
changeset
|
11 |
-- At first we implement the localization library using loadfile. |
37744d5c877e
Finnished up the lua translations by adding training maps, campaign is ignored for now
Henek
parents:
4493
diff
changeset
|
12 |
-- This allows us to localize strings without needing to think |
37744d5c877e
Finnished up the lua translations by adding training maps, campaign is ignored for now
Henek
parents:
4493
diff
changeset
|
13 |
-- about translations. |
37744d5c877e
Finnished up the lua translations by adding training maps, campaign is ignored for now
Henek
parents:
4493
diff
changeset
|
14 |
-- We can use the function loc(text) to localize a string. |
3234 | 15 |
|
4506
37744d5c877e
Finnished up the lua translations by adding training maps, campaign is ignored for now
Henek
parents:
4493
diff
changeset
|
16 |
loadfile(GetDataPath() .. "Scripts/Locale.lua")() |
3234 | 17 |
|
18 |
-- This variable will hold the number of destroyed targets. |
|
19 |
local score = 0 |
|
20 |
-- This variable represents the number of targets to destroy. |
|
21 |
local score_goal = 5 |
|
22 |
-- This variable controls how many milliseconds/ticks we'd |
|
23 |
-- like to wait before we end the round once all targets |
|
24 |
-- have been destroyed. |
|
25 |
local end_timer = 5000 -- 5000 ms = 5 s |
|
26 |
-- This variable is set to true if the game is lost (i.e. |
|
27 |
-- time runs out). |
|
28 |
local game_lost = false |
|
29 |
-- This variable will point to the hog's gear |
|
30 |
local player = nil |
|
31 |
-- This variable will grab the time left at the end of the round |
|
32 |
local time_goal = 0 |
|
33 |
||
34 |
-- This is a custom function to make it easier to |
|
35 |
-- spawn more targets with just one line of code |
|
36 |
-- You may define as many custom functions as you |
|
37 |
-- like. |
|
38 |
function spawnTarget() |
|
39 |
-- add a new target gear |
|
40 |
gear = AddGear(0, 0, gtTarget, 0, 0, 0, 0) |
|
41 |
||
42 |
-- move it to a random position within 0 and |
|
43 |
-- LAND_WIDTH - the width of the map |
|
44 |
FindPlace(gear, true, 0, LAND_WIDTH) |
|
45 |
||
46 |
-- move the target to a higher vertical position |
|
47 |
-- to ensure it's not somewhere down below |
|
48 |
x, y = GetGearPosition(gear) |
|
49 |
SetGearPosition(gear, x, 500) |
|
50 |
end |
|
51 |
||
52 |
-- This function is called before the game loads its |
|
53 |
-- resources. |
|
54 |
-- It's one of the predefined function names that will |
|
55 |
-- be called by the game. They give you entry points |
|
56 |
-- where you're able to call your own code using either |
|
57 |
-- provided instructions or custom functions. |
|
58 |
function onGameInit() |
|
59 |
-- At first we have to overwrite/set some global variables |
|
60 |
-- that define the map, the game has to load, as well as |
|
61 |
-- other things such as the game rules to use, etc. |
|
62 |
-- Things we don't modify here will use their default values. |
|
63 |
||
64 |
-- The base number for the random number generator |
|
65 |
Seed = 0 |
|
66 |
-- Game settings and rules |
|
67 |
GameFlags = gfMultiWeapon + gfOneClanMode |
|
68 |
-- The time the player has to move each round (in ms) |
|
69 |
TurnTime = 25000 |
|
70 |
-- The frequency of crate drops |
|
71 |
CaseFreq = 0 |
|
72 |
-- The number of mines being placed |
|
4162 | 73 |
MinesNum = 0 |
3234 | 74 |
-- The number of explosives being placed |
75 |
Explosives = 0 |
|
76 |
-- The delay between each round |
|
77 |
Delay = 0 |
|
78 |
-- The map to be played |
|
79 |
Map = "Mushrooms" |
|
80 |
-- The theme to be used |
|
81 |
Theme = "Nature" |
|
82 |
||
83 |
-- Create the player team |
|
4506
37744d5c877e
Finnished up the lua translations by adding training maps, campaign is ignored for now
Henek
parents:
4493
diff
changeset
|
84 |
AddTeam(loc("Shotgun Team"), 14483456, "Simple", "Island", "Default") |
3234 | 85 |
-- And add a hog to it |
4506
37744d5c877e
Finnished up the lua translations by adding training maps, campaign is ignored for now
Henek
parents:
4493
diff
changeset
|
86 |
player = AddHog(loc("Hunter"), 0, 1, "NoHat") |
4493
45db8e97d282
removing all ; from lua script as an act of temporary rage and perfectionism
Henek
parents:
4350
diff
changeset
|
87 |
SetGearPosition(player, 2334, 1254) |
3234 | 88 |
end |
89 |
||
90 |
-- This function is called when the round starts |
|
91 |
-- it spawns the first target that has to be destroyed. |
|
92 |
-- In addition it shows the scenario goal(s). |
|
93 |
function onGameStart() |
|
94 |
-- Spawn the first target. |
|
95 |
spawnTarget() |
|
96 |
||
97 |
-- Show some nice mission goals. |
|
98 |
-- Parameters are: caption, sub caption, description, |
|
99 |
-- extra text, icon and time to show. |
|
100 |
-- A negative icon parameter (-n) represents the n-th weapon icon |
|
101 |
-- A positive icon paramter (n) represents the (n+1)-th mission icon |
|
102 |
-- A timeframe of 0 is replaced with the default time to show. |
|
4506
37744d5c877e
Finnished up the lua translations by adding training maps, campaign is ignored for now
Henek
parents:
4493
diff
changeset
|
103 |
ShowMission(loc("Shotgun Training"), loc("Aiming Practice"), loc("Eliminate all targets before your time runs out.|You have unlimited ammo for this mission."), -amShotgun, 0) |
3234 | 104 |
end |
105 |
||
106 |
-- This function is called every game tick. |
|
107 |
-- Note that there are 1000 ticks within one second. |
|
108 |
-- You shouldn't try to calculate too complicated |
|
109 |
-- code here as this might slow down your game. |
|
110 |
function onGameTick() |
|
111 |
-- If time's up, set the game to be lost. |
|
112 |
-- We actually check the time to be "1 ms" as it |
|
113 |
-- will be at "0 ms" right at the start of the game. |
|
114 |
if TurnTimeLeft == 1 and score < score_goal then |
|
115 |
game_lost = true |
|
116 |
-- ... and show a short message. |
|
4506
37744d5c877e
Finnished up the lua translations by adding training maps, campaign is ignored for now
Henek
parents:
4493
diff
changeset
|
117 |
ShowMission(loc("Shotgun Training"), loc("Aiming Practice"), loc("Oh no! Time's up! Just try again."), -amSkip, 0) |
3234 | 118 |
-- How about killing our poor hog due to his poor performance? |
4493
45db8e97d282
removing all ; from lua script as an act of temporary rage and perfectionism
Henek
parents:
4350
diff
changeset
|
119 |
SetHealth(player, 0) |
3234 | 120 |
-- Just to be sure set the goal time to 1 ms |
121 |
time_goal = 1 |
|
122 |
end |
|
123 |
-- If the goal is reached or we've lost ... |
|
124 |
if score == score_goal or game_lost then |
|
125 |
-- ... check to see if the time we'd like to |
|
126 |
-- wait has passed and then ... |
|
127 |
if end_timer == 0 then |
|
128 |
-- ... end the game ... |
|
129 |
EndGame() |
|
130 |
else |
|
131 |
-- ... or just lower the timer by 1. |
|
132 |
end_timer = end_timer - 1 |
|
133 |
-- Reset the time left to stop the timer |
|
134 |
TurnTimeLeft = time_goal |
|
135 |
end |
|
136 |
end |
|
137 |
end |
|
138 |
||
139 |
-- This function is called when the game is initialized |
|
140 |
-- to request the available ammo and probabilities |
|
141 |
function onAmmoStoreInit() |
|
142 |
-- add an unlimited supply of shotgun ammo |
|
3346 | 143 |
SetAmmo(amShotgun, 9, 0, 0, 0) |
3234 | 144 |
end |
145 |
||
146 |
-- This function is called when a new gear is added. |
|
147 |
-- We don't need it for this training, so we can |
|
148 |
-- keep it empty. |
|
149 |
function onGearAdd(gear) |
|
150 |
end |
|
151 |
||
152 |
-- This function is called before a gear is destroyed. |
|
153 |
-- We use it to count the number of targets destroyed. |
|
154 |
function onGearDelete(gear) |
|
155 |
-- We're only interested in target gears. |
|
156 |
if GetGearType(gear) == gtTarget then |
|
157 |
-- Add one point to our score/counter |
|
158 |
score = score + 1 |
|
159 |
-- If we haven't reached the goal ... |
|
160 |
if score < score_goal then |
|
161 |
-- ... spawn another target. |
|
162 |
spawnTarget() |
|
163 |
else |
|
164 |
if not game_lost then |
|
165 |
-- Otherwise show that the goal was accomplished |
|
4506
37744d5c877e
Finnished up the lua translations by adding training maps, campaign is ignored for now
Henek
parents:
4493
diff
changeset
|
166 |
ShowMission(loc("Shotgun Training"), loc("Aiming Practice"), loc("Congratulations! You've eliminated all targets|within the allowed time frame."), 0, 0) |
3234 | 167 |
-- Also let the hogs shout "victory!" |
168 |
PlaySound(sndVictory) |
|
169 |
-- Save the time left so we may keep it. |
|
170 |
time_goal = TurnTimeLeft |
|
171 |
end |
|
172 |
end |
|
173 |
end |
|
174 |
end |